|
| 1 | +import { Store } from '@ngxs/store'; |
| 2 | + |
1 | 3 | import { MockComponent } from 'ng-mocks'; |
2 | 4 |
|
| 5 | +import { signal, WritableSignal } from '@angular/core'; |
3 | 6 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
4 | | -import { ActivatedRoute } from '@angular/router'; |
5 | 7 |
|
6 | 8 | import { AffiliatedInstitutionSelectComponent } from '@osf/shared/components/affiliated-institution-select/affiliated-institution-select.component'; |
7 | | -import { InstitutionsSelectors } from '@osf/shared/stores/institutions'; |
| 9 | +import { ResourceType } from '@osf/shared/enums/resource-type.enum'; |
| 10 | +import { Institution } from '@osf/shared/models/institutions/institutions.model'; |
| 11 | +import { |
| 12 | + FetchResourceInstitutions, |
| 13 | + FetchUserInstitutions, |
| 14 | + InstitutionsSelectors, |
| 15 | + UpdateResourceInstitutions, |
| 16 | +} from '@osf/shared/stores/institutions'; |
8 | 17 |
|
9 | 18 | import { RegistriesAffiliatedInstitutionComponent } from './registries-affiliated-institution.component'; |
10 | 19 |
|
11 | | -import { OSFTestingModule } from '@testing/osf.testing.module'; |
12 | | -import { ActivatedRouteMockBuilder } from '@testing/providers/route-provider.mock'; |
| 20 | +import { MOCK_INSTITUTION } from '@testing/mocks/institution.mock'; |
| 21 | +import { provideOSFCore } from '@testing/osf.testing.provider'; |
13 | 22 | import { provideMockStore } from '@testing/providers/store-provider.mock'; |
14 | 23 |
|
15 | 24 | describe('RegistriesAffiliatedInstitutionComponent', () => { |
16 | 25 | let component: RegistriesAffiliatedInstitutionComponent; |
17 | 26 | let fixture: ComponentFixture<RegistriesAffiliatedInstitutionComponent>; |
18 | | - let mockActivatedRoute: ReturnType<ActivatedRouteMockBuilder['build']>; |
| 27 | + let store: Store; |
| 28 | + let resourceInstitutionsSignal: WritableSignal<Institution[]>; |
19 | 29 |
|
20 | | - beforeEach(async () => { |
21 | | - mockActivatedRoute = ActivatedRouteMockBuilder.create().withParams({ id: 'draft-1' }).build(); |
| 30 | + beforeEach(() => { |
| 31 | + resourceInstitutionsSignal = signal<Institution[]>([]); |
22 | 32 |
|
23 | | - await TestBed.configureTestingModule({ |
24 | | - imports: [ |
25 | | - RegistriesAffiliatedInstitutionComponent, |
26 | | - OSFTestingModule, |
27 | | - MockComponent(AffiliatedInstitutionSelectComponent), |
28 | | - ], |
| 33 | + TestBed.configureTestingModule({ |
| 34 | + imports: [RegistriesAffiliatedInstitutionComponent, MockComponent(AffiliatedInstitutionSelectComponent)], |
29 | 35 | providers: [ |
30 | | - { provide: ActivatedRoute, useValue: mockActivatedRoute }, |
| 36 | + provideOSFCore(), |
31 | 37 | provideMockStore({ |
32 | 38 | signals: [ |
33 | 39 | { selector: InstitutionsSelectors.getUserInstitutions, value: [] }, |
34 | 40 | { selector: InstitutionsSelectors.areUserInstitutionsLoading, value: false }, |
35 | | - { selector: InstitutionsSelectors.getResourceInstitutions, value: [] }, |
| 41 | + { selector: InstitutionsSelectors.getResourceInstitutions, value: resourceInstitutionsSignal }, |
36 | 42 | { selector: InstitutionsSelectors.areResourceInstitutionsLoading, value: false }, |
37 | 43 | { selector: InstitutionsSelectors.areResourceInstitutionsSubmitting, value: false }, |
38 | 44 | ], |
39 | 45 | }), |
40 | 46 | ], |
41 | | - }).compileComponents(); |
| 47 | + }); |
42 | 48 |
|
| 49 | + store = TestBed.inject(Store); |
43 | 50 | fixture = TestBed.createComponent(RegistriesAffiliatedInstitutionComponent); |
44 | 51 | component = fixture.componentInstance; |
| 52 | + fixture.componentRef.setInput('draftId', 'draft-1'); |
45 | 53 | fixture.detectChanges(); |
46 | 54 | }); |
47 | 55 |
|
48 | 56 | it('should create', () => { |
49 | 57 | expect(component).toBeTruthy(); |
50 | 58 | }); |
51 | 59 |
|
52 | | - it('should dispatch updateResourceInstitutions on selection', () => { |
53 | | - const actionsMock = { |
54 | | - updateResourceInstitutions: jest.fn(), |
55 | | - fetchUserInstitutions: jest.fn(), |
56 | | - fetchResourceInstitutions: jest.fn(), |
57 | | - } as any; |
58 | | - Object.defineProperty(component, 'actions', { value: actionsMock }); |
59 | | - const selected = [{ id: 'i2' }] as any; |
60 | | - component.institutionsSelected(selected); |
61 | | - expect(actionsMock.updateResourceInstitutions).toHaveBeenCalledWith('draft-1', 8, selected); |
| 60 | + it('should dispatch fetchUserInstitutions and fetchResourceInstitutions on init', () => { |
| 61 | + expect(store.dispatch).toHaveBeenCalledWith(new FetchUserInstitutions()); |
| 62 | + expect(store.dispatch).toHaveBeenCalledWith( |
| 63 | + new FetchResourceInstitutions('draft-1', ResourceType.DraftRegistration) |
| 64 | + ); |
62 | 65 | }); |
63 | 66 |
|
64 | | - it('should fetch user and resource institutions on init', () => { |
65 | | - const actionsMock = { |
66 | | - updateResourceInstitutions: jest.fn(), |
67 | | - fetchUserInstitutions: jest.fn(), |
68 | | - fetchResourceInstitutions: jest.fn(), |
69 | | - } as any; |
70 | | - Object.defineProperty(component, 'actions', { value: actionsMock }); |
71 | | - component.ngOnInit(); |
72 | | - expect(actionsMock.fetchUserInstitutions).toHaveBeenCalled(); |
73 | | - expect(actionsMock.fetchResourceInstitutions).toHaveBeenCalledWith('draft-1', 8); |
| 67 | + it('should sync selectedInstitutions when resourceInstitutions emits', () => { |
| 68 | + const institutions: Institution[] = [MOCK_INSTITUTION as Institution]; |
| 69 | + resourceInstitutionsSignal.set(institutions); |
| 70 | + fixture.detectChanges(); |
| 71 | + expect(component.selectedInstitutions()).toEqual(institutions); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should dispatch updateResourceInstitutions on selection', () => { |
| 75 | + (store.dispatch as jest.Mock).mockClear(); |
| 76 | + const selected: Institution[] = [MOCK_INSTITUTION as Institution]; |
| 77 | + component.institutionsSelected(selected); |
| 78 | + expect(store.dispatch).toHaveBeenCalledWith( |
| 79 | + new UpdateResourceInstitutions('draft-1', ResourceType.DraftRegistration, selected) |
| 80 | + ); |
74 | 81 | }); |
75 | 82 | }); |
0 commit comments