Skip to content

Commit ca6179a

Browse files
authored
Test/644 preprints (#428)
* test(providers): added tests for the preprint components folder * test(preprints): added tests for the my-preprints, preprint-details, preprints-landing components * test(preprints-affiliated-institutions): added tests * test(title-and-abstract-step): added tests * test(create-new-version): added tests * test(preprint-provider-discover): added tests * test(provider-overview): added tests * test(pages): added tests * test(stepper-components): added tests * fix(tests): fixed failing tests * fix(tests): fixed failing tests
1 parent 48c22e6 commit ca6179a

File tree

49 files changed

+4547
-641
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+4547
-641
lines changed

jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ module.exports = {
6363
'<rootDir>/src/app/app.routes.ts',
6464
'<rootDir>/src/app/features/files/components',
6565
'<rootDir>/src/app/features/files/pages/file-detail',
66-
'<rootDir>/src/app/features/preprints/',
6766
'<rootDir>/src/app/features/project/addons/',
6867
'<rootDir>/src/app/features/project/overview/',
6968
'<rootDir>/src/app/features/project/registrations',
Lines changed: 30 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { MockProvider } from 'ng-mocks';
22

3-
import { PaginatorState } from 'primeng/paginator';
4-
53
import { ComponentFixture, TestBed } from '@angular/core/testing';
4+
import { FormControl } from '@angular/forms';
65
import { ActivatedRoute, Router } from '@angular/router';
76

87
import { InstitutionsSelectors } from '@osf/shared/stores/institutions';
@@ -54,69 +53,38 @@ describe.skip('Component: Institutions List', () => {
5453
expect(component).toBeTruthy();
5554
});
5655

57-
it('should update currentPage, first, and call updateQueryParams when page is provided', () => {
58-
const paginatorEvent: PaginatorState = {
59-
page: 1,
60-
first: 20,
61-
rows: 10,
62-
pageCount: 5,
63-
};
64-
65-
component.onPageChange(paginatorEvent);
66-
67-
expect(component.currentPage()).toBe(2);
68-
expect(component.first()).toBe(20);
69-
expect(routerMock.navigate).toHaveBeenCalledWith([], {
70-
relativeTo: expect.any(Object),
71-
queryParams: {
72-
page: '2',
73-
size: '10',
74-
},
75-
queryParamsHandling: 'merge',
76-
});
56+
it('should initialize with correct default values', () => {
57+
expect(component.classes).toBe('flex-1 flex flex-column w-full');
58+
expect(component.searchControl).toBeInstanceOf(FormControl);
59+
expect(component.searchControl.value).toBe('');
60+
});
61+
62+
it('should return institutions from store', () => {
63+
const institutions = component.institutions();
64+
expect(institutions).toBe(mockInstitutions);
65+
});
66+
67+
it('should return loading state from store', () => {
68+
const loading = component.institutionsLoading();
69+
expect(loading).toBe(false);
7770
});
7871

79-
it('should set currentPage to 1 when page is not provided', () => {
80-
const paginatorEvent: PaginatorState = {
81-
page: undefined,
82-
first: 0,
83-
rows: 20,
84-
pageCount: 3,
85-
};
86-
87-
component.onPageChange(paginatorEvent);
88-
89-
expect(component.currentPage()).toBe(1);
90-
expect(component.first()).toBe(0);
91-
expect(routerMock.navigate).toHaveBeenCalledWith([], {
92-
relativeTo: expect.any(Object),
93-
queryParams: {
94-
page: '1',
95-
size: '20',
96-
},
97-
queryParamsHandling: 'merge',
98-
});
72+
it('should handle search control value changes', () => {
73+
const searchValue = 'test search';
74+
component.searchControl.setValue(searchValue);
75+
76+
expect(component.searchControl.value).toBe(searchValue);
9977
});
10078

101-
it('should handle first being undefined', () => {
102-
const paginatorEvent: PaginatorState = {
103-
page: 2,
104-
first: undefined,
105-
rows: 15,
106-
pageCount: 4,
107-
};
108-
109-
component.onPageChange(paginatorEvent);
110-
111-
expect(component.currentPage()).toBe(2);
112-
expect(component.first()).toBe(0);
113-
expect(routerMock.navigate).toHaveBeenCalledWith([], {
114-
relativeTo: expect.any(Object),
115-
queryParams: {
116-
page: '2',
117-
size: '15',
118-
},
119-
queryParamsHandling: 'merge',
120-
});
79+
it('should handle empty search', () => {
80+
component.searchControl.setValue('');
81+
82+
expect(component.searchControl.value).toBe('');
83+
});
84+
85+
it('should handle null search value', () => {
86+
component.searchControl.setValue(null);
87+
88+
expect(component.searchControl.value).toBe(null);
12189
});
12290
});

src/app/features/preprints/components/advisory-board/advisory-board.component.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ describe('AdvisoryBoardComponent', () => {
66
let component: AdvisoryBoardComponent;
77
let fixture: ComponentFixture<AdvisoryBoardComponent>;
88

9+
const mockHtmlContent =
10+
'<div class="advisory-content"><h2>Advisory Board</h2><p>This is advisory board content.</p></div>';
11+
912
beforeEach(async () => {
1013
await TestBed.configureTestingModule({
1114
imports: [AdvisoryBoardComponent],
@@ -19,4 +22,71 @@ describe('AdvisoryBoardComponent', () => {
1922
it('should create', () => {
2023
expect(component).toBeTruthy();
2124
});
25+
26+
it('should have default input values', () => {
27+
expect(component.htmlContent()).toBeNull();
28+
expect(component.brand()).toBeUndefined();
29+
expect(component.isLandingPage()).toBe(false);
30+
});
31+
32+
it('should not render section when htmlContent is null', () => {
33+
fixture.detectChanges();
34+
35+
const compiled = fixture.nativeElement;
36+
const section = compiled.querySelector('section');
37+
38+
expect(section).toBeNull();
39+
});
40+
41+
it('should not render section when htmlContent is undefined', () => {
42+
fixture.componentRef.setInput('htmlContent', undefined);
43+
fixture.detectChanges();
44+
45+
const compiled = fixture.nativeElement;
46+
const section = compiled.querySelector('section');
47+
48+
expect(section).toBeNull();
49+
});
50+
51+
it('should render section when htmlContent is provided', () => {
52+
fixture.componentRef.setInput('htmlContent', mockHtmlContent);
53+
fixture.detectChanges();
54+
55+
const compiled = fixture.nativeElement;
56+
const section = compiled.querySelector('section');
57+
58+
expect(section).toBeTruthy();
59+
expect(section.innerHTML).toBe(mockHtmlContent);
60+
});
61+
62+
it('should apply correct CSS classes when isLandingPage is false', () => {
63+
fixture.componentRef.setInput('htmlContent', mockHtmlContent);
64+
fixture.componentRef.setInput('isLandingPage', false);
65+
fixture.detectChanges();
66+
67+
const compiled = fixture.nativeElement;
68+
const section = compiled.querySelector('section');
69+
70+
expect(section).toBeTruthy();
71+
expect(section.classList.contains('osf-preprint-service')).toBe(false);
72+
expect(section.classList.contains('preprints-advisory-board-section')).toBe(true);
73+
expect(section.classList.contains('pt-3')).toBe(true);
74+
expect(section.classList.contains('pb-5')).toBe(true);
75+
expect(section.classList.contains('px-3')).toBe(true);
76+
expect(section.classList.contains('flex')).toBe(true);
77+
expect(section.classList.contains('flex-column')).toBe(true);
78+
});
79+
80+
it('should apply correct CSS classes when isLandingPage is true', () => {
81+
fixture.componentRef.setInput('htmlContent', mockHtmlContent);
82+
fixture.componentRef.setInput('isLandingPage', true);
83+
fixture.detectChanges();
84+
85+
const compiled = fixture.nativeElement;
86+
const section = compiled.querySelector('section');
87+
88+
expect(section).toBeTruthy();
89+
expect(section.classList.contains('osf-preprint-service')).toBe(true);
90+
expect(section.classList.contains('preprints-advisory-board-section')).toBe(true);
91+
});
2292
});
Lines changed: 118 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,143 @@
1-
import { TranslatePipe } from '@ngx-translate/core';
2-
import { MockPipe } from 'ng-mocks';
3-
41
import { ComponentFixture, TestBed } from '@angular/core/testing';
5-
import { provideRouter } from '@angular/router';
62

7-
import { TranslateServiceMock } from '@shared/mocks';
3+
import { ResourceType } from '@shared/enums';
4+
import { SubjectModel } from '@shared/models';
85

96
import { BrowseBySubjectsComponent } from './browse-by-subjects.component';
107

8+
import { SUBJECTS_MOCK } from '@testing/mocks/subject.mock';
9+
import { OSFTestingModule } from '@testing/osf.testing.module';
10+
1111
describe('BrowseBySubjectsComponent', () => {
1212
let component: BrowseBySubjectsComponent;
1313
let fixture: ComponentFixture<BrowseBySubjectsComponent>;
1414

15+
const mockSubjects: SubjectModel[] = SUBJECTS_MOCK;
16+
1517
beforeEach(async () => {
1618
await TestBed.configureTestingModule({
17-
imports: [BrowseBySubjectsComponent, MockPipe(TranslatePipe)],
18-
providers: [provideRouter([]), TranslateServiceMock],
19+
imports: [BrowseBySubjectsComponent, OSFTestingModule],
1920
}).compileComponents();
2021

2122
fixture = TestBed.createComponent(BrowseBySubjectsComponent);
2223
component = fixture.componentInstance;
24+
});
2325

26+
it('should create', () => {
2427
fixture.componentRef.setInput('subjects', []);
2528
fixture.componentRef.setInput('areSubjectsLoading', false);
2629
fixture.componentRef.setInput('isProviderLoading', false);
2730

2831
fixture.detectChanges();
32+
expect(component).toBeTruthy();
2933
});
3034

31-
it('should create', () => {
32-
expect(component).toBeTruthy();
35+
it('should have default input values', () => {
36+
fixture.componentRef.setInput('subjects', []);
37+
fixture.componentRef.setInput('areSubjectsLoading', false);
38+
fixture.componentRef.setInput('isProviderLoading', false);
39+
fixture.detectChanges();
40+
41+
expect(component.subjects()).toEqual([]);
42+
expect(component.areSubjectsLoading()).toBe(false);
43+
expect(component.isProviderLoading()).toBe(false);
44+
expect(component.isLandingPage()).toBe(false);
45+
});
46+
47+
it('should display title', () => {
48+
fixture.componentRef.setInput('subjects', []);
49+
fixture.componentRef.setInput('areSubjectsLoading', false);
50+
fixture.componentRef.setInput('isProviderLoading', false);
51+
fixture.detectChanges();
52+
53+
const compiled = fixture.nativeElement;
54+
const title = compiled.querySelector('h2');
55+
56+
expect(title).toBeTruthy();
57+
expect(title.textContent).toBe('preprints.browseBySubjects.title');
58+
});
59+
60+
it('should display correct subject names in buttons', () => {
61+
fixture.componentRef.setInput('subjects', mockSubjects);
62+
fixture.componentRef.setInput('areSubjectsLoading', false);
63+
fixture.componentRef.setInput('isProviderLoading', false);
64+
fixture.detectChanges();
65+
66+
const compiled = fixture.nativeElement;
67+
const buttons = compiled.querySelectorAll('p-button');
68+
69+
expect(buttons[0].getAttribute('ng-reflect-label')).toBe('Mathematics');
70+
expect(buttons[1].getAttribute('ng-reflect-label')).toBe('Physics');
71+
});
72+
73+
it('should compute linksToSearchPageForSubject correctly', () => {
74+
fixture.componentRef.setInput('subjects', mockSubjects);
75+
fixture.componentRef.setInput('areSubjectsLoading', false);
76+
fixture.componentRef.setInput('isProviderLoading', false);
77+
fixture.detectChanges();
78+
79+
const links = component.linksToSearchPageForSubject();
80+
81+
expect(links).toHaveLength(2);
82+
expect(links[0]).toEqual({
83+
tab: ResourceType.Preprint,
84+
filter_subject: 'https://example.com/subjects/mathematics',
85+
});
86+
expect(links[1]).toEqual({
87+
tab: ResourceType.Preprint,
88+
filter_subject: 'https://example.com/subjects/physics',
89+
});
90+
});
91+
92+
it('should set correct routerLink for non-landing page', () => {
93+
fixture.componentRef.setInput('subjects', mockSubjects);
94+
fixture.componentRef.setInput('areSubjectsLoading', false);
95+
fixture.componentRef.setInput('isProviderLoading', false);
96+
fixture.componentRef.setInput('isLandingPage', false);
97+
fixture.detectChanges();
98+
99+
const compiled = fixture.nativeElement;
100+
const buttons = compiled.querySelectorAll('p-button');
101+
102+
expect(buttons[0].getAttribute('ng-reflect-router-link')).toBe('discover');
103+
});
104+
105+
it('should set correct routerLink for landing page', () => {
106+
fixture.componentRef.setInput('subjects', mockSubjects);
107+
fixture.componentRef.setInput('areSubjectsLoading', false);
108+
fixture.componentRef.setInput('isProviderLoading', false);
109+
fixture.componentRef.setInput('isLandingPage', true);
110+
fixture.detectChanges();
111+
112+
const compiled = fixture.nativeElement;
113+
const buttons = compiled.querySelectorAll('p-button');
114+
115+
expect(buttons[0].getAttribute('ng-reflect-router-link')).toBe('/search');
116+
});
117+
118+
it('should handle subjects without iri', () => {
119+
const subjectsWithoutIri: SubjectModel[] = [
120+
{
121+
id: 'subject-1',
122+
name: 'Physics',
123+
iri: undefined,
124+
children: [],
125+
parent: null,
126+
expanded: false,
127+
},
128+
];
129+
130+
fixture.componentRef.setInput('subjects', subjectsWithoutIri);
131+
fixture.componentRef.setInput('areSubjectsLoading', false);
132+
fixture.componentRef.setInput('isProviderLoading', false);
133+
fixture.detectChanges();
134+
135+
const links = component.linksToSearchPageForSubject();
136+
137+
expect(links).toHaveLength(1);
138+
expect(links[0]).toEqual({
139+
tab: ResourceType.Preprint,
140+
filter_subject: undefined,
141+
});
33142
});
34143
});

0 commit comments

Comments
 (0)