|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { FormBuilderService } from '../../../shared/form/builder/form-builder.service'; |
| 3 | +import { AdvancedSearchComponent } from './advanced-search.component'; |
| 4 | +import { getMockFormBuilderService } from '../../../shared/mocks/form-builder-service.mock'; |
| 5 | +import { SearchService } from '../../../core/shared/search/search.service'; |
| 6 | +import { SEARCH_CONFIG_SERVICE } from '../../../my-dspace-page/my-dspace-page.component'; |
| 7 | +import { SearchConfigurationServiceStub } from '../../testing/search-configuration-service.stub'; |
| 8 | +import { RemoteDataBuildService } from '../../../core/cache/builders/remote-data-build.service'; |
| 9 | +import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms'; |
| 10 | +import { APP_CONFIG } from '../../../../config/app-config.interface'; |
| 11 | +import { environment } from '../../../../environments/environment'; |
| 12 | +import { RouterStub } from '../../testing/router.stub'; |
| 13 | +import { Router } from '@angular/router'; |
| 14 | +import { ChangeDetectionStrategy, NO_ERRORS_SCHEMA } from '@angular/core'; |
| 15 | +import { BrowserOnlyMockPipe } from '../../testing/browser-only-mock.pipe'; |
| 16 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 17 | +import { TranslateModule } from '@ngx-translate/core'; |
| 18 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 19 | +describe('AdvancedSearchComponent', () => { |
| 20 | + let component: AdvancedSearchComponent; |
| 21 | + let fixture: ComponentFixture<AdvancedSearchComponent>; |
| 22 | + let builderService: FormBuilderService = getMockFormBuilderService(); |
| 23 | + let searchService: SearchService; |
| 24 | + let router; |
| 25 | + const searchServiceStub = { |
| 26 | + /* eslint-disable no-empty,@typescript-eslint/no-empty-function */ |
| 27 | + getClearFiltersQueryParams: () => { |
| 28 | + }, |
| 29 | + getSearchLink: () => { |
| 30 | + }, |
| 31 | + getConfigurationSearchConfig: () => { }, |
| 32 | + /* eslint-enable no-empty, @typescript-eslint/no-empty-function */ |
| 33 | + }; |
| 34 | + beforeEach(async () => { |
| 35 | + await TestBed.configureTestingModule({ |
| 36 | + declarations: [AdvancedSearchComponent, BrowserOnlyMockPipe], |
| 37 | + imports: [FormsModule, RouterTestingModule, TranslateModule.forRoot(), BrowserAnimationsModule, ReactiveFormsModule], |
| 38 | + providers: [ |
| 39 | + FormBuilder, |
| 40 | + { provide: APP_CONFIG, useValue: environment }, |
| 41 | + { provide: FormBuilderService, useValue: builderService }, |
| 42 | + { provide: Router, useValue: new RouterStub() }, |
| 43 | + { provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() }, |
| 44 | + { provide: RemoteDataBuildService, useValue: {} }, |
| 45 | + { provide: SearchService, useValue: searchServiceStub }, |
| 46 | + ], |
| 47 | + schemas: [NO_ERRORS_SCHEMA] |
| 48 | + }).overrideComponent(AdvancedSearchComponent, { |
| 49 | + set: { changeDetection: ChangeDetectionStrategy.Default } |
| 50 | + }).compileComponents(); |
| 51 | + }); |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + fixture = TestBed.createComponent(AdvancedSearchComponent); |
| 55 | + component = fixture.componentInstance; |
| 56 | + router = TestBed.inject(Router); |
| 57 | + fixture.detectChanges(); |
| 58 | + }); |
| 59 | + describe('when the getSearchLink method is called', () => { |
| 60 | + const data = { filter: 'title', textsearch: 'demo', operator: 'equals' }; |
| 61 | + it('should call navigate on the router with the right searchlink and parameters when the filter is provided with a valid operator', () => { |
| 62 | + component.advSearchForm.get('textsearch').patchValue('1'); |
| 63 | + component.advSearchForm.get('filter').patchValue('1'); |
| 64 | + component.advSearchForm.get('operator').patchValue('1'); |
| 65 | + |
| 66 | + component.onSubmit(data); |
| 67 | + expect(router.navigate).toHaveBeenCalledWith([undefined], { |
| 68 | + queryParams: { ['f.' + data.filter]: data.textsearch + ',' + data.operator }, |
| 69 | + queryParamsHandling: 'merge' |
| 70 | + }); |
| 71 | + |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments