From c419a3daf64a6b3efbe8e1592bb65dcd74651232 Mon Sep 17 00:00:00 2001 From: luisa-beerboom <101706784+luisa-beerboom@users.noreply.github.com> Date: Fri, 14 Jul 2023 10:26:01 +0200 Subject: [PATCH] Improve test coverage (#2525) --- .../error-mapping/error-map.service.spec.ts | 66 +++++++++++++------ .../src/app/gateways/storage.service.spec.ts | 53 ++++++++++++++- 2 files changed, 96 insertions(+), 23 deletions(-) diff --git a/client/src/app/gateways/error-mapping/error-map.service.spec.ts b/client/src/app/gateways/error-mapping/error-map.service.spec.ts index b0d2b82054..f0c321cc51 100644 --- a/client/src/app/gateways/error-mapping/error-map.service.spec.ts +++ b/client/src/app/gateways/error-mapping/error-map.service.spec.ts @@ -20,13 +20,13 @@ describe(`ErrorMapService`, () => { const testData: { title: string; - test: { message: string; url: string; data?: any }; + test: { message: string; options: { url: string; data?: any } }; expect: string | Error; testConditionCheck?: boolean; // Boolean value that determines wheter the error map constants still have a form that is conducive to this test }[] = [ { title: `test with unregistered url fragments`, - test: { message: `Message`, url: `no/meaning`, data: {} }, + test: { message: `Message`, options: { url: `no/meaning`, data: {} } }, expect: `Error: Message`, testConditionCheck: ![`no`, `meaning`].some(fragment => urlFragmentKeys.includes(fragment)) }, @@ -34,7 +34,7 @@ describe(`ErrorMapService`, () => { title: `test with registered fragments and unknown message`, test: { message: `This is not an auth service message`, - url: `random/url/with/auth/meaning` + options: { url: `random/url/with/auth/meaning` } }, expect: `Error: This is not an auth service message`, testConditionCheck: @@ -46,7 +46,7 @@ describe(`ErrorMapService`, () => { title: `test with registered fragments and registered message`, test: { message: `Property blue is yellow`, - url: `random/url/with/auth/meaning` + options: { url: `random/url/with/auth/meaning` } }, expect: `Error: User not found.` }, @@ -54,7 +54,7 @@ describe(`ErrorMapService`, () => { title: `test with registered fragments and message that only exists in other map`, test: { message: `Not the first vote`, - url: `random/url/with/auth/meaning` + options: { url: `random/url/with/auth/meaning` } }, expect: `Error: Not the first vote`, testConditionCheck: @@ -66,7 +66,7 @@ describe(`ErrorMapService`, () => { title: `test with registered fragments and message that returns error`, test: { message: `Username or password is incorrect.`, - url: `random/url/with/auth/meaning` + options: { url: `random/url/with/auth/meaning` } }, expect: new Error(`Username or password is incorrect.`) }, @@ -74,7 +74,7 @@ describe(`ErrorMapService`, () => { title: `test with 'http' fragment in url`, test: { message: `Username or password is incorrect.`, - url: `http://test.openslides.com/auth/meaning` + options: { url: `http://test.openslides.com/auth/meaning` } }, expect: new Error(`Username or password is incorrect.`) }, @@ -82,7 +82,7 @@ describe(`ErrorMapService`, () => { title: `test with 'https' fragment in url`, test: { message: `Username or password is incorrect.`, - url: `https://test.openslides.com/auth/meaning` + options: { url: `https://test.openslides.com/auth/meaning` } }, expect: new Error(`Username or password is incorrect.`) }, @@ -90,21 +90,21 @@ describe(`ErrorMapService`, () => { title: `test with a fragment that returns an ErrorMap getter function with a message that returns a function`, test: { message: `I am a forwarding error!`, - url: `random/action`, - data: getMockActionFromName(MotionAction.CREATE_FORWARDED) + options: { url: `random/action`, data: getMockActionFromName(MotionAction.CREATE_FORWARDED) } }, expect: new Error(`I am a forwarding error!`), testConditionCheck: !!actionMap && typeof actionMap === `function` && - !!actionMap(getMockActionFromName(MotionAction.CREATE_FORWARDED)) + !!actionMap(getMockActionFromName(MotionAction.CREATE_FORWARDED)) && + typeof Array.from(actionMap(getMockActionFromName(MotionAction.CREATE_FORWARDED)).values())[0] === + `function` }, { title: `test with a fragment that returns a function with data that returns nothing`, test: { message: `I am a create error!`, - url: `random/action`, - data: getMockActionFromName(MotionAction.CREATE) + options: { url: `random/action`, data: getMockActionFromName(MotionAction.CREATE) } }, expect: `Error: I am a create error!`, testConditionCheck: @@ -114,7 +114,7 @@ describe(`ErrorMapService`, () => { title: `test with an action fragment and no data`, test: { message: `I am some error!`, - url: `random/action` + options: { url: `random/action` } }, expect: `Error: I am some error!` }, @@ -122,9 +122,17 @@ describe(`ErrorMapService`, () => { title: `test with two registered fragments (should pick first)`, test: { message: `Not the first vote`, - url: `random/vote/no/auth` + options: { url: `random/vote/no/auth` } }, expect: `Error: You have already voted.` + }, + { + title: `test with options === null`, + test: { + message: `404 no options!`, + options: null + }, + expect: `Error: 404 no options!` } ]; @@ -134,17 +142,14 @@ describe(`ErrorMapService`, () => { }); service = TestBed.inject(ErrorMapService); + + spyOn(console, `warn`); }); for (let date of testData) { let test = date.test; it(date.title, () => { - expect( - service.getCleanErrorMessage(test.message, { - url: test.url, - data: test.data - }) - ).toEqual(date.expect); + expect(service.getCleanErrorMessage(test.message, test.options)).toEqual(date.expect); }); if (date.testConditionCheck !== undefined) { it(`Conditions for '${date.title}' are present`, () => { @@ -152,4 +157,23 @@ describe(`ErrorMapService`, () => { }); } } + + it(`test with value that matches multiple regexes`, () => { + UrlFragmentToHttpErrorMap.set( + `test123456789`, + new ErrorMap([ + [/1, 2, 3, [0-9,\s]+/, `Number range begins with 1`], + [/[0-9,\s] 7, 8, 9+/, `Number range ends with 9`] + ]) + ); + + expect(service.getCleanErrorMessage(`1, 2, 3, 4, 5, 6, 7, 8, 9`, { url: `test123456789` })).toBe( + `Error: Number range begins with 1` + ); + expect(console.warn).toHaveBeenCalledOnceWith( + `ErrorMapService has found multiple matches for "1, 2, 3, 4, 5, 6, 7, 8, 9"` + ); + + UrlFragmentToHttpErrorMap.delete(`test123456789`); + }); }); diff --git a/client/src/app/gateways/storage.service.spec.ts b/client/src/app/gateways/storage.service.spec.ts index 87bd52fbe9..80bc74b9c2 100644 --- a/client/src/app/gateways/storage.service.spec.ts +++ b/client/src/app/gateways/storage.service.spec.ts @@ -7,6 +7,8 @@ import { StorageService } from './storage.service'; class MockLocalStorage { public storage: { [key: string]: any } = {}; + public returnFalse = false; + private tick = interval(2); private current = 0; @@ -38,8 +40,14 @@ class MockLocalStorage { const current = this.current; return this.tick.pipe( takeWhile(time => time < current + 10), - map(getValueFn) - ); + map(getValueFn), + map(val => { + if (typeof val === `boolean` && this.returnFalse) { + return false; + } + return val; + }) + ) as Observable; } } @@ -84,4 +92,45 @@ describe(`StorageService`, () => { await service.clear(); expect(localStorage.storage).toEqual({}); }); + + it(`check if addNoClearKey works`, async () => { + localStorage.storage = { + example: `Something something text text text`, + anotherExample: `Another something text` + }; + service.addNoClearKey(`anotherExample`); + await service.clear(); + expect(localStorage.storage).toEqual({ + anotherExample: `Another something text` + }); + }); + + it(`check set with null`, async () => { + localStorage.storage = { + example: `Something something text text text`, + anotherExample: `Another something text` + }; + await service.set(`example`, null); + expect(localStorage.storage).toEqual({ + anotherExample: `Another something text` + }); + }); + + it(`check set with undefined`, async () => { + localStorage.storage = { + example: `Something something text text text`, + anotherExample: `Another something text` + }; + await service.set(`example`, undefined); + expect(localStorage.storage).toEqual({ + anotherExample: `Another something text` + }); + }); + + it(`check error cases`, async () => { + localStorage.returnFalse = true; + await expectAsync(service.set(`example`, `Something new`)).toBeRejectedWithError(`Could not set the item.`); + await expectAsync(service.remove(`example`)).toBeRejectedWithError(`Could not delete the item.`); + await expectAsync(service.clear()).toBeRejectedWithError(`Could not clear the storage.`); + }); });