|
1 | 1 | import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
2 | | -import { NoopAnimationsModule } from '@angular/platform-browser/animations'; |
3 | 2 | import { TranslateModule } from '@ngx-translate/core'; |
4 | | -import { ChangeDetectionStrategy, NO_ERRORS_SCHEMA } from '@angular/core'; |
5 | | -import { FormsModule } from '@angular/forms'; |
6 | | -import { Observable, of as observableOf } from 'rxjs'; |
7 | | -import { Params, Router } from '@angular/router'; |
| 3 | +import { Params, ActivatedRoute } from '@angular/router'; |
8 | 4 | import { SearchLabelComponent } from './search-label.component'; |
9 | | -import { ObjectKeysPipe } from '../../../utils/object-keys-pipe'; |
10 | | -import { SEARCH_CONFIG_SERVICE } from '../../../../my-dspace-page/my-dspace-page.component'; |
11 | 5 | import { SearchServiceStub } from '../../../testing/search-service.stub'; |
12 | | -import { SearchConfigurationServiceStub } from '../../../testing/search-configuration-service.stub'; |
13 | 6 | import { SearchService } from '../../../../core/shared/search/search.service'; |
14 | | -import { PaginationComponentOptions } from '../../../pagination/pagination-component-options.model'; |
15 | | -import { SortDirection, SortOptions } from '../../../../core/cache/models/sort-options.model'; |
16 | | -import { PaginationService } from '../../../../core/pagination/pagination.service'; |
| 7 | +import { ActivatedRouteStub } from '../../../testing/active-router.stub'; |
| 8 | +import { AppliedFilter } from '../../models/applied-filter.model'; |
| 9 | +import { addOperatorToFilterValue } from '../../search.utils'; |
| 10 | +import { RouterTestingModule } from '@angular/router/testing'; |
17 | 11 | import { SearchConfigurationService } from '../../../../core/shared/search/search-configuration.service'; |
18 | | -import { PaginationServiceStub } from '../../../testing/pagination-service.stub'; |
19 | | -import { FindListOptions } from '../../../../core/data/find-list-options.model'; |
| 12 | +import { SearchConfigurationServiceStub } from '../../../testing/search-configuration-service.stub'; |
20 | 13 |
|
21 | 14 | describe('SearchLabelComponent', () => { |
22 | 15 | let comp: SearchLabelComponent; |
23 | 16 | let fixture: ComponentFixture<SearchLabelComponent>; |
24 | 17 |
|
| 18 | + let route: ActivatedRouteStub; |
| 19 | + let searchConfigurationService: SearchConfigurationServiceStub; |
| 20 | + |
25 | 21 | const searchLink = '/search'; |
26 | | - let searchService; |
| 22 | + let appliedFilter: AppliedFilter; |
| 23 | + let initialRouteParams: Params; |
27 | 24 |
|
28 | | - const key1 = 'author'; |
29 | | - const key2 = 'subject'; |
30 | | - const value1 = 'Test, Author'; |
31 | | - const normValue1 = 'Test, Author'; |
32 | | - const value2 = 'TestSubject'; |
33 | | - const value3 = 'Test, Authority,authority'; |
34 | | - const normValue3 = 'Test, Authority'; |
35 | | - const filter1 = [key1, value1]; |
36 | | - const filter2 = [key2, value2]; |
37 | | - const mockFilters = [ |
38 | | - filter1, |
39 | | - filter2 |
40 | | - ]; |
| 25 | + function init(): void { |
| 26 | + appliedFilter = Object.assign(new AppliedFilter(), { |
| 27 | + filter: 'author', |
| 28 | + operator: 'authority', |
| 29 | + value: '1282121b-5394-4689-ab93-78d537764052', |
| 30 | + label: 'Odinson, Thor', |
| 31 | + }); |
| 32 | + initialRouteParams = { |
| 33 | + 'query': '', |
| 34 | + 'spc.page': '1', |
| 35 | + 'f.author': addOperatorToFilterValue(appliedFilter.value, appliedFilter.operator), |
| 36 | + 'f.has_content_in_original_bundle': addOperatorToFilterValue('true', 'equals'), |
| 37 | + }; |
| 38 | + } |
41 | 39 |
|
42 | | - const pagination = Object.assign(new PaginationComponentOptions(), { id: 'page-id', currentPage: 1, pageSize: 20 }); |
43 | | - const paginationService = new PaginationServiceStub(pagination); |
| 40 | + beforeEach(waitForAsync(async () => { |
| 41 | + init(); |
| 42 | + route = new ActivatedRouteStub(initialRouteParams); |
| 43 | + searchConfigurationService = new SearchConfigurationServiceStub(); |
44 | 44 |
|
45 | | - beforeEach(waitForAsync(() => { |
46 | | - TestBed.configureTestingModule({ |
47 | | - imports: [TranslateModule.forRoot(), NoopAnimationsModule, FormsModule], |
48 | | - declarations: [SearchLabelComponent, ObjectKeysPipe], |
| 45 | + await TestBed.configureTestingModule({ |
| 46 | + imports: [ |
| 47 | + RouterTestingModule, |
| 48 | + TranslateModule.forRoot(), |
| 49 | + ], |
| 50 | + declarations: [ |
| 51 | + SearchLabelComponent, |
| 52 | + ], |
49 | 53 | providers: [ |
| 54 | + { provide: SearchConfigurationService, useValue: searchConfigurationService }, |
50 | 55 | { provide: SearchService, useValue: new SearchServiceStub(searchLink) }, |
51 | | - { provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() }, |
52 | | - { provide: SearchConfigurationService, useValue: new SearchConfigurationServiceStub() }, |
53 | | - { provide: PaginationService, useValue: paginationService }, |
54 | | - { provide: Router, useValue: {} } |
55 | | - // { provide: SearchConfigurationService, useValue: {getCurrentFrontendFilters : () => observableOf({})} } |
| 56 | + { provide: ActivatedRoute, useValue: route }, |
56 | 57 | ], |
57 | | - schemas: [NO_ERRORS_SCHEMA] |
58 | | - }).overrideComponent(SearchLabelComponent, { |
59 | | - set: { changeDetection: ChangeDetectionStrategy.Default } |
60 | 58 | }).compileComponents(); |
61 | 59 | })); |
62 | 60 |
|
63 | 61 | beforeEach(() => { |
64 | 62 | fixture = TestBed.createComponent(SearchLabelComponent); |
65 | 63 | comp = fixture.componentInstance; |
66 | | - searchService = (comp as any).searchService; |
67 | | - comp.key = key1; |
68 | | - comp.value = value1; |
69 | | - (comp as any).appliedFilters = observableOf(mockFilters); |
| 64 | + comp.appliedFilter = appliedFilter; |
70 | 65 | fixture.detectChanges(); |
71 | 66 | }); |
72 | 67 |
|
73 | | - describe('when getRemoveParams is called', () => { |
74 | | - let obs: Observable<Params>; |
75 | | - |
76 | | - beforeEach(() => { |
77 | | - obs = comp.getRemoveParams(); |
78 | | - }); |
79 | | - |
80 | | - it('should return all params but the provided filter', () => { |
81 | | - obs.subscribe((params) => { |
82 | | - // Should contain only filter2 and page: length == 2 |
83 | | - expect(Object.keys(params).length).toBe(2); |
84 | | - }); |
85 | | - }); |
86 | | - }); |
87 | | - |
88 | | - describe('when normalizeFilterValue is called', () => { |
89 | | - it('should return properly filter value', () => { |
90 | | - let result: string; |
91 | | - |
92 | | - result = comp.normalizeFilterValue(value1); |
93 | | - expect(result).toBe(normValue1); |
94 | | - |
95 | | - result = comp.normalizeFilterValue(value3); |
96 | | - expect(result).toBe(normValue3); |
97 | | - }); |
| 68 | + it('should create', () => { |
| 69 | + expect(comp).toBeTruthy(); |
98 | 70 | }); |
99 | 71 | }); |
0 commit comments