Skip to content

Commit 45ad353

Browse files
committed
chore(datacite-tracker): rewritten existing tests to respect recent refactor
1 parent e514759 commit 45ad353

7 files changed

Lines changed: 164 additions & 158 deletions

File tree

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
11
import { Store } from '@ngxs/store';
22

3+
import { TranslateModule } from '@ngx-translate/core';
34
import { MockProvider } from 'ng-mocks';
45

5-
import { BehaviorSubject } from 'rxjs';
6+
import { BehaviorSubject, of } from 'rxjs';
67

8+
import { signal } from '@angular/core';
79
import { ComponentFixture, TestBed } from '@angular/core/testing';
810

911
import { PreprintSelectors } from '@osf/features/preprints/store/preprint';
1012
import { IS_LARGE, IS_MEDIUM } from '@osf/shared/helpers';
1113
import { MOCK_STORE } from '@shared/mocks';
14+
import { DataciteService } from '@shared/services/datacite/datacite.service';
1215

1316
import { PreprintFileSectionComponent } from './preprint-file-section.component';
1417

15-
describe.skip('PreprintFileSectionComponent', () => {
18+
describe('PreprintFileSectionComponent', () => {
1619
let component: PreprintFileSectionComponent;
1720
let fixture: ComponentFixture<PreprintFileSectionComponent>;
18-
21+
let dataciteService: jest.Mocked<DataciteService>;
1922
const mockStore = MOCK_STORE;
2023
let isMediumSubject: BehaviorSubject<boolean>;
2124
let isLargeSubject: BehaviorSubject<boolean>;
25+
// const
2226

2327
beforeEach(async () => {
2428
(MOCK_STORE.selectSignal as jest.Mock).mockImplementation((selector) => {
2529
if (
2630
selector === PreprintSelectors.isPreprintFileLoading ||
27-
selector === PreprintSelectors.getPreprintFileVersions ||
31+
// selector === PreprintSelectors.getPreprintFileVersions ||
2832
selector === PreprintSelectors.arePreprintFileVersionsLoading
2933
) {
3034
return () => [];
35+
} else if (selector == PreprintSelectors.getPreprint) {
36+
return () => ({
37+
id: 1,
38+
});
39+
} else if (selector == PreprintSelectors.getPreprintFileVersions) {
40+
return signal([{ date: '12312312', downloadUrl: '21312', id: '1' }]);
3141
}
3242
return () => null;
3343
});
@@ -36,9 +46,12 @@ describe.skip('PreprintFileSectionComponent', () => {
3646
isLargeSubject = new BehaviorSubject<boolean>(true);
3747

3848
await TestBed.configureTestingModule({
39-
imports: [PreprintFileSectionComponent],
49+
imports: [PreprintFileSectionComponent, TranslateModule.forRoot()],
4050
providers: [
4151
MockProvider(Store, mockStore),
52+
MockProvider(DataciteService, {
53+
logIdentifiableDownload: jest.fn().mockReturnValue(of(void 0)),
54+
}),
4255
MockProvider(IS_MEDIUM, isMediumSubject),
4356
MockProvider(IS_LARGE, isLargeSubject),
4457
],
@@ -47,9 +60,27 @@ describe.skip('PreprintFileSectionComponent', () => {
4760
fixture = TestBed.createComponent(PreprintFileSectionComponent);
4861
component = fixture.componentInstance;
4962
fixture.detectChanges();
63+
dataciteService = TestBed.inject(DataciteService) as jest.MockedObject<DataciteService>;
64+
});
65+
66+
it('should call dataciteService.logIdentifiableDownload when logDownload is called', () => {
67+
component.logDownload();
68+
expect(dataciteService.logIdentifiableDownload).toHaveBeenCalledWith(component.preprint$);
5069
});
5170

52-
it('should create', () => {
53-
expect(component).toBeTruthy();
71+
it('should call logDownload when version menu item is clicked', () => {
72+
// Get the command from versionMenuItems
73+
fixture.detectChanges();
74+
const menuItems = component.versionMenuItems();
75+
expect(menuItems.length).toBeGreaterThan(0);
76+
77+
const versionCommand = menuItems[0].command!;
78+
jest.spyOn(component, 'logDownload');
79+
80+
// simulate clicking the menu item
81+
versionCommand();
82+
83+
expect(component.logDownload).toHaveBeenCalled();
84+
expect(dataciteService.logIdentifiableDownload).toHaveBeenCalledWith(expect.anything());
5485
});
5586
});

src/app/features/preprints/pages/preprint-details/preprint-details.component.spec.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { ShareAndDownloadComponent } from '@osf/features/preprints/components/pr
1717
import { PreprintSelectors } from '@osf/features/preprints/store/preprint';
1818
import { PreprintProvidersSelectors } from '@osf/features/preprints/store/preprint-providers';
1919
import { MOCK_PROVIDER, MOCK_STORE, TranslateServiceMock } from '@shared/mocks';
20-
import { Identifier } from '@shared/models';
2120
import { DataciteService } from '@shared/services/datacite/datacite.service';
2221

2322
import { PreprintDetailsComponent } from './preprint-details.component';
@@ -51,7 +50,7 @@ describe('PreprintDetailsComponent', () => {
5150
});
5251
(MOCK_STORE.dispatch as jest.Mock).mockImplementation(() => of());
5352
dataciteService = {
54-
logView: jest.fn().mockReturnValue(of(void 0)),
53+
logIdentifiableView: jest.fn().mockReturnValue(of(void 0)),
5554
} as unknown as jest.Mocked<DataciteService>;
5655

5756
await TestBed.configureTestingModule({
@@ -86,30 +85,6 @@ describe('PreprintDetailsComponent', () => {
8685

8786
it('reacts to sequence of state changes', () => {
8887
fixture.detectChanges();
89-
expect(dataciteService.logView).toHaveBeenCalledTimes(0);
90-
91-
preprintSignal.set(getPreprint([]));
92-
93-
fixture.detectChanges();
94-
expect(dataciteService.logView).toHaveBeenCalledTimes(0);
95-
96-
preprintSignal.set(getPreprint([{ category: 'dio', value: '123', id: '', type: 'identifier' }]));
97-
fixture.detectChanges();
98-
expect(dataciteService.logView).toHaveBeenCalledTimes(0);
99-
100-
preprintSignal.set(getPreprint([{ category: 'doi', value: '123', id: '', type: 'identifier' }]));
101-
102-
fixture.detectChanges();
103-
expect(dataciteService.logView).toHaveBeenCalled();
104-
105-
preprintSignal.set(getPreprint([{ category: 'doi', value: '456', id: '', type: 'identifier' }]));
106-
fixture.detectChanges();
107-
expect(dataciteService.logView).toHaveBeenLastCalledWith('123');
88+
expect(dataciteService.logIdentifiableView).toHaveBeenCalledWith(component.preprint$);
10889
});
10990
});
110-
111-
function getPreprint(identifiers: Identifier[]) {
112-
return {
113-
identifiers: identifiers,
114-
};
115-
}

src/app/features/project/overview/project-overview.component.spec.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const sampleReviewAction: CollectionSubmissionReviewAction = {
6565
describe('ProjectOverviewComponent', () => {
6666
let fixture: ComponentFixture<ProjectOverviewComponent>;
6767
let dataciteService: jest.Mocked<DataciteService>;
68+
let component: ProjectOverviewComponent;
6869
const projectSignal = signal<any>(getProject());
6970

7071
const activatedRouteMock = {
@@ -118,7 +119,7 @@ describe('ProjectOverviewComponent', () => {
118119
});
119120

120121
dataciteService = {
121-
logView: jest.fn().mockReturnValue(of(void 0)),
122+
logIdentifiableView: jest.fn().mockReturnValue(of(void 0)),
122123
} as unknown as jest.Mocked<DataciteService>;
123124

124125
await TestBed.configureTestingModule({
@@ -142,6 +143,7 @@ describe('ProjectOverviewComponent', () => {
142143
ViewOnlyLinkMessageComponent,
143144
],
144145
providers: [
146+
TranslatePipe,
145147
{ provide: ActivatedRoute, useValue: activatedRouteMock },
146148
{ provide: Store, useValue: MOCK_STORE },
147149
{ provide: DataciteService, useValue: dataciteService },
@@ -152,32 +154,14 @@ describe('ProjectOverviewComponent', () => {
152154
TranslateService,
153155
],
154156
}).compileComponents();
155-
156157
fixture = TestBed.createComponent(ProjectOverviewComponent);
158+
component = fixture.componentInstance;
157159
fixture.detectChanges();
158160
});
159161

160162
it('reacts to sequence of state changes', () => {
161163
fixture.detectChanges();
162-
expect(dataciteService.logView).toHaveBeenCalledTimes(0);
163-
164-
projectSignal.set(getProject());
165-
166-
fixture.detectChanges();
167-
expect(dataciteService.logView).toHaveBeenCalledTimes(0);
168-
169-
projectSignal.set(getProject([{ category: 'dio', value: '123', id: '', type: 'identifier' }]));
170-
fixture.detectChanges();
171-
expect(dataciteService.logView).toHaveBeenCalledTimes(0);
172-
173-
projectSignal.set(getProject([{ category: 'doi', value: '123', id: '', type: 'identifier' }]));
174-
175-
fixture.detectChanges();
176-
expect(dataciteService.logView).toHaveBeenCalled();
177-
178-
projectSignal.set(getProject([{ category: 'doi', value: '456', id: '', type: 'identifier' }]));
179-
fixture.detectChanges();
180-
expect(dataciteService.logView).toHaveBeenLastCalledWith('123');
164+
expect(dataciteService.logIdentifiableView).toHaveBeenCalledWith(component.currentProject$);
181165
});
182166

183167
function getProject(identifiers?: Identifier[]) {

src/app/features/project/overview/project-overview.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class ProjectOverviewComponent implements OnInit {
154154

155155
currentProject = select(ProjectOverviewSelectors.getProject);
156156
isAnonymous = select(ProjectOverviewSelectors.isProjectAnonymous);
157-
private currentProject$ = toObservable(this.currentProject);
157+
currentProject$ = toObservable(this.currentProject);
158158

159159
userPermissions = computed(() => {
160160
return this.currentProject()?.currentUserPermissions || [];

src/app/features/registry/registry.component.spec.ts

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import { TestBed } from '@angular/core/testing';
88

99
import { RegistryOverviewSelectors } from '@osf/features/registry/store/registry-overview';
1010
import { MetaTagsService } from '@osf/shared/services';
11-
import { Identifier } from '@shared/models';
1211
import { DataciteService } from '@shared/services/datacite/datacite.service';
1312

1413
import { RegistryComponent } from './registry.component';
1514

1615
describe('RegistryComponent', () => {
1716
let fixture: any;
17+
let component: RegistryComponent;
1818
let dataciteService: jest.Mocked<DataciteService>;
1919

2020
const registrySignal = signal<any | null>(null);
2121

2222
beforeEach(async () => {
2323
dataciteService = {
24-
logView: jest.fn().mockReturnValue(of(void 0)),
24+
logIdentifiableView: jest.fn().mockReturnValue(of(void 0)),
2525
} as unknown as jest.Mocked<DataciteService>;
2626

2727
const mockStore = {
@@ -47,48 +47,12 @@ describe('RegistryComponent', () => {
4747
}).compileComponents();
4848

4949
fixture = TestBed.createComponent(RegistryComponent);
50+
component = fixture.componentInstance;
5051
TestBed.inject(MetaTagsService);
5152
});
5253

5354
it('reacts to sequence of state changes', () => {
54-
registrySignal.set(null);
5555
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');
56+
expect(dataciteService.logIdentifiableView).toHaveBeenCalledWith(component.registry$);
7557
});
7658
});
77-
78-
function getRegistry(identifiers: Identifier[]) {
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)