|
1 | | -import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 1 | +import { Store } from '@ngxs/store'; |
| 2 | + |
| 3 | +import { of } from 'rxjs'; |
| 4 | + |
| 5 | +import { DatePipe } from '@angular/common'; |
| 6 | +import { signal } from '@angular/core'; |
| 7 | +import { TestBed } from '@angular/core/testing'; |
| 8 | + |
| 9 | +import { ProjectIdentifiers } from '@osf/features/project/overview/models'; |
| 10 | +import { RegistryOverviewSelectors } from '@osf/features/registry/store/registry-overview'; |
| 11 | +import { MetaTagsService } from '@osf/shared/services'; |
| 12 | +import { DataciteService } from '@shared/services/datacite/datacite.service'; |
2 | 13 |
|
3 | 14 | import { RegistryComponent } from './registry.component'; |
4 | 15 |
|
5 | 16 | describe('RegistryComponent', () => { |
6 | | - let component: RegistryComponent; |
7 | | - let fixture: ComponentFixture<RegistryComponent>; |
| 17 | + let fixture: any; |
| 18 | + let dataciteService: jest.Mocked<DataciteService>; |
| 19 | + |
| 20 | + const registrySignal = signal<any | null>(null); |
8 | 21 |
|
9 | 22 | beforeEach(async () => { |
| 23 | + dataciteService = { |
| 24 | + logView: jest.fn().mockReturnValue(of(void 0)), |
| 25 | + } as unknown as jest.Mocked<DataciteService>; |
| 26 | + |
| 27 | + const mockStore = { |
| 28 | + selectSignal: jest.fn((selector: any) => { |
| 29 | + if (selector === RegistryOverviewSelectors.getRegistry) { |
| 30 | + return registrySignal; // return a signal, not an observable |
| 31 | + } |
| 32 | + return signal(null); |
| 33 | + }), |
| 34 | + }; |
| 35 | + |
10 | 36 | await TestBed.configureTestingModule({ |
11 | | - imports: [RegistryComponent], |
| 37 | + imports: [RegistryComponent], // standalone component |
| 38 | + providers: [ |
| 39 | + { provide: Store, useValue: mockStore }, |
| 40 | + DatePipe, |
| 41 | + { provide: DataciteService, useValue: dataciteService }, |
| 42 | + { |
| 43 | + provide: MetaTagsService, |
| 44 | + useValue: { updateMetaTagsForRoute: jest.fn() }, |
| 45 | + }, |
| 46 | + ], |
12 | 47 | }).compileComponents(); |
13 | 48 |
|
14 | 49 | fixture = TestBed.createComponent(RegistryComponent); |
15 | | - component = fixture.componentInstance; |
16 | | - fixture.detectChanges(); |
| 50 | + TestBed.inject(MetaTagsService); |
17 | 51 | }); |
18 | 52 |
|
19 | | - it('should create', () => { |
20 | | - expect(component).toBeTruthy(); |
| 53 | + it('reacts to sequence of state changes', () => { |
| 54 | + registrySignal.set(null); |
| 55 | + fixture.detectChanges(); |
| 56 | + expect(dataciteService.logView).toHaveBeenCalledTimes(0); |
| 57 | + |
| 58 | + registrySignal.set(getRegistry([])); |
| 59 | + |
| 60 | + fixture.detectChanges(); |
| 61 | + expect(dataciteService.logView).toHaveBeenCalledTimes(0); |
| 62 | + |
| 63 | + registrySignal.set(getRegistry([{ category: 'dio', value: '123', id: '', type: 'identifier' }])); |
| 64 | + fixture.detectChanges(); |
| 65 | + expect(dataciteService.logView).toHaveBeenCalledTimes(0); |
| 66 | + |
| 67 | + registrySignal.set(getRegistry([{ category: 'doi', value: '123', id: '', type: 'identifier' }])); |
| 68 | + |
| 69 | + fixture.detectChanges(); |
| 70 | + expect(dataciteService.logView).toHaveBeenCalled(); |
| 71 | + |
| 72 | + registrySignal.set(getRegistry([{ category: 'doi', value: '456', id: '', type: 'identifier' }])); |
| 73 | + fixture.detectChanges(); |
| 74 | + expect(dataciteService.logView).toHaveBeenLastCalledWith('123'); |
21 | 75 | }); |
22 | 76 | }); |
| 77 | + |
| 78 | +function getRegistry(identifiers: ProjectIdentifiers[]) { |
| 79 | + return { |
| 80 | + id: 'r1', |
| 81 | + title: 'Mock Registry', |
| 82 | + description: 'Test description', |
| 83 | + dateRegistered: new Date('2023-01-01'), |
| 84 | + dateModified: new Date('2023-02-01'), |
| 85 | + doi: '10.1000/mockdoi', |
| 86 | + tags: ['angular', 'jest'], |
| 87 | + license: { name: 'MIT' }, |
| 88 | + contributors: [ |
| 89 | + { givenName: 'Alice', familyName: 'Smith' }, |
| 90 | + { givenName: 'Bob', familyName: 'Brown' }, |
| 91 | + ], |
| 92 | + identifiers: identifiers, |
| 93 | + }; |
| 94 | +} |
0 commit comments