-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdoTesting.ts
60 lines (49 loc) · 1.75 KB
/
doTesting.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { join } from 'path';
import { BaseMock, readMock } from './readMocks';
const telegarfi18n = require('telegraf-i18n');
interface ToBeTested {
name: string;
toTest: Function;
location: string;
translation?: boolean;
}
interface LocaleToBeTested extends BaseMock {
toTest: Function;
translation?: boolean;
}
interface TheTest extends LocaleToBeTested {
input: any;
output: any;
kind: string;
label: string;
}
export const i18n = new telegarfi18n({
useSession: true,
allowMissing: true,
defaultLanguage: 'en',
directory: join(__dirname, '../others/locales')
});
const doTheTest = ({ label, locale, toTest, input, output, kind, translation }: TheTest): void => {
const curriedTranslation = {
t: ((resourceKey, templateData) => i18n.t(locale, resourceKey, templateData))
};
const value = toTest({ translate: (true === translation) ? curriedTranslation : null, ...input });
if ('await' === kind) {
return test(label, async () => {
expect.assertions(1);
expect(await value).toEqual(output);
});
} if ('await/throw' === kind) {
return test(label, () => {
expect(value).rejects.toThrow(output);
});
}
return test(label, () => expect(value).toEqual(output));
};
const localeTesting = ({ locale, mocks, ...remaining }: LocaleToBeTested): void => {
describe(locale, () => mocks.map(input => doTheTest({ locale, mocks, ...remaining, ...input })));
};
export const doTesting = ({ location, toTest, name, translation = false }: ToBeTested): void => {
const file = readMock({ location, toTest: name });
describe(`Testing ${toTest.name}`, () => file.map(input => localeTesting({ toTest, translation, ...input })));
};