From 5235239d3139e766f5d9191ce096c7386eedd38f Mon Sep 17 00:00:00 2001 From: Pablo Martinez Garcia Date: Thu, 10 Aug 2023 15:34:50 +0200 Subject: [PATCH] [AAE-15836] Improve coverage --- lib/core/src/lib/pipes/full-name.pipe.spec.ts | 173 +++++------------- lib/core/src/lib/pipes/full-name.pipe.ts | 4 +- 2 files changed, 45 insertions(+), 132 deletions(-) diff --git a/lib/core/src/lib/pipes/full-name.pipe.spec.ts b/lib/core/src/lib/pipes/full-name.pipe.spec.ts index 9bd372107e..cf5878f234 100644 --- a/lib/core/src/lib/pipes/full-name.pipe.spec.ts +++ b/lib/core/src/lib/pipes/full-name.pipe.spec.ts @@ -17,15 +17,7 @@ import { FullNamePipe } from './full-name.pipe'; import { UserLike } from './user-like.interface'; - -interface TestCases { - [key: string]: { - title: string; - includeEmailToken: boolean | undefined; - includeEmailParameter: boolean | undefined; - testCases: TestCase[]; - }; -}; +import { cloneDeep } from 'lodash'; interface TestCase { title: string; @@ -37,147 +29,68 @@ describe('FullNamePipe', () => { let pipe: FullNamePipe; - const cosntBaseTestCases: TestCase[] = [ - { - title: 'should return empty string when there is no name', - user: { firstName: '', lastName: '', username: '', email: '' }, - result: '' - }, + const emptyUserTestCase: TestCase = { + title: 'should return empty string when there is no name', + user: {}, + result: '' + }; + + const onlyUserEmailTestCase: TestCase = { + title: 'should return user email when firstName, lastName and username are not available', + user: { email: 'abcXyz@gmail.com' }, + result: 'abcXyz@gmail.com' + }; + + const baseTestCases: TestCase[] = [ { title: 'should return only firstName as fullName when there is no lastName', - user: { firstName: 'Abc', lastName: '', username: '', email: '' }, + user: { firstName: 'Abc', lastName: '', username: '' }, result: 'Abc' }, { title: 'should return only lastName as fullName when there is no firstName', - user: { firstName: '', lastName: 'Xyz', username: '', email: '' }, + user: { firstName: '', lastName: 'Xyz', username: '' }, result: 'Xyz' }, { title: 'should return fullName when firstName and lastName are available', - user: { firstName: 'Abc', lastName: 'Xyz', username: '', email: '' }, + user: { firstName: 'Abc', lastName: 'Xyz', username: '' }, result: 'Abc Xyz' }, { title: 'should return username when firstName and lastName are not available', - user: { firstName: '', lastName: '', username: 'username', email: '' }, + user: { firstName: '', lastName: '', username: 'username' }, result: 'username' - }, - { - title: 'should return user email when firstName, lastName and username are not available', - user: { firstName: '', lastName: '', username: '', email: 'abcXyz@gmail.com' }, - result: 'abcXyz@gmail.com' } ]; - function patchEmailAddress(testCase: TestCase): TestCase { - return { - ...testCase, - user: { ...testCase.user, email: 'abcXyz@gmail.com' } - }; - } + [undefined, false, true].forEach(injectionToken => { + [undefined, false, true].forEach(pipeParameter => { + [undefined, '', 'abcXyz@gmail.com'].forEach(emailAddress => { + const testCases: TestCase[] = getTestCases(baseTestCases, emailAddress, pipeParameter, injectionToken, emptyUserTestCase, onlyUserEmailTestCase); - function patchEmailResult(testCase: TestCase): TestCase { - return { - ...testCase, - result: testCase.result + ' ' - }; - } - - function patchEmail(testCase: TestCase): TestCase { - return patchEmailResult(patchEmailAddress(testCase)); - } - - function getTestCasesWithEmailPatched(): TestCase[] { - return cosntBaseTestCases.slice(1, cosntBaseTestCases.length - 1).map(testCase => patchEmail(testCase)).concat([{ - title: 'should return user email when firstName, lastName and username are not available', - user: { firstName: '', lastName: '', username: '', email: 'abcXyz@gmail.com' }, - result: 'abcXyz@gmail.com' - }]); - } - - const testCases: TestCases = { - undefinedIncludeEmailTokenUndefinedIncludeEmailParameter: { - title: 'and include email token is undefined and include email is undefined', - includeEmailToken: undefined, - includeEmailParameter: undefined, - testCases: cosntBaseTestCases - }, - undefinedIncludeEmailTokenFalseIncludeEmailParameter: { - title: 'and include email token is undefined and include email is false', - includeEmailToken: undefined, - includeEmailParameter: false, - testCases: cosntBaseTestCases - }, - undefinedIncludeEmailTokenFTrueIncludeEmailParameter: { - title: 'and include email token is undefined and include email is true', - includeEmailToken: undefined, - includeEmailParameter: true, - testCases: getTestCasesWithEmailPatched() - }, - falseIncludeEmailTokenUndefinedIncludeEmailParameter: { - title: 'and include email token is false and include email is undefined', - includeEmailToken: false, - includeEmailParameter: undefined, - testCases: cosntBaseTestCases - }, - falseIncludeEmailTokenFalseIncludeEmailParameter: { - title: 'and include email token is false and include email is false', - includeEmailToken: false, - includeEmailParameter: false, - testCases: cosntBaseTestCases - }, - falseIncludeEmailTokenTrueIncludeEmailParameter: { - title: 'and include email token is false and include email is true', - includeEmailToken: false, - includeEmailParameter: true, - testCases: getTestCasesWithEmailPatched() - }, - trueIncludeEmailTokennUndefinedIncludeEmailParameterButEmailAddressNotPresent: { - title: 'and include email token is true and include email is undefined but email is not provided', - includeEmailToken: true, - includeEmailParameter: undefined, - testCases: cosntBaseTestCases.slice(0, cosntBaseTestCases.length - 1) - }, - trueIncludeEmailTokennFalseIncludeEmailParameterButEmailAddressNotPresent: { - title: 'and include email token is true and include email is false but email is not provided', - includeEmailToken: true, - includeEmailParameter: false, - testCases: cosntBaseTestCases.slice(0, cosntBaseTestCases.length - 1) - }, - trueIncludeEmailTokennTrueIncludeEmailParameterButEmailAddressNotPresent: { - title: 'and include email token is true and include email is true but email is not provided', - includeEmailToken: true, - includeEmailParameter: true, - testCases: cosntBaseTestCases.slice(0, cosntBaseTestCases.length - 1) - }, - trueIncludeEmailTokenUndefinedIncludeEmailParameterAndEmailAddressPresent: { - title: 'and include email token is true and include email is undefined and email is provided', - includeEmailToken: true, - includeEmailParameter: undefined, - testCases: getTestCasesWithEmailPatched() - }, - trueIncludeEmailTokenFalseIncludeEmailParameterAndEmailAddressPresent: { - title: 'and include email token is true and include email is false and email is provided', - includeEmailToken: true, - includeEmailParameter: false, - testCases: cosntBaseTestCases.slice(1, cosntBaseTestCases.length - 1).map(testCase => patchEmailAddress(testCase)) - }, - trueIncludeEmailTokenTrueIncludeEmailParameterAndEmailAddressPresent: { - title: 'and include email token is true and include email is true and email is provided', - includeEmailToken: true, - includeEmailParameter: true, - testCases: getTestCasesWithEmailPatched() - } - }; - - Object.keys(testCases).forEach(block => { - const testCasesToExecute = testCases[block].testCases; - testCasesToExecute.forEach(testCase => { - it(`${testCase.title} ${testCases[block].title}`, () => { - pipe = new FullNamePipe(testCases[block].includeEmailToken); - expect(pipe.transform(testCase.user, testCases[block].includeEmailParameter)).toBe(testCase.result); + testCases.forEach(testCase => { + it(`${testCase.title} and injection token is ${injectionToken} and pipe parameter is ${pipeParameter} and email address is ${emailAddress}`, () => { + pipe = new FullNamePipe(injectionToken); + expect(pipe.transform(testCase.user, pipeParameter)).toBe(testCase.result); + }); + }); }); }); }); }); + +function getTestCases(baseTestCases: TestCase[], emailAddress: string | undefined, pipeParameter: boolean | undefined, injectionToken: boolean | undefined, emptyUserTestCase: TestCase, onlyUserEmailTestCase: TestCase) { + let testCases: TestCase[] = cloneDeep(baseTestCases).map(testCase => ({ ...testCase, user: { ...testCase.user, email: emailAddress } })); + + const patchEmailInResult = !!(pipeParameter === undefined ? injectionToken : pipeParameter) && emailAddress?.length; + if (patchEmailInResult) { + testCases = testCases.map(testCase => ({ ...testCase, result: testCase.result.concat(' ') })); + } + + testCases.push(emptyUserTestCase); + testCases.push(onlyUserEmailTestCase); + + return testCases; +} + diff --git a/lib/core/src/lib/pipes/full-name.pipe.ts b/lib/core/src/lib/pipes/full-name.pipe.ts index 69e5833314..0a1fd63cec 100644 --- a/lib/core/src/lib/pipes/full-name.pipe.ts +++ b/lib/core/src/lib/pipes/full-name.pipe.ts @@ -30,7 +30,7 @@ export class FullNamePipe implements PipeTransform { } private includeEmailInFullName(includeEmail: boolean | undefined) { - return includeEmail === undefined ? this.includeEmail : includeEmail; + return includeEmail === undefined ? !!this.includeEmail : includeEmail; } private buildFullName(user: UserLike, includeEmail: boolean | undefined): string { @@ -47,7 +47,7 @@ export class FullNamePipe implements PipeTransform { fullName.push(user?.lastName); } - if (this.includeEmailInFullName(includeEmail) && hasName && user?.email) { + if (this.includeEmailInFullName(includeEmail) && hasName && !!user?.email) { fullName.push(`<${user.email}>`); }