Skip to content

Commit a13b596

Browse files
authored
[ENG-10255] Part 2: Added unit tests for pages components in registries (#881)
- Ticket: [ENG-10255] - Feature flag: n/a ## Summary of Changes 1. Added unit tests for components in registries-metadata-step.
1 parent 5bb1c44 commit a13b596

20 files changed

Lines changed: 652 additions & 457 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,82 @@
1+
import { Store } from '@ngxs/store';
2+
13
import { MockComponent } from 'ng-mocks';
24

5+
import { signal, WritableSignal } from '@angular/core';
36
import { ComponentFixture, TestBed } from '@angular/core/testing';
4-
import { ActivatedRoute } from '@angular/router';
57

68
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';
817

918
import { RegistriesAffiliatedInstitutionComponent } from './registries-affiliated-institution.component';
1019

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';
1322
import { provideMockStore } from '@testing/providers/store-provider.mock';
1423

1524
describe('RegistriesAffiliatedInstitutionComponent', () => {
1625
let component: RegistriesAffiliatedInstitutionComponent;
1726
let fixture: ComponentFixture<RegistriesAffiliatedInstitutionComponent>;
18-
let mockActivatedRoute: ReturnType<ActivatedRouteMockBuilder['build']>;
27+
let store: Store;
28+
let resourceInstitutionsSignal: WritableSignal<Institution[]>;
1929

20-
beforeEach(async () => {
21-
mockActivatedRoute = ActivatedRouteMockBuilder.create().withParams({ id: 'draft-1' }).build();
30+
beforeEach(() => {
31+
resourceInstitutionsSignal = signal<Institution[]>([]);
2232

23-
await TestBed.configureTestingModule({
24-
imports: [
25-
RegistriesAffiliatedInstitutionComponent,
26-
OSFTestingModule,
27-
MockComponent(AffiliatedInstitutionSelectComponent),
28-
],
33+
TestBed.configureTestingModule({
34+
imports: [RegistriesAffiliatedInstitutionComponent, MockComponent(AffiliatedInstitutionSelectComponent)],
2935
providers: [
30-
{ provide: ActivatedRoute, useValue: mockActivatedRoute },
36+
provideOSFCore(),
3137
provideMockStore({
3238
signals: [
3339
{ selector: InstitutionsSelectors.getUserInstitutions, value: [] },
3440
{ selector: InstitutionsSelectors.areUserInstitutionsLoading, value: false },
35-
{ selector: InstitutionsSelectors.getResourceInstitutions, value: [] },
41+
{ selector: InstitutionsSelectors.getResourceInstitutions, value: resourceInstitutionsSignal },
3642
{ selector: InstitutionsSelectors.areResourceInstitutionsLoading, value: false },
3743
{ selector: InstitutionsSelectors.areResourceInstitutionsSubmitting, value: false },
3844
],
3945
}),
4046
],
41-
}).compileComponents();
47+
});
4248

49+
store = TestBed.inject(Store);
4350
fixture = TestBed.createComponent(RegistriesAffiliatedInstitutionComponent);
4451
component = fixture.componentInstance;
52+
fixture.componentRef.setInput('draftId', 'draft-1');
4553
fixture.detectChanges();
4654
});
4755

4856
it('should create', () => {
4957
expect(component).toBeTruthy();
5058
});
5159

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+
);
6265
});
6366

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+
);
7481
});
7582
});

src/app/features/registries/components/registries-metadata-step/registries-affiliated-institution/registries-affiliated-institution.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { TranslatePipe } from '@ngx-translate/core';
44

55
import { Card } from 'primeng/card';
66

7-
import { ChangeDetectionStrategy, Component, effect, inject, OnInit, signal } from '@angular/core';
8-
import { ActivatedRoute } from '@angular/router';
7+
import { ChangeDetectionStrategy, Component, effect, input, OnInit, signal } from '@angular/core';
98

109
import { AffiliatedInstitutionSelectComponent } from '@osf/shared/components/affiliated-institution-select/affiliated-institution-select.component';
1110
import { ResourceType } from '@osf/shared/enums/resource-type.enum';
@@ -25,8 +24,7 @@ import {
2524
changeDetection: ChangeDetectionStrategy.OnPush,
2625
})
2726
export class RegistriesAffiliatedInstitutionComponent implements OnInit {
28-
private readonly route = inject(ActivatedRoute);
29-
private readonly draftId = this.route.snapshot.params['id'];
27+
draftId = input.required<string>();
3028

3129
selectedInstitutions = signal<Institution[]>([]);
3230

@@ -53,10 +51,10 @@ export class RegistriesAffiliatedInstitutionComponent implements OnInit {
5351

5452
ngOnInit() {
5553
this.actions.fetchUserInstitutions();
56-
this.actions.fetchResourceInstitutions(this.draftId, ResourceType.DraftRegistration);
54+
this.actions.fetchResourceInstitutions(this.draftId(), ResourceType.DraftRegistration);
5755
}
5856

5957
institutionsSelected(institutions: Institution[]) {
60-
this.actions.updateResourceInstitutions(this.draftId, ResourceType.DraftRegistration, institutions);
58+
this.actions.updateResourceInstitutions(this.draftId(), ResourceType.DraftRegistration, institutions);
6159
}
6260
}

0 commit comments

Comments
 (0)