forked from open-feature/js-sdk-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflipt-web-provider.spec.ts
89 lines (73 loc) · 3.35 KB
/
flipt-web-provider.spec.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { GeneralError, TypeMismatchError } from '@openfeature/web-sdk';
import { FliptWebProvider } from './flipt-web-provider';
import fs from 'fs';
import path from 'path';
describe('FliptWebProvider', () => {
const endpoint = 'http://localhost:8080';
const pathToData = path.resolve(__dirname, '..', '__mocks__/state.json');
const data = JSON.parse(fs.readFileSync(pathToData, 'utf-8'));
const fetcher = (): Promise<Response> => {
return Promise.resolve(new Response(JSON.stringify(data)));
};
let provider: FliptWebProvider;
beforeEach(async () => {
provider = new FliptWebProvider('default', { url: endpoint, fetcher });
await provider.initialize();
});
it('should be and instance of FliptWebProvider', () => {
expect(provider).toBeInstanceOf(FliptWebProvider);
});
describe('method resolveStringEvaluation', () => {
it('should throw general error for non-existent flag', () => {
expect(() => {
provider.resolveStringEvaluation('nonExistent', 'default', { targetingKey: '1234', fizz: 'buzz' });
}).toThrow(GeneralError);
});
it('should return right value if key exists', () => {
const value = provider.resolveStringEvaluation('flag_string', 'default', { targetingKey: '1234', fizz: 'buzz' });
expect(value).toHaveProperty('value', 'variant1');
expect(value).toHaveProperty('reason', 'TARGETING_MATCH');
});
});
describe('method resolveNumberEvaluation', () => {
it('should throw general error for non-existent flag', () => {
expect(() => {
provider.resolveNumberEvaluation('nonExistent', 1, { targetingKey: '1234', fizz: 'buzz' });
}).toThrow(GeneralError);
});
it('should return right value if key exists', () => {
const value = provider.resolveNumberEvaluation('flag_number', 0, { targetingKey: '1234', fizz: 'buzz' });
expect(value).toHaveProperty('value', 5);
expect(value).toHaveProperty('reason', 'TARGETING_MATCH');
});
});
describe('method resolveBooleanEvaluation', () => {
it('should throw general error for non-existent flag', () => {
expect(() => {
provider.resolveBooleanEvaluation('nonExistent', false, { targetingKey: '1234', fizz: 'buzz' });
}).toThrow(GeneralError);
});
it('should return right value if key exists', () => {
const value = provider.resolveBooleanEvaluation('flag_boolean', false, { targetingKey: '1234', fizz: 'buzz' });
expect(value).toHaveProperty('value', true);
expect(value).toHaveProperty('reason', 'TARGETING_MATCH');
});
});
describe('method resolveObjectEvaluation', () => {
it('should throw general error for non-existent flag', () => {
expect(() => {
provider.resolveObjectEvaluation('nonExistent', {}, { targetingKey: '1234', fizz: 'buzz' });
}).toThrow(GeneralError);
});
it('should return right value if key exists', () => {
const value = provider.resolveObjectEvaluation('flag_object', { fizz: 'buzz' }, { targetingKey: '1234', fizz: 'buzz' });
expect(value).toHaveProperty('value', { foo: 'bar' });
expect(value).toHaveProperty('reason', 'TARGETING_MATCH');
});
});
it('should throw TypeMismatchError on non-number value', () => {
expect(() => {
provider.resolveNumberEvaluation('flag_string', 0, { targetingKey: '1234', fizz: 'buzz' });
}).toThrow(TypeMismatchError);
});
});