-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprovider.spec.js
More file actions
241 lines (211 loc) · 8.55 KB
/
provider.spec.js
File metadata and controls
241 lines (211 loc) · 8.55 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* eslint-disable jest/no-conditional-expect */
import { getLocalHostSplitClient, getSplitFactory } from '../testUtils';
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
const cases = [
[
'provider tests mode: splitClient',
() => ({ splitClient: getLocalHostSplitClient()}),
],
[
'provider tests mode: splitFactory',
getSplitFactory
],
];
describe.each(cases)('%s', (label, getOptions) => {
let provider;
let options;
beforeEach(() => {
options = getOptions();
provider = new OpenFeatureSplitProvider(options);
});
afterEach(async () => {
jest.clearAllMocks()
await provider.onClose();
});
test('evaluate Boolean null/empty test', async () => {
try {
await provider.resolveBooleanEvaluation('', false, { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('flagKey must be a non-empty string');
expect(e.code).toBe('FLAG_NOT_FOUND');
}
});
test('evaluate Boolean control test', async () => {
try {
await provider.resolveBooleanEvaluation('non-existent-feature', false, { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('Received the "control" value from Split.');
expect(e.code).toBe('FLAG_NOT_FOUND');
}
});
test('evaluate Boolean true test', async () => {
const details = await provider.resolveBooleanEvaluation('my_feature', false, { targetingKey: 'key' });
expect(details.value).toBe(true);
expect(details.variant).toBe('on');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to ON treatment"}' });
});
test('evaluate Boolean on test', async () => {
const details = await provider.resolveBooleanEvaluation('my_feature', true, { targetingKey: 'key' });
expect(details.value).toBe(true);
expect(details.variant).toBe('on');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to ON treatment"}' });
});
test('evaluate Boolean false test', async () => {
const details = await provider.resolveBooleanEvaluation('some_other_feature', true, { targetingKey: 'user1' });
expect(details.value).toBe(false);
expect(details.variant).toBe('off');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.flagMetadata).toEqual({ config: '' });
});
test('evaluate Boolean off test', async () => {
const details = await provider.resolveBooleanEvaluation('some_other_feature', false, { targetingKey: 'user1' });
expect(details.value).toBe(false);
expect(details.variant).toBe('off');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.flagMetadata).toEqual({ config: '' });
});
test('evaluate Boolean error test', async () => {
try {
await provider.resolveBooleanEvaluation('int_feature', false, { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('Invalid boolean value for 32');
expect(e.code).toBe('PARSE_ERROR');
}
});
test('evaluate String null/empty test', async () => {
try {
await provider.resolveStringEvaluation('', 'default', { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('flagKey must be a non-empty string');
expect(e.code).toBe('FLAG_NOT_FOUND');
}
});
test('evaluate String control test', async () => {
try {
await provider.resolveStringEvaluation('non-existent-feature', 'default', { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('Received the "control" value from Split.');
expect(e.code).toBe('FLAG_NOT_FOUND');
}
});
test('evaluate String regular test', async () => {
try {
await provider.resolveStringEvaluation('string_feature', 'default', { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('Received the "control" value from Split.');
expect(e.code).toBe('FLAG_NOT_FOUND');
}
});
test('evaluate String error test', async () => {
try {
await provider.resolveStringEvaluation('int_feature', 'default', { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('Invalid string value for 32');
expect(e.code).toBe('PARSE_ERROR');
}
});
test('evaluate Number null/empty test', async () => {
try {
await provider.resolveNumberEvaluation('', 0, { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('flagKey must be a non-empty string');
expect(e.code).toBe('FLAG_NOT_FOUND');
}
});
test('evaluate Number control test', async () => {
try {
await provider.resolveNumberEvaluation('non-existent-feature', 0, { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('Received the "control" value from Split.');
expect(e.code).toBe('FLAG_NOT_FOUND');
}
});
test('evaluate Number regular test', async () => {
const details = await provider.resolveNumberEvaluation('int_feature', 0, { targetingKey: 'user1' });
expect(details.value).toBe(32);
expect(details.variant).toBe('32');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to number treatment"}' });
});
test('evaluate Number error test', async () => {
try {
await provider.resolveNumberEvaluation('my_feature', 0, { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('Invalid numeric value off');
expect(e.code).toBe('PARSE_ERROR');
}
});
test('evaluate Structure null/empty test', async () => {
try {
await provider.resolveObjectEvaluation('', {}, { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('flagKey must be a non-empty string');
expect(e.code).toBe('FLAG_NOT_FOUND');
}
});
test('evaluate Structure control test', async () => {
try {
await provider.resolveObjectEvaluation('non-existent-feature', {}, { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('Received the "control" value from Split.');
expect(e.code).toBe('FLAG_NOT_FOUND');
}
});
test('evaluate Structure regular test', async () => {
const details = await provider.resolveObjectEvaluation('obj_feature', {}, { targetingKey: 'user1' });
expect(details.value).toEqual({ key: 'value' });
expect(details.variant).toBe('{"key": "value"}');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to obj treatment"}' });
});
test('evaluate Structure error test', async () => {
try {
await provider.resolveObjectEvaluation('int_feature', {}, { targetingKey: 'user1' });
} catch (e) {
expect(e.message).toBe('Error parsing 32 as JSON, ParseError: Flag value 32 had unexpected type number, expected "object"');
expect(e.code).toBe('PARSE_ERROR');
}
});
test('track: throws when missing eventName', async () => {
try {
await provider.track('', { targetingKey: 'u1', trafficType: 'user' }, {});
} catch (e) {
expect(e.message).toBe('Missing eventName, required to track');
expect(e.code).toBe('PARSE_ERROR');
}
});
test('track: throws when missing trafficType', async () => {
try {
await provider.track('evt', { targetingKey: 'u1' }, {});
} catch (e) {
expect(e.message).toBe('Missing trafficType variable, required to track');
expect(e.code).toBe('INVALID_CONTEXT');
}
});
test('track: throws when missing targetingKey', async () => {
try {
await provider.track('evt', { trafficType: 'user' }, {});
} catch (e) {
expect(e.message).toBe('Missing targetingKey, required to track');
expect(e.code).toBe('TARGETING_KEY_MISSING');
}
});
test('track: ok without details', async () => {
const trackSpy = jest.spyOn(options.splitClient ? options.splitClient : options.client(), 'track');
await provider.track('view', { targetingKey: 'u1', trafficType: 'user' }, null);
expect(trackSpy).toHaveBeenCalledTimes(1);
expect(trackSpy).toHaveBeenCalledWith('u1', 'user', 'view', undefined, {});
});
test('track: ok with details', async () => {
const trackSpy = jest.spyOn(options.splitClient ? options.splitClient : options.client(), 'track');
await provider.track(
'purchase',
{ targetingKey: 'u1', trafficType: 'user' },
{ value: 9.99, properties: { plan: 'pro', beta: true } }
);
expect(trackSpy).toHaveBeenCalledTimes(1);
expect(trackSpy).toHaveBeenCalledWith('u1', 'user', 'purchase', 9.99, { plan: 'pro', beta: true });
});
});