-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclientSpecification.test.ts
55 lines (50 loc) · 1.48 KB
/
clientSpecification.test.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
import clientSpecification from "@unleash/client-specification/specifications/index.json";
import { evaluateFlags } from "./evaluateFlags";
import { flagsClient } from "./flagsClient";
const files = clientSpecification.map((file) =>
require(`@unleash/client-specification/specifications/${file}`)
) as Array<{
state: any;
tests?: Array<{
description: string;
context: any;
toggleName: string;
expectedResult: boolean;
}>;
variantTests?: Array<{
description: string;
context: any;
toggleName: string;
expectedResult: any;
}>;
}>;
// Test `evaluateFlags` and `flagsClient` together
// using @unleash/client-specification
describe.each(files)("$name", ({ state, tests, variantTests }) => {
beforeAll(() => {
vi.stubGlobal("console", { warn: vi.fn(), error: vi.fn() });
});
afterAll(() => {
vi.unstubAllGlobals();
});
if (tests) {
test.each(tests)(
"$description",
({ context, toggleName, expectedResult }) => {
const { toggles } = evaluateFlags(state, context);
const result = flagsClient(toggles).isEnabled(toggleName);
expect(result).toBe(expectedResult);
}
);
}
if (variantTests) {
test.each(variantTests)(
"$description",
({ context, toggleName, expectedResult }) => {
const { toggles } = evaluateFlags(state, context);
const result = flagsClient(toggles).getVariant(toggleName);
expect(result).toStrictEqual(expectedResult);
}
);
}
});