Skip to content

Commit 98c64c7

Browse files
committed
chore(datacite-tracker): added tests to registry component
1 parent 224879b commit 98c64c7

2 files changed

Lines changed: 80 additions & 9 deletions

File tree

jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ module.exports = {
7070
testPathIgnorePatterns: [
7171
'<rootDir>/src/app/app.config.ts',
7272
'<rootDir>/src/app/app.routes.ts',
73-
'<rootDir>/src/app/features/registry/',
7473
'<rootDir>/src/app/features/project/addons/components/configure-configure-addon/',
7574
'<rootDir>/src/app/features/project/addons/components/connect-configured-addon/',
7675
'<rootDir>/src/app/features/project/addons/components/disconnect-addon-modal/',
Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,94 @@
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';
213

314
import { RegistryComponent } from './registry.component';
415

516
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);
821

922
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+
1036
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+
],
1247
}).compileComponents();
1348

1449
fixture = TestBed.createComponent(RegistryComponent);
15-
component = fixture.componentInstance;
16-
fixture.detectChanges();
50+
TestBed.inject(MetaTagsService);
1751
});
1852

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');
2175
});
2276
});
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

Comments
 (0)