From e80146ea973f3a9ab095cf56ba541668350f0938 Mon Sep 17 00:00:00 2001 From: danrevah Date: Mon, 13 Mar 2017 14:33:57 +0200 Subject: [PATCH] Improve Coverage --- src/app/pipes/array/every.spec.ts | 1 + src/app/pipes/array/every.ts | 4 ++-- src/app/pipes/array/initial.spec.ts | 5 +++++ src/app/pipes/array/unique.spec.ts | 5 +++++ src/app/pipes/math/max.spec.ts | 1 + src/app/pipes/math/max.ts | 2 +- src/app/pipes/math/min.spec.ts | 1 + src/app/pipes/math/min.ts | 2 +- src/app/pipes/math/pow.spec.ts | 5 +++++ src/app/pipes/math/pow.ts | 2 +- src/app/pipes/math/sqrt.spec.ts | 5 +++++ src/app/pipes/math/sqrt.ts | 2 +- src/app/pipes/math/sum.spec.ts | 5 +++++ src/app/pipes/math/sum.ts | 2 +- src/app/pipes/string/latinise.ts | 5 +++-- src/app/pipes/string/ucfirst.spec.ts | 5 +++++ src/app/pipes/string/ucfirst.ts | 2 +- src/app/pipes/string/ucwords.spec.ts | 5 +++++ src/app/pipes/string/ucwords.ts | 14 ++++++++------ 19 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/app/pipes/array/every.spec.ts b/src/app/pipes/array/every.spec.ts index dbf37543..a3fa20a5 100644 --- a/src/app/pipes/array/every.spec.ts +++ b/src/app/pipes/array/every.spec.ts @@ -9,6 +9,7 @@ describe('EveryPipe', () => { }); it('should check if every elements of the array fits the predicate', () => { + expect(pipe.transform(42, isNumber)).toBeFalsy(); expect(pipe.transform([1, 2, 3, 4], isNumber)).toBeTruthy(); expect(pipe.transform([1, 2, 3, 'a'], isNumber)).toBeFalsy(); expect(pipe.transform([1, 2, 3, 4], isUndefined)).toBeFalsy(); diff --git a/src/app/pipes/array/every.ts b/src/app/pipes/array/every.ts index b7948d59..05d21e45 100644 --- a/src/app/pipes/array/every.ts +++ b/src/app/pipes/array/every.ts @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core'; @Pipe({name: 'every'}) export class EveryPipe implements PipeTransform { - transform(input: any[], predicate: (value: any, index: number, array: any[]) => boolean): boolean|any[] { - return Array.isArray(input) ? input.every(predicate) : input; + transform(input: any, predicate: (value: any, index: number, array: any[]) => boolean): boolean|any[] { + return Array.isArray(input) ? input.every(predicate) : false; } } diff --git a/src/app/pipes/array/initial.spec.ts b/src/app/pipes/array/initial.spec.ts index cc610761..811fb11c 100644 --- a/src/app/pipes/array/initial.spec.ts +++ b/src/app/pipes/array/initial.spec.ts @@ -7,6 +7,11 @@ describe('InitialPipe', () => { pipe = new InitialPipe(); }); + it('should return value if not array', () => { + expect(pipe.transform(42)).toEqual(42); + expect(pipe.transform(false)).toEqual(false); + }); + it('should check that nothing happens if there are no parameters', () => { let arr = [1, 2, 3]; let result = pipe.transform(arr); diff --git a/src/app/pipes/array/unique.spec.ts b/src/app/pipes/array/unique.spec.ts index cf5742f7..5d91e278 100644 --- a/src/app/pipes/array/unique.spec.ts +++ b/src/app/pipes/array/unique.spec.ts @@ -7,6 +7,11 @@ describe('DiffPipe', () => { pipe = new UniquePipe(); }); + it('should return value if not array', () => { + expect(pipe.transform(42)).toEqual(42); + expect(pipe.transform(false)).toEqual(false); + }); + it('should keep the array the same way if it doesn\'t have any duplicate elements', () => { expect(pipe.transform([1, 2, 3])).toEqual([1, 2, 3]); expect(pipe.transform([])).toEqual([]); diff --git a/src/app/pipes/math/max.spec.ts b/src/app/pipes/math/max.spec.ts index 31fd497c..6720484c 100644 --- a/src/app/pipes/math/max.spec.ts +++ b/src/app/pipes/math/max.spec.ts @@ -8,6 +8,7 @@ describe('MaxPipe', () => { }); it('should return max element from array', () => { + expect(pipe.transform(1)).toEqual(1); expect(pipe.transform([1, 2, 3])).toEqual(3); expect(pipe.transform([5, 4, 3, 2, 1, 2, 3, 4, 5])).toEqual(5); expect(pipe.transform([5, 4, 3, 2, 1, 2, 3, 4, -5, 19])).toEqual(19); diff --git a/src/app/pipes/math/max.ts b/src/app/pipes/math/max.ts index 47915d76..011dee83 100644 --- a/src/app/pipes/math/max.ts +++ b/src/app/pipes/math/max.ts @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core'; @Pipe({name: 'max'}) export class MaxPipe implements PipeTransform { - transform(arr: number[]): number|number[] { + transform(arr: any): number|number[] { return Array.isArray(arr) ? Math.max(...arr) : arr; diff --git a/src/app/pipes/math/min.spec.ts b/src/app/pipes/math/min.spec.ts index f443b283..80593cb6 100644 --- a/src/app/pipes/math/min.spec.ts +++ b/src/app/pipes/math/min.spec.ts @@ -8,6 +8,7 @@ describe('MinPipe', () => { }); it('should return min element from array', () => { + expect(pipe.transform(42)).toEqual(42); expect(pipe.transform([1, 2, 3])).toEqual(1); expect(pipe.transform([5, 4, 3, 2, 1, 2, 3, 4, 5])).toEqual(1); expect(pipe.transform([5, 4, 3, 2, 1, 2, 3, 4, -5])).toEqual(-5); diff --git a/src/app/pipes/math/min.ts b/src/app/pipes/math/min.ts index 1a7625a2..8f143104 100644 --- a/src/app/pipes/math/min.ts +++ b/src/app/pipes/math/min.ts @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core'; @Pipe({name: 'min'}) export class MinPipe implements PipeTransform { - transform(arr: number[]): number|number[] { + transform(arr: any): number|number[] { return Array.isArray(arr) ? Math.min(...arr) : arr; diff --git a/src/app/pipes/math/pow.spec.ts b/src/app/pipes/math/pow.spec.ts index e801ca75..40734d64 100644 --- a/src/app/pipes/math/pow.spec.ts +++ b/src/app/pipes/math/pow.spec.ts @@ -7,6 +7,11 @@ describe('PowerPipe', () => { pipe = new PowerPipe(); }); + it('should return value if not a number', () => { + expect(pipe.transform({a: 1})).toEqual({a: 1}); + expect(pipe.transform(NaN)).toEqual(NaN); + }); + it('should return power of a given number', () => { expect(pipe.transform(3)).toEqual(9); expect(pipe.transform(3, 3)).toEqual(27); diff --git a/src/app/pipes/math/pow.ts b/src/app/pipes/math/pow.ts index f554c945..ee94ef1a 100644 --- a/src/app/pipes/math/pow.ts +++ b/src/app/pipes/math/pow.ts @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core'; @Pipe({name: 'pow'}) export class PowerPipe implements PipeTransform { - transform(num: number, power: number = 2): number { + transform(num: any, power: number = 2): number { return !isNaN(num) ? num ** power : num ; diff --git a/src/app/pipes/math/sqrt.spec.ts b/src/app/pipes/math/sqrt.spec.ts index 91e090b9..9d8b6ff6 100644 --- a/src/app/pipes/math/sqrt.spec.ts +++ b/src/app/pipes/math/sqrt.spec.ts @@ -7,6 +7,11 @@ describe('SqrtPipe', () => { pipe = new SqrtPipe(); }); + it('should return value if not a number', () => { + expect(pipe.transform({a: 1})).toEqual({a: 1}); + expect(pipe.transform(NaN)).toEqual(NaN); + }); + it('should return square of given number', () => { expect(pipe.transform(9)).toEqual(3); expect(pipe.transform(16)).toEqual(4); diff --git a/src/app/pipes/math/sqrt.ts b/src/app/pipes/math/sqrt.ts index 7fad19e9..3f6ab60a 100644 --- a/src/app/pipes/math/sqrt.ts +++ b/src/app/pipes/math/sqrt.ts @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core'; @Pipe({name: 'sqrt'}) export class SqrtPipe implements PipeTransform { - transform(num: number): number { + transform(num: any): number { return !isNaN(num) ? Math.sqrt(num) : num; diff --git a/src/app/pipes/math/sum.spec.ts b/src/app/pipes/math/sum.spec.ts index c8df4f16..ee682e07 100644 --- a/src/app/pipes/math/sum.spec.ts +++ b/src/app/pipes/math/sum.spec.ts @@ -7,6 +7,11 @@ describe('SumPipe', () => { pipe = new SumPipe(); }); + it('should return value if not an array', () => { + expect(pipe.transform({a: 1})).toEqual({a: 1}); + expect(pipe.transform(NaN)).toEqual(NaN); + }); + it('should return sum of element', () => { expect(pipe.transform([1, 2, 3, 4])).toEqual(10); expect(pipe.transform([5, 4, 3, 2, 1, -2, -3, -4, -5])).toEqual(1); diff --git a/src/app/pipes/math/sum.ts b/src/app/pipes/math/sum.ts index 5a0947b8..92befe96 100644 --- a/src/app/pipes/math/sum.ts +++ b/src/app/pipes/math/sum.ts @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core'; @Pipe({name: 'sum'}) export class SumPipe implements PipeTransform { - transform(arr: number[]): number|number[] { + transform(arr: any): number|number[] { return Array.isArray(arr) ? arr.reduce((sum, curr) => sum + curr, 0) : arr; diff --git a/src/app/pipes/string/latinise.ts b/src/app/pipes/string/latinise.ts index 7349d19b..062cd8c8 100644 --- a/src/app/pipes/string/latinise.ts +++ b/src/app/pipes/string/latinise.ts @@ -9,7 +9,8 @@ export class LatinisePipe implements PipeTransform { transform(text: any, chars: string = '\\s'): string { return isString(text) - ? text.replace(/[^A-Za-z0-9]/g, (key: string) => (this.latinMap[key] || key)) - : text; + ? text.replace(/[^A-Za-z0-9]/g, (key: string) => { + return this.latinMap[key] || key; + }) : text; } } diff --git a/src/app/pipes/string/ucfirst.spec.ts b/src/app/pipes/string/ucfirst.spec.ts index 0216ed0c..03a8065d 100644 --- a/src/app/pipes/string/ucfirst.spec.ts +++ b/src/app/pipes/string/ucfirst.spec.ts @@ -7,6 +7,11 @@ describe('UcFirstPipe Tests', () => { pipe = new UcFirstPipe(); }); + it('Should return value if not a string', () => { + expect(pipe.transform(42)).toEqual(42); + expect(pipe.transform(false)).toEqual(false); + }); + it('Should capitalize first word in a string', () => { let result = pipe.transform('foo bar baz'); expect(result).toEqual('Foo bar baz'); diff --git a/src/app/pipes/string/ucfirst.ts b/src/app/pipes/string/ucfirst.ts index 8904fe95..858dd1b3 100644 --- a/src/app/pipes/string/ucfirst.ts +++ b/src/app/pipes/string/ucfirst.ts @@ -4,7 +4,7 @@ import {isString} from '../helpers/helpers'; @Pipe({name: 'ucfirst'}) export class UcFirstPipe implements PipeTransform { - transform(text: string):string { + transform(text: any):string { return isString(text) ? (text.slice(0, 1).toUpperCase() + text.slice(1)) : text; diff --git a/src/app/pipes/string/ucwords.spec.ts b/src/app/pipes/string/ucwords.spec.ts index 84cdd65c..ab2eba86 100644 --- a/src/app/pipes/string/ucwords.spec.ts +++ b/src/app/pipes/string/ucwords.spec.ts @@ -7,6 +7,11 @@ describe('UcFirstPipe Tests', () => { pipe = new UcWordsPipe(); }); + it('Should return value if not a string', () => { + expect(pipe.transform(42)).toEqual(42); + expect(pipe.transform(false)).toEqual(false); + }); + it('Should capitalize all words in a string', () => { let result = pipe.transform('foo bar baz'); expect(result).toEqual('Foo Bar Baz'); diff --git a/src/app/pipes/string/ucwords.ts b/src/app/pipes/string/ucwords.ts index 67f658e5..aca93121 100644 --- a/src/app/pipes/string/ucwords.ts +++ b/src/app/pipes/string/ucwords.ts @@ -4,11 +4,13 @@ import {isString} from '../helpers/helpers'; @Pipe({name: 'ucwords'}) export class UcWordsPipe implements PipeTransform { - transform(text: string): string { - return isString(text) - ? text.split(' ') - .map(sub => sub.slice(0, 1).toUpperCase() + sub.slice(1)) - .join(' ') - : text; + transform(text: any): string { + if (isString(text)) { + return text.split(' ') + .map((sub: any) => sub.slice(0, 1).toUpperCase() + sub.slice(1)) + .join(' '); + } + + return text; } }