From fd11ab37519b7129502a1f02ff8ed290d4d9f383 Mon Sep 17 00:00:00 2001 From: Yura Bondarenko Date: Mon, 15 Feb 2021 12:39:49 +0100 Subject: [PATCH] 76922: Add unit tests for ConfigurationSearchPageComponent --- ...onfiguration-search-page.component.spec.ts | 53 +++++++++++++++++-- src/app/+search-page/search.component.spec.ts | 4 +- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/app/+search-page/configuration-search-page.component.spec.ts b/src/app/+search-page/configuration-search-page.component.spec.ts index f49d329edd..8ce4154c66 100644 --- a/src/app/+search-page/configuration-search-page.component.spec.ts +++ b/src/app/+search-page/configuration-search-page.component.spec.ts @@ -2,20 +2,63 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { configureSearchComponentTestingModule } from './search.component.spec'; import { ConfigurationSearchPageComponent } from './configuration-search-page.component'; import { SearchConfigurationService } from '../core/shared/search/search-configuration.service'; +import { Component, ViewChild } from '@angular/core'; +import { Router } from '@angular/router'; +import { RouteService } from '../core/services/route.service'; +import createSpy = jasmine.createSpy; + +const CONFIGURATION = 'test-configuration'; +const QUERY = 'test query'; + +@Component({ + template: ` + + + `, +}) +class HostComponent { + @ViewChild('configurationSearchPage') configurationSearchPage: ConfigurationSearchPageComponent; +} describe('ConfigurationSearchPageComponent', () => { let comp: ConfigurationSearchPageComponent; - let fixture: ComponentFixture; + let fixture: ComponentFixture; let searchConfigService: SearchConfigurationService; + let routeService: RouteService; beforeEach(waitForAsync(() => { - configureSearchComponentTestingModule(ConfigurationSearchPageComponent); + configureSearchComponentTestingModule(ConfigurationSearchPageComponent, [HostComponent]); })); beforeEach(() => { - fixture = TestBed.createComponent(ConfigurationSearchPageComponent); - comp = fixture.componentInstance; - searchConfigService = (comp as any).searchConfigService; + fixture = TestBed.createComponent(HostComponent); + + // Set router url to a dummy value for SearchComponent#ngOnInit + spyOnProperty(TestBed.inject(Router), 'url', 'get').and.returnValue('some/url/here'); + + routeService = TestBed.inject(RouteService); + routeService.setParameter = createSpy('setParameter'); + fixture.detectChanges(); + + comp = fixture.componentInstance.configurationSearchPage; + searchConfigService = (comp as any).searchConfigService; + }); + + it('should set route parameters on init', () => { + expect(comp.configuration).toBe(CONFIGURATION); + expect(comp.fixedFilterQuery).toBe(QUERY); + + expect(routeService.setParameter).toHaveBeenCalledWith('configuration', CONFIGURATION); + expect(routeService.setParameter).toHaveBeenCalledWith('fixedFilterQuery', QUERY); + }); + + it('should reset route parameters on destroy', () => { + fixture.destroy(); + + expect(routeService.setParameter).toHaveBeenCalledWith('configuration', undefined); + expect(routeService.setParameter).toHaveBeenCalledWith('fixedFilterQuery', undefined); }); }); diff --git a/src/app/+search-page/search.component.spec.ts b/src/app/+search-page/search.component.spec.ts index 2ad497aa49..989aed403d 100644 --- a/src/app/+search-page/search.component.spec.ts +++ b/src/app/+search-page/search.component.spec.ts @@ -84,10 +84,10 @@ const routeServiceStub = { } }; -export function configureSearchComponentTestingModule(compType) { +export function configureSearchComponentTestingModule(compType, additionalDeclarations: any[] = []) { TestBed.configureTestingModule({ imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), NoopAnimationsModule, NgbCollapseModule], - declarations: [compType], + declarations: [compType, ...additionalDeclarations], providers: [ { provide: SearchService, useValue: searchServiceStub }, {