|
| 1 | +import { Store } from '@ngxs/store'; |
| 2 | + |
| 3 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 4 | + |
| 5 | +import { CurrentResourceType } from '@osf/shared/enums/resource-type.enum'; |
| 6 | +import { ActivityLogsSelectors, ClearActivityLogs } from '@osf/shared/stores/activity-logs'; |
| 7 | + |
| 8 | +import { ProjectRecentActivityComponent } from './project-recent-activity.component'; |
| 9 | + |
| 10 | +import { MOCK_ACTIVITY_LOGS_WITH_DISPLAY } from '@testing/mocks/activity-log-with-display.mock'; |
| 11 | +import { OSFTestingModule } from '@testing/osf.testing.module'; |
| 12 | +import { provideMockStore } from '@testing/providers/store-provider.mock'; |
| 13 | + |
| 14 | +describe('ProjectRecentActivityComponent', () => { |
| 15 | + let component: ProjectRecentActivityComponent; |
| 16 | + let fixture: ComponentFixture<ProjectRecentActivityComponent>; |
| 17 | + let store: Store; |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + await TestBed.configureTestingModule({ |
| 21 | + imports: [ProjectRecentActivityComponent, OSFTestingModule], |
| 22 | + providers: [ |
| 23 | + provideMockStore({ |
| 24 | + signals: [ |
| 25 | + { selector: ActivityLogsSelectors.getActivityLogs, value: [] }, |
| 26 | + { selector: ActivityLogsSelectors.getActivityLogsTotalCount, value: 0 }, |
| 27 | + { selector: ActivityLogsSelectors.getActivityLogsLoading, value: false }, |
| 28 | + ], |
| 29 | + }), |
| 30 | + ], |
| 31 | + }).compileComponents(); |
| 32 | + |
| 33 | + store = TestBed.inject(Store); |
| 34 | + jest.spyOn(store, 'dispatch'); |
| 35 | + |
| 36 | + fixture = TestBed.createComponent(ProjectRecentActivityComponent); |
| 37 | + component = fixture.componentInstance; |
| 38 | + }); |
| 39 | + |
| 40 | + it('should initialize with default values', () => { |
| 41 | + expect(component.pageSize()).toBe(5); |
| 42 | + expect(component.currentPage()).toBe(1); |
| 43 | + expect(component.firstIndex()).toBe(0); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should dispatch GetActivityLogs when projectId is provided', () => { |
| 47 | + fixture.componentRef.setInput('projectId', 'project123'); |
| 48 | + fixture.detectChanges(); |
| 49 | + |
| 50 | + expect(store.dispatch).toHaveBeenCalledWith( |
| 51 | + expect.objectContaining({ |
| 52 | + resourceId: 'project123', |
| 53 | + resourceType: CurrentResourceType.Projects, |
| 54 | + page: 1, |
| 55 | + pageSize: 5, |
| 56 | + }) |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should not dispatch when projectId is not provided', () => { |
| 61 | + fixture.detectChanges(); |
| 62 | + |
| 63 | + expect(store.dispatch).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should dispatch GetActivityLogs when currentPage changes', () => { |
| 67 | + fixture.componentRef.setInput('projectId', 'project123'); |
| 68 | + fixture.detectChanges(); |
| 69 | + |
| 70 | + (store.dispatch as jest.Mock).mockClear(); |
| 71 | + |
| 72 | + component.currentPage.set(2); |
| 73 | + fixture.detectChanges(); |
| 74 | + |
| 75 | + expect(store.dispatch).toHaveBeenCalledWith( |
| 76 | + expect.objectContaining({ |
| 77 | + resourceId: 'project123', |
| 78 | + resourceType: CurrentResourceType.Projects, |
| 79 | + page: 2, |
| 80 | + pageSize: 5, |
| 81 | + }) |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should update currentPage and dispatch on page change', () => { |
| 86 | + fixture.componentRef.setInput('projectId', 'project123'); |
| 87 | + fixture.detectChanges(); |
| 88 | + |
| 89 | + (store.dispatch as jest.Mock).mockClear(); |
| 90 | + |
| 91 | + component.onPageChange({ page: 1 } as any); |
| 92 | + fixture.detectChanges(); |
| 93 | + |
| 94 | + expect(component.currentPage()).toBe(2); |
| 95 | + expect(store.dispatch).toHaveBeenCalledWith( |
| 96 | + expect.objectContaining({ |
| 97 | + page: 2, |
| 98 | + }) |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should not update currentPage when page is undefined', () => { |
| 103 | + fixture.componentRef.setInput('projectId', 'project123'); |
| 104 | + fixture.detectChanges(); |
| 105 | + |
| 106 | + const initialPage = component.currentPage(); |
| 107 | + component.onPageChange({} as any); |
| 108 | + |
| 109 | + expect(component.currentPage()).toBe(initialPage); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should compute firstIndex correctly', () => { |
| 113 | + component.currentPage.set(1); |
| 114 | + expect(component.firstIndex()).toBe(0); |
| 115 | + |
| 116 | + component.currentPage.set(2); |
| 117 | + expect(component.firstIndex()).toBe(5); |
| 118 | + |
| 119 | + component.currentPage.set(3); |
| 120 | + expect(component.firstIndex()).toBe(10); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should clear store on destroy', () => { |
| 124 | + fixture.componentRef.setInput('projectId', 'project123'); |
| 125 | + fixture.detectChanges(); |
| 126 | + |
| 127 | + (store.dispatch as jest.Mock).mockClear(); |
| 128 | + |
| 129 | + fixture.destroy(); |
| 130 | + |
| 131 | + expect(store.dispatch).toHaveBeenCalledWith(expect.any(ClearActivityLogs)); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should return activity logs from selector', () => { |
| 135 | + TestBed.resetTestingModule(); |
| 136 | + TestBed.configureTestingModule({ |
| 137 | + imports: [ProjectRecentActivityComponent, OSFTestingModule], |
| 138 | + providers: [ |
| 139 | + provideMockStore({ |
| 140 | + signals: [ |
| 141 | + { selector: ActivityLogsSelectors.getActivityLogs, value: MOCK_ACTIVITY_LOGS_WITH_DISPLAY }, |
| 142 | + { selector: ActivityLogsSelectors.getActivityLogsTotalCount, value: 2 }, |
| 143 | + { selector: ActivityLogsSelectors.getActivityLogsLoading, value: false }, |
| 144 | + ], |
| 145 | + }), |
| 146 | + ], |
| 147 | + }).compileComponents(); |
| 148 | + |
| 149 | + fixture = TestBed.createComponent(ProjectRecentActivityComponent); |
| 150 | + component = fixture.componentInstance; |
| 151 | + fixture.detectChanges(); |
| 152 | + |
| 153 | + expect(component.activityLogs()).toEqual(MOCK_ACTIVITY_LOGS_WITH_DISPLAY); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should return totalCount from selector', () => { |
| 157 | + TestBed.resetTestingModule(); |
| 158 | + TestBed.configureTestingModule({ |
| 159 | + imports: [ProjectRecentActivityComponent, OSFTestingModule], |
| 160 | + providers: [ |
| 161 | + provideMockStore({ |
| 162 | + signals: [ |
| 163 | + { selector: ActivityLogsSelectors.getActivityLogs, value: [] }, |
| 164 | + { selector: ActivityLogsSelectors.getActivityLogsTotalCount, value: 10 }, |
| 165 | + { selector: ActivityLogsSelectors.getActivityLogsLoading, value: false }, |
| 166 | + ], |
| 167 | + }), |
| 168 | + ], |
| 169 | + }).compileComponents(); |
| 170 | + |
| 171 | + fixture = TestBed.createComponent(ProjectRecentActivityComponent); |
| 172 | + component = fixture.componentInstance; |
| 173 | + fixture.detectChanges(); |
| 174 | + |
| 175 | + expect(component.totalCount()).toBe(10); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should return isLoading from selector', () => { |
| 179 | + TestBed.resetTestingModule(); |
| 180 | + TestBed.configureTestingModule({ |
| 181 | + imports: [ProjectRecentActivityComponent, OSFTestingModule], |
| 182 | + providers: [ |
| 183 | + provideMockStore({ |
| 184 | + signals: [ |
| 185 | + { selector: ActivityLogsSelectors.getActivityLogs, value: [] }, |
| 186 | + { selector: ActivityLogsSelectors.getActivityLogsTotalCount, value: 0 }, |
| 187 | + { selector: ActivityLogsSelectors.getActivityLogsLoading, value: true }, |
| 188 | + ], |
| 189 | + }), |
| 190 | + ], |
| 191 | + }).compileComponents(); |
| 192 | + |
| 193 | + fixture = TestBed.createComponent(ProjectRecentActivityComponent); |
| 194 | + component = fixture.componentInstance; |
| 195 | + fixture.detectChanges(); |
| 196 | + |
| 197 | + expect(component.isLoading()).toBe(true); |
| 198 | + }); |
| 199 | +}); |
0 commit comments