[SI-176] FE Unit Tests added
diff --git a/angular.json b/angular.json index bba4260..f0029a8 100644 --- a/angular.json +++ b/angular.json
@@ -135,7 +135,7 @@ "scripts": [], "codeCoverage": true, "codeCoverageExclude": [ - "src/environments/**", + "**/environments/**", "e2e/*.ts", "**/*.action.*", "**/*.model.*", @@ -144,7 +144,8 @@ "**/*api-client.*", "**/*.animation.*", "**/lib/**/*", - "**/testing/**/*" + "**/testing/**/*", + "**/assets/**/*" ] } }, @@ -267,7 +268,21 @@ "karmaConfig": "projects/grid-failure-information-map-app/karma.conf.js", "assets": ["projects/grid-failure-information-map-app/src/favicon.ico", "projects/grid-failure-information-map-app/src/assets"], "styles": ["projects/grid-failure-information-map-app/src/styles.scss"], - "scripts": [] + "scripts": [], + "codeCoverage": true, + "codeCoverageExclude": [ + "**/environments/**", + "e2e/*.ts", + "**/*.action.*", + "**/*.model.*", + "**/*.module.*", + "**/*column-definition*", + "**/*api-client.*", + "**/*.animation.*", + "**/lib/**/*", + "**/testing/**/*", + "**/assets/**/*" + ] } }, "lint": { @@ -313,7 +328,20 @@ "options": { "main": "projects/openk/grid-failure-information-map/src/test.ts", "tsConfig": "projects/openk/grid-failure-information-map/tsconfig.spec.json", - "karmaConfig": "projects/openk/grid-failure-information-map/karma.conf.js" + "karmaConfig": "projects/openk/grid-failure-information-map/karma.conf.js", + "codeCoverage": true, + "codeCoverageExclude": [ + "**/environments/**", + "e2e/*.ts", + "**/*.action.*", + "**/*.model.*", + "**/*.module.*", + "**/*column-definition*", + "**/*api-client.*", + "**/*.animation.*", + "**/testing/**/*", + "**/assets/**/*" + ] } }, "lint": {
diff --git a/package.json b/package.json index e04426c..a3ce0ab 100644 --- a/package.json +++ b/package.json
@@ -14,11 +14,11 @@ "start-integration": "npm run sy-pre-start && npm run build-map-library && ng serve --proxy-config proxy.conf-integration.json", "start-local": "npm run sy-pre-start && ng serve --proxy-config proxy.conf-local.json", "test": "npm run sy-pre-test && ng test grid-failure-information-app", - "test-single-run": "npm run sy-pre-test && ng test --code-coverage grid-failure-information-app --watch=false", - "test-openk": "npm run sy-pre-test && ng test --code-coverage openk-grid-failure-information-map-lib", - "test-openk-single-run": "npm run sy-pre-test && ng test --code-coverage openk-grid-failure-information-map-lib --watch=false", - "test-map-app": "npm run sy-pre-test && ng test --code-coverage grid-failure-information-map-app", - "test-map-app-single-run": "npm run sy-pre-test && ng test --code-coverage grid-failure-information-map-app --watch=false", + "test-single-run": "npm run sy-pre-test && ng test grid-failure-information-app --watch=false", + "test-openk": "npm run sy-pre-test && ng test openk-grid-failure-information-map-lib", + "test-openk-single-run": "npm run sy-pre-test && ng test openk-grid-failure-information-map-lib --watch=false", + "test-map-app": "npm run sy-pre-test && ng test grid-failure-information-map-app", + "test-map-app-single-run": "npm run sy-pre-test && ng test grid-failure-information-map-app --watch=false", "test-all": "npm run test-single-run && npm run test-openk-single-run", "pree2e": "webdriver-manager update --standalone false --gecko false", "e2e": "ng e2e",
diff --git a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.spec.ts b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.spec.ts index f47c3a1..8833305 100644 --- a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.spec.ts +++ b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.spec.ts
@@ -18,6 +18,7 @@ import { UtilService } from '@grid-failure-information-app/shared/utility/utility.service'; import { Router } from '@angular/router'; import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; +import { GridFailure } from '@grid-failure-information-app/shared/models'; describe('GridFailureDetailsSandbox', () => { let service: GridFailureDetailsSandbox; @@ -26,6 +27,7 @@ let utilService: UtilService; let router: Router; let modalService: NgbModal; + let dispatchSpy: any; beforeEach(() => { appState = { dispatch: () => {}, pipe: () => of(true), select: () => of(true) } as any; @@ -33,7 +35,7 @@ utilService = { displayNotification() {} } as any; router = { navigateByUrl() {} } as any; modalService = { open() {} } as any; - spyOn(appState, 'dispatch').and.callFake(() => {}); + dispatchSpy = spyOn(appState, 'dispatch').and.callFake(() => {}); service = new GridFailureDetailsSandbox(appState, actionSubject, router, utilService, modalService); }); @@ -117,4 +119,35 @@ 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'; + + service.currentFormState = { + ...service.currentFormState, + isValid: true, + value: gfdetail, + }; + 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'; + + service.currentFormState = { + ...service.currentFormState, + isValid: false, + value: gfdetail, + }; + service.saveGridFailure(); + expect(spy1).toHaveBeenCalled(); + }); });
diff --git a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure.resolver.spec.ts b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure.resolver.spec.ts index c42bcf7..7d8e08d 100644 --- a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure.resolver.spec.ts +++ b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure.resolver.spec.ts
@@ -45,8 +45,10 @@ it('should call loadGridFailure', () => { const spy = spyOn(detailSandbox, 'loadGridFailure'); + const spy2 = spyOn(detailSandbox, 'loadGridFailureVersions'); let ar: any = { params: { gridFailureId: '6432a9c9-0384-44af-9bb8-34f2878d7b49' } }; component.resolve(ar); expect(spy).toHaveBeenCalled(); + expect(spy2).toHaveBeenCalled(); }); });
diff --git a/projects/grid-failure-information-app/src/app/pages/logout/logged-out/logged-out.component.spec.ts b/projects/grid-failure-information-app/src/app/pages/logout/logged-out/logged-out.component.spec.ts index 4ae208c..d7b4841 100644 --- a/projects/grid-failure-information-app/src/app/pages/logout/logged-out/logged-out.component.spec.ts +++ b/projects/grid-failure-information-app/src/app/pages/logout/logged-out/logged-out.component.spec.ts
@@ -22,4 +22,11 @@ it('should create', () => { expect(component).toBeTruthy(); }); + + it('should focus on button on init', () => { + component.button = { nativeElement: { focus() {} } }; + const spy = spyOn(component.button.nativeElement, 'focus').and.callThrough(); + component.ngOnInit(); + expect(spy).toHaveBeenCalled(); + }); });
diff --git a/projects/grid-failure-information-app/src/app/shared/components/date-time-picker/date-time-picker.component.ts b/projects/grid-failure-information-app/src/app/shared/components/date-time-picker/date-time-picker.component.ts index 4759add..69561fe 100644 --- a/projects/grid-failure-information-app/src/app/shared/components/date-time-picker/date-time-picker.component.ts +++ b/projects/grid-failure-information-app/src/app/shared/components/date-time-picker/date-time-picker.component.ts
@@ -106,7 +106,6 @@ const date = DateTimeModel.fromLocalString($event); if (!date) { - this.dateString = this.dateString; return; }
diff --git a/projects/grid-failure-information-app/src/app/shared/models/date-time.model.ts b/projects/grid-failure-information-app/src/app/shared/models/date-time.model.ts index a9f969f..355ec16 100644 --- a/projects/grid-failure-information-app/src/app/shared/models/date-time.model.ts +++ b/projects/grid-failure-information-app/src/app/shared/models/date-time.model.ts
@@ -1,5 +1,16 @@ +/******************************************************************************** + * 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 { NgbTimeStruct, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; -import { DatePipe } from '@angular/common'; export interface NgbDateTimeStruct extends NgbDateStruct, NgbTimeStruct {}
diff --git a/projects/grid-failure-information-app/src/app/shared/models/server-side.model.ts b/projects/grid-failure-information-app/src/app/shared/models/server-side.model.ts index 4085140..98cac3f 100644 --- a/projects/grid-failure-information-app/src/app/shared/models/server-side.model.ts +++ b/projects/grid-failure-information-app/src/app/shared/models/server-side.model.ts
@@ -1,4 +1,4 @@ - /******************************************************************************** +/******************************************************************************** * Copyright (c) 2020 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional @@ -10,13 +10,11 @@ * * SPDX-License-Identifier: EPL-2.0 ********************************************************************************/ -import { Action } from '@ngrx/store'; - export class ServerSideModel { loadAction: any; successAction?: any; pageSize?: number; - filter?:any; + filter?: any; constructor() {} }
diff --git a/projects/grid-failure-information-app/src/app/shared/pipes/ngb-date-custom-parser-formatter.spec.ts b/projects/grid-failure-information-app/src/app/shared/pipes/ngb-date-custom-parser-formatter.spec.ts index 21f4fa3..57ce33c 100644 --- a/projects/grid-failure-information-app/src/app/shared/pipes/ngb-date-custom-parser-formatter.spec.ts +++ b/projects/grid-failure-information-app/src/app/shared/pipes/ngb-date-custom-parser-formatter.spec.ts
@@ -1,4 +1,17 @@ -import { NgbDateCustomParserFormatter, toInteger } from '@grid-failure-information-app/shared/pipes/ngb-date-custom-parser-formatter'; +/******************************************************************************** + * 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 { DateTimeModel } from '@grid-failure-information-app/shared/models/date-time.model'; +import { NgbDateCustomParserFormatter } from '@grid-failure-information-app/shared/pipes/ngb-date-custom-parser-formatter'; describe('NgbDateCustomParserFormatter', () => { let service: NgbDateCustomParserFormatter; @@ -11,8 +24,48 @@ expect(service).toBeTruthy(); }); - it('should parse string to Integer via toInteger()', () => { - let testValue = toInteger('1'); - expect(testValue).toBe(parseInt(`${1}`, 10)); + it('should format a date to string', () => { + const date: DateTimeModel = new DateTimeModel(); + date.year = 2020; + date.month = 4; + date.day = 1; + date.hour = 12; + date.minute = 30; + date.second = 30; + expect(service.format(date)).toBe('01.04.2020 / 00:00'); + }); + + it('should parse a string with 1 part into a date', () => { + const stringDate: string = '01'; + expect(service.parse(stringDate)).toEqual(Object({ day: 1, month: null, year: null, hour: null, minute: null, second: null })); + }); + + it('should parse a string with 2 parts into a date', () => { + const stringDate: string = '01/04'; + expect(service.parse(stringDate)).toEqual(Object({ day: 1, month: 4, year: null, hour: null, minute: null, second: null })); + }); + + it('should parse a string with 3 parts into a date', () => { + const stringDate: string = '01/04/2020'; + expect(service.parse(stringDate)).toEqual(Object({ day: 1, month: 4, year: 2020, hour: null, minute: null, second: null })); + }); + + it('should parse a string with 4 parts into a date', () => { + const stringDate: string = '01/04/2020/12'; + expect(service.parse(stringDate)).toEqual(Object({ day: 1, month: 4, year: 2020, hour: 12, minute: null, second: null })); + }); + + it('should parse a string with 5 parts into a date', () => { + const stringDate: string = '01/04/2020/12/30'; + expect(service.parse(stringDate)).toEqual(Object({ day: 1, month: 4, year: 2020, hour: 12, minute: 30, second: null })); + }); + + it('should parse a string with 6 parts into a date', () => { + const stringDate: string = '01/04/2020/12/30/30'; + expect(service.parse(stringDate)).toEqual(Object({ day: 1, month: 4, year: 2020, hour: 12, minute: 30, second: 30 })); + }); + + it('should return null parse called without value', () => { + expect(service.parse(null)).toEqual(null); }); });
diff --git a/projects/grid-failure-information-app/src/app/shared/pipes/ngb-date-custom-parser-formatter.ts b/projects/grid-failure-information-app/src/app/shared/pipes/ngb-date-custom-parser-formatter.ts index 1118ebe..bb51e4f 100644 --- a/projects/grid-failure-information-app/src/app/shared/pipes/ngb-date-custom-parser-formatter.ts +++ b/projects/grid-failure-information-app/src/app/shared/pipes/ngb-date-custom-parser-formatter.ts
@@ -1,7 +1,19 @@ -import { DateTimeModel } from './../models/date-time.model'; +/******************************************************************************** + * 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 { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap'; import { Injectable } from '@angular/core'; -import { NgbDateTimeStruct } from '../models/date-time.model'; +import { NgbDateTimeStruct } from '@grid-failure-information-app/shared/models/date-time.model'; +import { toInteger, isNumber, padNumber } from '@grid-failure-information-app/shared/utility'; @Injectable() export class NgbDateCustomParserFormatter extends NgbDateParserFormatter { @@ -77,18 +89,3 @@ : ''; } } -export function toInteger(value: any): number { - return parseInt(`${value}`, 10); -} - -export function isNumber(value: any): value is number { - return !isNaN(toInteger(value)); -} - -export function padNumber(value: number) { - if (isNumber(value)) { - return `0${value}`.slice(-2); - } else { - return ''; - } -}
diff --git a/projects/grid-failure-information-app/src/app/shared/pipes/unique.pipe.spec.ts b/projects/grid-failure-information-app/src/app/shared/pipes/unique.pipe.spec.ts index 2063a44..7aaa568 100644 --- a/projects/grid-failure-information-app/src/app/shared/pipes/unique.pipe.spec.ts +++ b/projects/grid-failure-information-app/src/app/shared/pipes/unique.pipe.spec.ts
@@ -1,4 +1,4 @@ - /******************************************************************************** +/******************************************************************************** * Copyright (c) 2020 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional @@ -14,7 +14,6 @@ import { UniquePipe } from './unique.pipe'; describe('Pipe: Uniquee', () => { - let uniquePipe: UniquePipe; // synchronous beforeEach @@ -27,7 +26,6 @@ }); it('should return empty array if no items given', () => { - const items = []; const filtered = uniquePipe.transform(items, 'filedNameText'); @@ -37,7 +35,6 @@ }); it('should return empty array if no items given (2)', () => { - const items = []; items.push({ branch: 'W', title: 'its a test title', statusId: '3', createUser: 'otto' }); @@ -47,7 +44,6 @@ }); it('should return empty array if items are undefined', () => { - const items = undefined; const filtered = uniquePipe.transform(items, 'filedNameText'); @@ -55,4 +51,19 @@ expect(filtered).toEqual([]); }); + it('should return the whole input array if all items are defined and unique', () => { + const items = [{ test: '1' }, { test: '2' }, { test: '3' }]; + const filtered = uniquePipe.transform(items, 'test'); + + expect(filtered.length).toBe(3); + expect(filtered).toEqual([{ test: '1' }, { test: '2' }, { test: '3' }]); + }); + + it('should return only the unique elements of the input array', () => { + const items = [{ test: '1' }, { test: '2' }, { test: '3' }, { test: '3' }]; + const filtered = uniquePipe.transform(items, 'test'); + + expect(filtered.length).toBe(3); + expect(filtered).toEqual([{ test: '1' }, { test: '2' }, { test: '3' }]); + }); });
diff --git a/projects/grid-failure-information-app/src/app/shared/utility/utilityHelpers.spec.ts b/projects/grid-failure-information-app/src/app/shared/utility/utilityHelpers.spec.ts index 02507d9..bbeb8f9 100644 --- a/projects/grid-failure-information-app/src/app/shared/utility/utilityHelpers.spec.ts +++ b/projects/grid-failure-information-app/src/app/shared/utility/utilityHelpers.spec.ts
@@ -19,4 +19,35 @@ utilityHelpers.navigateHome(router); expect(spy).toHaveBeenCalledWith('/grid-failures'); }); + + it('should parse string to Integer via toInteger()', () => { + let testValue = utilityHelpers.toInteger('1'); + expect(testValue).toBe(parseInt(`${1}`, 10)); + }); + + it('should return true if a value is a number', () => { + let testValue = utilityHelpers.isNumber('1'); + expect(testValue).toBe(true); + }); + + it('should return false if a value is not a number', () => { + let testValue = utilityHelpers.isNumber('a'); + expect(testValue).toBe(false); + }); + + it('should pad the number if a value is a number', () => { + let testValue = utilityHelpers.padNumber(1); + expect(testValue).toBe('01'); + }); + + it('should return empty string if a value is not a number', () => { + let testValue = utilityHelpers.padNumber(null); + expect(testValue).toBe(''); + }); + + it('should convert a string date to a german date string', () => { + const dateString: string = '2020-12-01'; + let testValue = utilityHelpers.localeDateString(dateString); + expect(testValue).toBe('1.12.2020'); + }); });
diff --git a/projects/grid-failure-information-app/src/app/shared/utility/utilityHelpers.ts b/projects/grid-failure-information-app/src/app/shared/utility/utilityHelpers.ts index 057ce53..d845f3b 100644 --- a/projects/grid-failure-information-app/src/app/shared/utility/utilityHelpers.ts +++ b/projects/grid-failure-information-app/src/app/shared/utility/utilityHelpers.ts
@@ -19,7 +19,7 @@ * @param dateString * @param culture */ -export function localeDateString(dateString: string, culture: string = 'en-EN'): string { +export function localeDateString(dateString: string, culture: string = 'de-DE'): string { let date = new Date(dateString); return date.toLocaleDateString(culture); }
diff --git a/sonar-project.properties b/sonar-project.properties index 351f0b9..5d483ef 100644 --- a/sonar-project.properties +++ b/sonar-project.properties
@@ -10,7 +10,7 @@ sonar.sourceEncoding=UTF-8 sonar.sources=projects/grid-failure-information-app/src, projects/openk/grid-failure-information-map/src -sonar.exclusions=**/node_modules/**,projects/grid-failure-information-app/src/environments/**,e2e/*.ts,**/*.spec.ts,**/test-data/*.ts,**/testing/*.ts,**/assets/js/*.js,**/*.model.*,**/*.action.*,**/*.module.*,**/*column-definition*,**/*animation*,**/*api-client.*,**/styles.scss +sonar.exclusions=**/node_modules/**,**/environments/**,e2e/*.ts,**/*.spec.ts,**/test-data/*.ts,**/testing/*.ts,**/assets/**/*,**/*.model.*,**/*.action.*,**/*.module.*,**/*column-definition*,**/*animation*,**/*api-client.*,**/styles.scss sonar.tests=projects/grid-failure-information-app/src, projects/openk/grid-failure-information-map/src sonar.test.inclusions=**/*.spec.ts sonar.test.exclusions=