|
1 | | -import { TranslatePipe } from '@ngx-translate/core'; |
2 | | -import { MockComponents } from 'ng-mocks'; |
| 1 | +import { MockComponents, MockProvider } from 'ng-mocks'; |
3 | 2 |
|
| 3 | +import { signal } from '@angular/core'; |
4 | 4 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
5 | | -import { FormsModule } from '@angular/forms'; |
| 5 | +import { ActivatedRoute, Router } from '@angular/router'; |
6 | 6 |
|
| 7 | +import { ENVIRONMENT } from '@core/provider/environment.provider'; |
7 | 8 | import { CustomPaginatorComponent } from '@osf/shared/components/custom-paginator/custom-paginator.component'; |
8 | 9 | import { LoadingSpinnerComponent } from '@osf/shared/components/loading-spinner/loading-spinner.component'; |
9 | 10 | import { RegistrationCardComponent } from '@osf/shared/components/registration-card/registration-card.component'; |
10 | 11 | import { SubHeaderComponent } from '@osf/shared/components/sub-header/sub-header.component'; |
| 12 | +import { CurrentResourceSelectors } from '@shared/stores/current-resource'; |
11 | 13 |
|
12 | 14 | import { RegistrationsComponent } from './registrations.component'; |
| 15 | +import { GetRegistrations, RegistrationsSelectors } from './store'; |
| 16 | + |
| 17 | +import { MOCK_REGISTRATION } from '@testing/mocks/registration.mock'; |
| 18 | +import { OSFTestingModule } from '@testing/osf.testing.module'; |
| 19 | +import { ActivatedRouteMockBuilder } from '@testing/providers/route-provider.mock'; |
| 20 | +import { RouterMockBuilder } from '@testing/providers/router-provider.mock'; |
| 21 | +import { provideMockStore } from '@testing/providers/store-provider.mock'; |
13 | 22 |
|
14 | 23 | describe('RegistrationsComponent', () => { |
15 | 24 | let component: RegistrationsComponent; |
16 | 25 | let fixture: ComponentFixture<RegistrationsComponent>; |
| 26 | + let routerMock: ReturnType<RouterMockBuilder['build']>; |
| 27 | + let activatedRouteMock: ReturnType<ActivatedRouteMockBuilder['build']>; |
| 28 | + let storeDispatchSpy: jest.SpyInstance; |
| 29 | + |
| 30 | + const mockProjectId = 'project-123'; |
| 31 | + const mockRegistrations = [MOCK_REGISTRATION]; |
| 32 | + const mockEnvironment = { |
| 33 | + defaultProvider: 'test-provider', |
| 34 | + }; |
17 | 35 |
|
18 | 36 | beforeEach(async () => { |
| 37 | + routerMock = RouterMockBuilder.create().build(); |
| 38 | + activatedRouteMock = ActivatedRouteMockBuilder.create().withParams({ id: mockProjectId }).build(); |
| 39 | + |
| 40 | + const mockStore = provideMockStore({ |
| 41 | + signals: [ |
| 42 | + { selector: CurrentResourceSelectors.hasAdminAccess, value: signal(true) }, |
| 43 | + { selector: RegistrationsSelectors.getRegistrations, value: signal(mockRegistrations) }, |
| 44 | + { selector: RegistrationsSelectors.getRegistrationsTotalCount, value: signal(10) }, |
| 45 | + { selector: RegistrationsSelectors.isRegistrationsLoading, value: signal(false) }, |
| 46 | + ], |
| 47 | + }); |
| 48 | + |
| 49 | + storeDispatchSpy = jest.spyOn(mockStore.useValue, 'dispatch'); |
| 50 | + |
19 | 51 | await TestBed.configureTestingModule({ |
20 | 52 | imports: [ |
21 | 53 | RegistrationsComponent, |
| 54 | + OSFTestingModule, |
22 | 55 | ...MockComponents( |
23 | 56 | RegistrationCardComponent, |
24 | 57 | SubHeaderComponent, |
25 | | - FormsModule, |
26 | | - TranslatePipe, |
27 | 58 | LoadingSpinnerComponent, |
28 | 59 | CustomPaginatorComponent |
29 | 60 | ), |
30 | 61 | ], |
| 62 | + providers: [ |
| 63 | + MockProvider(Router, routerMock), |
| 64 | + MockProvider(ActivatedRoute, activatedRouteMock), |
| 65 | + MockProvider(ENVIRONMENT, mockEnvironment), |
| 66 | + mockStore, |
| 67 | + ], |
31 | 68 | }).compileComponents(); |
32 | 69 |
|
33 | 70 | fixture = TestBed.createComponent(RegistrationsComponent); |
34 | 71 | component = fixture.componentInstance; |
35 | | - fixture.detectChanges(); |
36 | 72 | }); |
37 | 73 |
|
38 | | - it('should create', () => { |
39 | | - expect(component).toBeTruthy(); |
| 74 | + it('should have default values', () => { |
| 75 | + expect(component.itemsPerPage).toBe(10); |
| 76 | + expect(component.first).toBe(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should initialize projectId from route params', () => { |
| 80 | + expect(component.projectId()).toBe(mockProjectId); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should dispatch getRegistrations action on ngOnInit', () => { |
| 84 | + component.ngOnInit(); |
| 85 | + |
| 86 | + expect(storeDispatchSpy).toHaveBeenCalledWith( |
| 87 | + expect.objectContaining({ |
| 88 | + projectId: mockProjectId, |
| 89 | + page: 1, |
| 90 | + pageSize: 10, |
| 91 | + }) |
| 92 | + ); |
| 93 | + expect(storeDispatchSpy).toHaveBeenCalledWith(expect.any(GetRegistrations)); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should navigate to registries route when addRegistration is called', () => { |
| 97 | + const navigateSpy = jest.spyOn(routerMock, 'navigate'); |
| 98 | + |
| 99 | + component.addRegistration(); |
| 100 | + |
| 101 | + expect(navigateSpy).toHaveBeenCalledWith([`registries/${mockEnvironment.defaultProvider}/new`], { |
| 102 | + queryParams: { projectId: mockProjectId }, |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should dispatch getRegistrations and update first on page change', () => { |
| 107 | + const mockPaginatorState = { |
| 108 | + page: 2, |
| 109 | + first: 20, |
| 110 | + rows: 10, |
| 111 | + pageCount: 5, |
| 112 | + } as any; |
| 113 | + |
| 114 | + component.onPageChange(mockPaginatorState); |
| 115 | + |
| 116 | + expect(storeDispatchSpy).toHaveBeenCalledWith( |
| 117 | + expect.objectContaining({ |
| 118 | + projectId: mockProjectId, |
| 119 | + page: 3, |
| 120 | + pageSize: 10, |
| 121 | + }) |
| 122 | + ); |
| 123 | + expect(component.first).toBe(20); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should handle page change with page 0', () => { |
| 127 | + const mockPaginatorState = { |
| 128 | + page: 0, |
| 129 | + first: 0, |
| 130 | + rows: 10, |
| 131 | + pageCount: 5, |
| 132 | + } as any; |
| 133 | + |
| 134 | + component.onPageChange(mockPaginatorState); |
| 135 | + |
| 136 | + expect(storeDispatchSpy).toHaveBeenCalledWith( |
| 137 | + expect.objectContaining({ |
| 138 | + projectId: mockProjectId, |
| 139 | + page: 1, |
| 140 | + pageSize: 10, |
| 141 | + }) |
| 142 | + ); |
| 143 | + expect(component.first).toBe(0); |
40 | 144 | }); |
41 | 145 | }); |
0 commit comments