| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| import { GridFailureDetailsSandbox } from '@grid-failure-information-app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox'; |
| import { GridFailure } from '@grid-failure-information-app/shared/models'; |
| import { State } from '@grid-failure-information-app/shared/store'; |
| import * as gridFailureActions from '@grid-failure-information-app/shared/store/actions/grid-failures.action'; |
| import { UtilService } from '@grid-failure-information-app/shared/utility/utility.service'; |
| import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; |
| import { ActionsSubject, Store } from '@ngrx/store'; |
| import { of } from 'rxjs'; |
| import { Router } from '@angular/router'; |
| import { StateEnum } from '@grid-failure-information-app/shared/constants/enums'; |
| import { FailureStation } from '@grid-failure-information-app/shared/models'; |
| import { FailureHousenumber } from '@grid-failure-information-app/shared/models'; |
| |
| describe('GridFailureDetailsSandbox', () => { |
| let service: GridFailureDetailsSandbox; |
| let appState: Store<State>; |
| let actionSubject: ActionsSubject; |
| let utilService: UtilService; |
| let router: Router; |
| let modalService: NgbModal; |
| let dispatchSpy: any; |
| |
| beforeEach(() => { |
| appState = { dispatch: () => {}, pipe: () => of(true), select: () => of(true), userDefinedProperties: { controlId: '' } } as any; |
| actionSubject = { pipe: () => of(true), map: () => ({}) } as any; |
| utilService = { displayNotification() {} } as any; |
| router = { navigateByUrl() {} } as any; |
| modalService = { open() {} } as any; |
| dispatchSpy = spyOn(appState, 'dispatch').and.callFake(() => {}); |
| |
| service = new GridFailureDetailsSandbox(appState, actionSubject, router, utilService, modalService); |
| const gridFailureDetailsFormState = { value: { lontitude: 1.24, latitude: 1.24 } }; |
| service.gridFailureDetailsFormState$ = of(gridFailureDetailsFormState as any); |
| }); |
| |
| it('should create GridFailureDetailsSandbox service', () => { |
| expect(service).toBeTruthy(); |
| }); |
| |
| it('should dispatch loadGridFailure Action via loadGridFailure(id)', () => { |
| service.loadGridFailure('id'); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadGridFailureDetail({ payload: 'id' })); |
| }); |
| |
| it('should dispatch loadGridFailureBranches Action via loadGridFailureBranches()', () => { |
| service.loadGridFailureBranches(); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadGridFailureBranches()); |
| }); |
| |
| it('should dispatch loadGridFailureClassifications Action via loadGridFailureClassifications()', () => { |
| service.loadGridFailureClassifications(); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadGridFailureClassifications()); |
| }); |
| |
| it('should dispatch loadGridFailureTypes Action via loadGridFailureTypes()', () => { |
| service.loadGridFailureTypes(); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadGridFailureTypes()); |
| }); |
| |
| it('should dispatch loadGridFailureStates Action via loadGridFailureStates()', () => { |
| service.loadGridFailureStates(); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadGridFailureStates()); |
| }); |
| |
| it('should dispatch loadGridFailureRadii Action via loadGridFailureRadii()', () => { |
| service.loadGridFailureRadii(); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadGridFailureRadii()); |
| }); |
| |
| it('should dispatch loadGridFailureExpectedReasons Action via loadGridFailureExpectedReasons()', () => { |
| service.loadGridFailureExpectedReasons(); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadGridFailureExpectedReasons()); |
| }); |
| |
| it('should dispatch loadGridFailureVersions Action via loadGridFailureVersions(id)', () => { |
| service.loadGridFailureVersions('id'); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadGridFailureVersions({ payload: 'id' })); |
| }); |
| |
| it('should dispatch loadGridFailureVersion Action via loadGridFailureVersion(VsNo)', () => { |
| service['gridFailureId'] = 'id'; |
| service.loadGridFailureVersion(1); |
| expect(appState.dispatch).toHaveBeenCalledTimes(2); |
| }); |
| |
| it('should dispatch loadGridFailureVersion Action via loadGridFailureVersion(VsNo)', () => { |
| service['gridFailureId'] = 'id'; |
| service.maxVersionNumber = 2; |
| service.loadGridFailureVersion(1); |
| expect(appState.dispatch).toHaveBeenCalledTimes(2); |
| }); |
| |
| it('should clear form state when current change is canceled and form state is pristine', () => { |
| const spy: any = spyOn(service as any, '_clear').and.callThrough(); |
| service.currentFormState = { isPristine: true } as any; |
| service.cancel(); |
| expect(spy).toHaveBeenCalled(); |
| }); |
| |
| it('should open modal when current change is canceled and form state is not pristine', () => { |
| spyOn(service['_modalService'], 'open').and.returnValue({ componentInstance: { title: '' }, result: { then: () => of(true) } } as any); |
| service.currentFormState = { isPristine: false } as any; |
| service.cancel(); |
| expect(modalService.open).toHaveBeenCalled(); |
| }); |
| |
| it('should dispatch actions to clear the form', () => { |
| service['_clear'](); |
| expect(appState.dispatch).toHaveBeenCalledTimes(3); |
| }); |
| |
| it('should call "clearGridFailureData(actionType: string)" and dispatch the right actions to clear one property of the form', () => { |
| let actionType = 'failureBegin'; //case 1 |
| service.clearGridFailureData(actionType); |
| |
| actionType = 'failureEndPlanned'; // case 2 |
| service.clearGridFailureData(actionType); |
| |
| actionType = 'failureEndResupplied'; //case 3 |
| service.clearGridFailureData(actionType); |
| |
| actionType = 'test'; //default (no dispatch) |
| service.clearGridFailureData(actionType); |
| |
| expect(appState.dispatch).toHaveBeenCalledTimes(3); |
| }); |
| |
| it('should call "setGridFailureDateTime(dateTime: string, actionType: string)" and dispatch the right actions to set one property of the form', () => { |
| const datetime = new Date(); |
| |
| let actionType = 'failureBegin'; //case 1 |
| service.setGridFailureDateTime(datetime.toISOString(), actionType); |
| |
| actionType = 'failureEndPlanned'; // case 2 |
| service.setGridFailureDateTime(datetime.toISOString(), actionType); |
| |
| actionType = 'failureEndResupplied'; //case 3 |
| service.setGridFailureDateTime(datetime.toISOString(), actionType); |
| |
| actionType = 'test'; //default (no dispatch) |
| service.setGridFailureDateTime(datetime.toISOString(), actionType); |
| |
| expect(appState.dispatch).toHaveBeenCalledTimes(3); |
| }); |
| |
| it('should call dispatch and clear if save a valid grid failure detail', () => { |
| const spy1 = spyOn(service as any, '_clear'); |
| const gfdetail = new GridFailure(); |
| gfdetail.id = 'id'; |
| gfdetail.responsibility = 'test'; |
| gfdetail.postcode = '00000'; |
| |
| service.currentFormState = { |
| ...service.currentFormState, |
| isValid: true, |
| value: gfdetail, |
| userDefinedProperties: { controlId: '' }, |
| }; |
| service.saveGridFailure(); |
| expect(dispatchSpy).toHaveBeenCalledWith(Object({ payload: gfdetail, type: gridFailureActions.saveGridFailure.type })); |
| expect(spy1).toHaveBeenCalled(); |
| }); |
| |
| it('should display a notification if the grid failure detail is invalid', () => { |
| const spy1 = spyOn(utilService, 'displayNotification'); |
| const gfdetail = new GridFailure(); |
| gfdetail.id = 'id'; |
| gfdetail.responsibility = 'test'; |
| gfdetail.postcode = '00000'; |
| |
| service.currentFormState = { |
| ...service.currentFormState, |
| isValid: false, |
| value: gfdetail, |
| userDefinedProperties: { controlId: '' }, |
| }; |
| service.saveGridFailure(); |
| expect(spy1).toHaveBeenCalled(); |
| }); |
| |
| it('should register events', () => { |
| let gridFailureDetailsFormResponse: any = { |
| value: { |
| failureBegin: 'test1', |
| failureEndPlanned: 'test2', |
| failureEndResupplied: 'test3', |
| postcode: '00000', |
| }, |
| userDefinedProperties: { controlId: '' }, |
| }; |
| service.gridFailureDetailsFormState$ = of(gridFailureDetailsFormResponse); |
| const gridFailure: GridFailure = new GridFailure(); |
| gridFailure.versionNumber = 3; |
| const gridFailureVersions: GridFailure[] = [gridFailure]; |
| spyOn(actionSubject, 'pipe').and.returnValue(of({ payload: gridFailureVersions })); |
| const spy = spyOn(service as any, '_showButtonsByState'); |
| |
| service.registerEvents(); |
| expect(service.currentFormState).toBeDefined(); |
| expect(spy).toHaveBeenCalled(); |
| }); |
| |
| it('should dispatch action in response to controlId = gridFailureDetailsForm', () => { |
| let formState: any = { |
| id: 'gridFailureDetailsForm', |
| value: { |
| statusIntern: StateEnum.CREATED, |
| postcode: '00000', |
| }, |
| userDefinedProperties: { controlId: 'gridFailureDetailsForm' }, |
| controls: { |
| postcode: { id: 'gridFailureDetailsForm.postcode' }, |
| }, |
| }; |
| spyOn(service.gridFailureDetailsFormState$, 'pipe').and.returnValue(of(formState)); |
| service.registerEvents(); |
| expect(appState.dispatch).toHaveBeenCalled(); |
| }); |
| |
| it('should dispatch action in response to controlId = gridFailureDetailsForm.postcode', () => { |
| let formState: any = { |
| value: { |
| statusIntern: StateEnum.CREATED, |
| postcode: '00000', |
| }, |
| userDefinedProperties: { controlId: 'gridFailureDetailsForm.postcode' }, |
| controls: { |
| postcode: { id: 'gridFailureDetailsForm.postcode' }, |
| }, |
| }; |
| spyOn(service.gridFailureDetailsFormState$, 'pipe').and.returnValue(of(formState)); |
| service.registerEvents(); |
| expect(appState.dispatch).toHaveBeenCalled(); |
| }); |
| |
| it('should dispatch action in response to controlId = gridFailureDetailsForm.city', () => { |
| let formState: any = { |
| value: { |
| statusIntern: StateEnum.CREATED, |
| postcode: '00000', |
| city: 'S', |
| }, |
| userDefinedProperties: { controlId: 'gridFailureDetailsForm.city' }, |
| controls: { |
| postcode: { id: 'gridFailureDetailsForm.postcode' }, |
| city: { id: 'gridFailureDetailsForm.city' }, |
| }, |
| }; |
| spyOn(service.gridFailureDetailsFormState$, 'pipe').and.returnValue(of(formState)); |
| service.registerEvents(); |
| expect(appState.dispatch).toHaveBeenCalled(); |
| }); |
| |
| it('should dispatch action in response to controlId = gridFailureDetailsForm.district', () => { |
| let formState: any = { |
| value: { |
| statusIntern: StateEnum.CREATED, |
| postcode: '00000', |
| city: 'X', |
| district: 'Y', |
| }, |
| userDefinedProperties: { controlId: 'gridFailureDetailsForm.district' }, |
| controls: { |
| postcode: { id: 'gridFailureDetailsForm.postcode' }, |
| city: { id: 'gridFailureDetailsForm.city' }, |
| district: { id: 'gridFailureDetailsForm.district' }, |
| }, |
| }; |
| spyOn(service.gridFailureDetailsFormState$, 'pipe').and.returnValue(of(formState)); |
| service.registerEvents(); |
| expect(appState.dispatch).toHaveBeenCalled(); |
| }); |
| |
| it('should dispatch action in response to controlId = gridFailureDetailsForm.street', () => { |
| let formState: any = { |
| value: { |
| statusIntern: StateEnum.CREATED, |
| postcode: '00000', |
| city: 'X', |
| district: 'Y', |
| street: 'Z', |
| }, |
| userDefinedProperties: { controlId: 'gridFailureDetailsForm.street' }, |
| controls: { |
| postcode: { id: 'gridFailureDetailsForm.postcode' }, |
| city: { id: 'gridFailureDetailsForm.city' }, |
| district: { id: 'gridFailureDetailsForm.district' }, |
| street: { id: 'gridFailureDetailsForm.street' }, |
| }, |
| }; |
| spyOn(service.gridFailureDetailsFormState$, 'pipe').and.returnValue(of(formState)); |
| service.registerEvents(); |
| expect(appState.dispatch).toHaveBeenCalled(); |
| }); |
| |
| it('should show qualify button state is applied', () => { |
| const state = StateEnum.CREATED; |
| (service as any)._showButtonsByState(state); |
| expect(service.showQualifyButton).toBeTruthy(); |
| }); |
| |
| it('should show qualify button state is updated', () => { |
| const state = StateEnum.UPDATED; |
| (service as any)._showButtonsByState(state); |
| expect(service.showQualifyButton).toBeTruthy(); |
| }); |
| |
| it('should show storno button state is applied', () => { |
| const state = StateEnum.CREATED; |
| (service as any)._showButtonsByState(state); |
| expect(service.showStornoButton).toBeTruthy(); |
| }); |
| |
| it('should show storno button state is updated', () => { |
| const state = StateEnum.UPDATED; |
| (service as any)._showButtonsByState(state); |
| expect(service.showStornoButton).toBeTruthy(); |
| }); |
| |
| it('should not show storno button or qualify button if other state', () => { |
| const state = StateEnum.COMPLETED; |
| (service as any)._showButtonsByState(state); |
| expect(service.showQualifyButton).toBeFalsy(); |
| expect(service.showStornoButton).toBeFalsy(); |
| }); |
| |
| it('should show create button if state is NEW', () => { |
| const state = StateEnum.NEW; |
| (service as any)._showButtonsByState(state); |
| expect(service.showCreatedButton).toBeTruthy(); |
| }); |
| |
| it('should not show state buttons if state is an empty string', () => { |
| (service as any)._showButtonsByState(''); |
| expect(service.showCreatedButton).toBeFalsy(); |
| expect(service.showStornoButton).toBeFalsy(); |
| expect(service.showQualifyButton).toBeFalsy(); |
| }); |
| |
| it('should set the state in form to qualify and call save fuction', () => { |
| service.gridFailureInternalStates$ = { pipe: () => of({}), map: () => of({}) } as any; |
| spyOn(service.gridFailureInternalStates$, 'pipe').and.returnValue(of([{ status: StateEnum.QUALIFIED, id: '123' }])); |
| const spySave = spyOn(service, 'saveGridFailure'); |
| service.setState(StateEnum.QUALIFIED); |
| expect(spySave).toHaveBeenCalled(); |
| expect(dispatchSpy).toHaveBeenCalled(); |
| }); |
| |
| it('should set the state in form to canceled and call save fuction', () => { |
| service.gridFailureInternalStates$ = { pipe: () => of({}), map: () => of({}) } as any; |
| spyOn(service.gridFailureInternalStates$, 'pipe').and.returnValue(of([{ status: StateEnum.CANCELED, id: '123' }])); |
| const spySave = spyOn(service, 'saveGridFailure'); |
| service.setState(StateEnum.CANCELED); |
| expect(spySave).toHaveBeenCalled(); |
| expect(dispatchSpy).toHaveBeenCalled(); |
| }); |
| it('should set the state in form to qualify and call save fuction', () => { |
| service.gridFailureInternalStates$ = { pipe: () => of({}), map: () => of({}) } as any; |
| spyOn(service.gridFailureInternalStates$, 'pipe').and.returnValue(of([{ status: StateEnum.QUALIFIED, id: '123' }])); |
| const spySave = spyOn(service, 'saveGridFailure'); |
| service.setState(StateEnum.QUALIFIED); |
| expect(spySave).toHaveBeenCalled(); |
| expect(dispatchSpy).toHaveBeenCalled(); |
| }); |
| |
| it('should set the state in form to canceled and call save fuction', () => { |
| service.gridFailureInternalStates$ = { pipe: () => of({}), map: () => of({}) } as any; |
| spyOn(service.gridFailureInternalStates$, 'pipe').and.returnValue(of([{ status: StateEnum.CANCELED, id: '123' }])); |
| const spySave = spyOn(service, 'saveGridFailure'); |
| service.setState(StateEnum.CANCELED); |
| expect(spySave).toHaveBeenCalled(); |
| expect(dispatchSpy).toHaveBeenCalled(); |
| }); |
| |
| it('should load GridFailureStations', () => { |
| service.loadGridFailureStations(); |
| expect(dispatchSpy).toHaveBeenCalledWith(Object({ type: gridFailureActions.loadGridFailureStations.type })); |
| }); |
| |
| it('should map lon and lat values', () => { |
| const gfdetail = new GridFailure(); |
| gfdetail.id = 'id'; |
| gfdetail.responsibility = 'test'; |
| |
| service.currentFormState = { |
| ...service.currentFormState, |
| isValid: true, |
| value: gfdetail, |
| }; |
| |
| service.latLonMapping(null); |
| expect(dispatchSpy).not.toHaveBeenCalled(); |
| |
| const event = new FailureStation(); |
| event.latitude = 1.24; |
| event.longitude = 1.23; |
| |
| service.latLonMapping(event); |
| |
| expect(dispatchSpy).toHaveBeenCalled(); |
| }); |
| |
| it('should dispatch loadAddressPostalcodes Action via loadAddressPostalcodes()', () => { |
| service.loadAddressPostalcodes(); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadAddressPostalcodes()); |
| }); |
| |
| it('should map stationId to gridFailure object', () => { |
| const gfdetail = new GridFailure(); |
| gfdetail.id = 'id'; |
| gfdetail.stationId = '1'; |
| |
| service.currentFormState = { |
| ...service.currentFormState, |
| isValid: true, |
| value: gfdetail, |
| }; |
| |
| const stationId = '2'; |
| |
| service.setStationId(stationId); |
| |
| expect(dispatchSpy).toHaveBeenCalled(); |
| }); |
| |
| it('should reset Coords', () => { |
| const gfdetail = new GridFailure(); |
| gfdetail.id = 'id'; |
| gfdetail.longitude = 1.0; |
| gfdetail.latitude = 1.0; |
| |
| service.currentFormState = { |
| ...service.currentFormState, |
| isValid: true, |
| value: gfdetail, |
| }; |
| |
| service.resetCoords(); |
| |
| expect(dispatchSpy).toHaveBeenCalled(); |
| }); |
| |
| it('should reset stationId', () => { |
| const gfdetail = new GridFailure(); |
| gfdetail.id = 'id'; |
| gfdetail.stationId = 'test'; |
| |
| service.currentFormState = { |
| ...service.currentFormState, |
| isValid: true, |
| value: gfdetail, |
| }; |
| |
| service.resetStationId(); |
| |
| expect(dispatchSpy).toHaveBeenCalled(); |
| }); |
| |
| it('should trigger formatter and check if the right value was returned', () => { |
| const failureStation: FailureStation = new FailureStation(); |
| failureStation.stationName = 'test'; |
| failureStation.stationId = 'test'; |
| const result1 = service.formatter(failureStation); |
| const result2 = service.formatter('test'); |
| |
| expect(result1).toBe('test (test)'); |
| expect(result2).toBe('test'); |
| }); |
| |
| it('should dispatch Action after loadPostalCodes', () => { |
| service.loadAddressPostalcodes(); |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadAddressPostalcodes()); |
| }); |
| |
| it('should trigger searchForAddressPostalcodes and check if the right value was returned', () => { |
| (service as any)._addressPostalcodes = ['test', 'test1', 'test2', 'hello']; |
| const text$ = of('hello'); |
| const result = service.searchForAddressPostalcodes(text$); |
| |
| result.subscribe(item => { |
| expect(item).toEqual(['hello']); |
| }); |
| }); |
| |
| it('should trigger searchForAddressPostalcodes and check if the right value was returned if length is under 2', () => { |
| (service as any)._addressPostalcodes = ['test', 'test1', 'test2', 'hello']; |
| const text$ = of('h'); |
| const result = service.searchForAddressPostalcodes(text$); |
| |
| result.subscribe(item => { |
| expect(item).toEqual([]); |
| }); |
| }); |
| |
| it('should trigger searchForStation and check if the right value was returned', () => { |
| const failureStation: FailureStation = new FailureStation(); |
| failureStation.stationName = 'hello'; |
| (service as any)._gridFailureStations = [new FailureStation(), new FailureStation(), failureStation]; |
| const text$ = of('hello'); |
| const result = service.searchForStation(text$); |
| |
| result.subscribe(item => { |
| expect(item).toEqual([failureStation]); |
| }); |
| }); |
| |
| it('should trigger searchForStation and check if the right value was returned if length is under 2', () => { |
| const failureStation: FailureStation = new FailureStation(); |
| failureStation.stationName = 'hello'; |
| (service as any)._gridFailureStations = [new FailureStation(), new FailureStation(), failureStation]; |
| const text$ = of('h'); |
| const result = service.searchForStation(text$); |
| |
| result.subscribe(item => { |
| expect(item).toEqual([]); |
| }); |
| }); |
| |
| it('should call setLatLong and dispatch loadGridFailureAddress()', () => { |
| const failureHousenumber = new FailureHousenumber(); |
| failureHousenumber.housenumber = 'test'; |
| failureHousenumber.uuid = 'hello'; |
| service.addressHouseNumbers = [failureHousenumber]; |
| const hsnr = 'test'; |
| service.setLatLong(hsnr); |
| |
| expect(appState.dispatch).toHaveBeenCalledWith(gridFailureActions.loadGridFailureAddress({ payload: failureHousenumber.uuid })); |
| }); |
| }); |