Skip to content

Commit

Permalink
Improve test coverage (#2525)
Browse files Browse the repository at this point in the history
  • Loading branch information
luisa-beerboom authored Jul 14, 2023
1 parent 7c37b4b commit c419a3d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 23 deletions.
66 changes: 45 additions & 21 deletions client/src/app/gateways/error-mapping/error-map.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ 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))
},
{
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:
Expand All @@ -46,15 +46,15 @@ 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.`
},
{
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:
Expand All @@ -66,45 +66,45 @@ 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.`)
},
{
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.`)
},
{
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.`)
},
{
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:
Expand All @@ -114,17 +114,25 @@ 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!`
},
{
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!`
}
];

Expand All @@ -134,22 +142,38 @@ 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`, () => {
expect(date.testConditionCheck).toBe(true);
});
}
}

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`);
});
});
53 changes: 51 additions & 2 deletions client/src/app/gateways/storage.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T>;
}
}

Expand Down Expand Up @@ -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.`);
});
});

0 comments on commit c419a3d

Please sign in to comment.