|
| 1 | +import { MockProvider } from 'ng-mocks'; |
| 2 | + |
1 | 3 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 4 | +import { ActivatedRoute } from '@angular/router'; |
2 | 5 |
|
3 | 6 | import { ResourceType } from '@shared/enums/resource-type.enum'; |
4 | 7 | import { SubjectModel } from '@shared/models/subject/subject.model'; |
5 | 8 |
|
6 | 9 | import { BrowseBySubjectsComponent } from './browse-by-subjects.component'; |
7 | 10 |
|
8 | 11 | import { SUBJECTS_MOCK } from '@testing/mocks/subject.mock'; |
9 | | -import { OSFTestingModule } from '@testing/osf.testing.module'; |
| 12 | +import { provideOSFCore } from '@testing/osf.testing.provider'; |
| 13 | +import { ActivatedRouteMockBuilder } from '@testing/providers/route-provider.mock'; |
10 | 14 |
|
11 | 15 | describe('BrowseBySubjectsComponent', () => { |
12 | 16 | let component: BrowseBySubjectsComponent; |
13 | 17 | let fixture: ComponentFixture<BrowseBySubjectsComponent>; |
14 | 18 |
|
15 | 19 | const mockSubjects: SubjectModel[] = SUBJECTS_MOCK; |
16 | 20 |
|
17 | | - beforeEach(async () => { |
18 | | - await TestBed.configureTestingModule({ |
19 | | - imports: [BrowseBySubjectsComponent, OSFTestingModule], |
20 | | - }).compileComponents(); |
| 21 | + function setup(overrides?: { |
| 22 | + subjects?: SubjectModel[]; |
| 23 | + areSubjectsLoading?: boolean; |
| 24 | + isProviderLoading?: boolean; |
| 25 | + isLandingPage?: boolean; |
| 26 | + }) { |
| 27 | + TestBed.configureTestingModule({ |
| 28 | + imports: [BrowseBySubjectsComponent], |
| 29 | + providers: [provideOSFCore(), MockProvider(ActivatedRoute, ActivatedRouteMockBuilder.create().build())], |
| 30 | + }); |
21 | 31 |
|
22 | 32 | fixture = TestBed.createComponent(BrowseBySubjectsComponent); |
23 | 33 | component = fixture.componentInstance; |
24 | | - }); |
25 | | - |
26 | | - it('should create', () => { |
27 | | - fixture.componentRef.setInput('subjects', []); |
28 | | - fixture.componentRef.setInput('areSubjectsLoading', false); |
29 | | - fixture.componentRef.setInput('isProviderLoading', false); |
30 | | - |
| 34 | + fixture.componentRef.setInput('subjects', overrides?.subjects ?? []); |
| 35 | + fixture.componentRef.setInput('areSubjectsLoading', overrides?.areSubjectsLoading ?? false); |
| 36 | + fixture.componentRef.setInput('isProviderLoading', overrides?.isProviderLoading ?? false); |
| 37 | + fixture.componentRef.setInput('isLandingPage', overrides?.isLandingPage ?? false); |
31 | 38 | fixture.detectChanges(); |
32 | | - expect(component).toBeTruthy(); |
33 | | - }); |
| 39 | + } |
34 | 40 |
|
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(); |
| 41 | + it('should keep default isLandingPage input as false', () => { |
| 42 | + setup(); |
40 | 43 |
|
41 | | - expect(component.subjects()).toEqual([]); |
42 | | - expect(component.areSubjectsLoading()).toBe(false); |
43 | | - expect(component.isProviderLoading()).toBe(false); |
44 | 44 | expect(component.isLandingPage()).toBe(false); |
45 | 45 | }); |
46 | 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'); |
| 47 | + it('should render skeleton rows while loading', () => { |
| 48 | + setup({ areSubjectsLoading: true, subjects: mockSubjects }); |
55 | 49 |
|
56 | | - expect(title).toBeTruthy(); |
57 | | - expect(title.textContent).toBe('preprints.browseBySubjects.title'); |
| 50 | + expect(fixture.nativeElement.querySelectorAll('p-skeleton').length).toBe(6); |
| 51 | + expect(fixture.nativeElement.querySelectorAll('p-button').length).toBe(0); |
58 | 52 | }); |
59 | 53 |
|
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'); |
| 54 | + it('should render one button per subject when not loading', () => { |
| 55 | + setup({ subjects: mockSubjects }); |
68 | 56 |
|
69 | | - expect(buttons[0].getAttribute('ng-reflect-label')).toBe('Mathematics'); |
70 | | - expect(buttons[1].getAttribute('ng-reflect-label')).toBe('Physics'); |
| 57 | + expect(fixture.nativeElement.querySelectorAll('p-button').length).toBe(mockSubjects.length); |
71 | 58 | }); |
72 | 59 |
|
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(); |
| 60 | + it('should build query params for subject with iri', () => { |
| 61 | + setup({ subjects: mockSubjects }); |
80 | 62 |
|
81 | | - expect(links).toHaveLength(2); |
82 | | - expect(links[0]).toEqual({ |
| 63 | + expect(component.getQueryParamsForSubject(mockSubjects[0])).toEqual({ |
83 | 64 | tab: ResourceType.Preprint, |
84 | 65 | filter_subject: '[{"label":"Mathematics","value":"https://example.com/subjects/mathematics"}]', |
85 | 66 | }); |
86 | | - expect(links[1]).toEqual({ |
87 | | - tab: ResourceType.Preprint, |
88 | | - filter_subject: '[{"label":"Physics","value":"https://example.com/subjects/physics"}]', |
89 | | - }); |
90 | 67 | }); |
91 | 68 |
|
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({ |
| 69 | + it('should build query params for subject without iri', () => { |
| 70 | + setup(); |
| 71 | + const subjectWithoutIri = { |
| 72 | + id: 'subject-1', |
| 73 | + name: 'Physics', |
| 74 | + iri: undefined, |
| 75 | + children: [], |
| 76 | + parent: null, |
| 77 | + expanded: false, |
| 78 | + } as SubjectModel; |
| 79 | + |
| 80 | + expect(component.getQueryParamsForSubject(subjectWithoutIri)).toEqual({ |
139 | 81 | tab: ResourceType.Preprint, |
140 | 82 | filter_subject: '[{"label":"Physics"}]', |
141 | 83 | }); |
142 | 84 | }); |
| 85 | + |
| 86 | + it.each([ |
| 87 | + { isLandingPage: false, expected: 'discover' }, |
| 88 | + { isLandingPage: true, expected: '/search' }, |
| 89 | + ])('should resolve route for isLandingPage=$isLandingPage', ({ isLandingPage, expected }) => { |
| 90 | + setup({ isLandingPage }); |
| 91 | + |
| 92 | + expect(component.subjectRoute()).toBe(expected); |
| 93 | + }); |
143 | 94 | }); |
0 commit comments