-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock.test.js
More file actions
95 lines (80 loc) · 3.17 KB
/
mock.test.js
File metadata and controls
95 lines (80 loc) · 3.17 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
/* eslint-disable jest/no-conditional-expect */
import { OpenFeature } from '@openfeature/web-sdk';
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
describe('OpenFeature Split Provider - Mock Integration Tests', () => {
let client;
let mockSplitClient;
let provider;
beforeEach(() => {
// Create a fully mocked Split client that returns predictable values
mockSplitClient = {
// Mock Split client methods
ready: jest.fn(() => true),
on: jest.fn((event, callback) => {
if (event === 'SDK_READY') {
// Immediately call the callback to simulate ready state
callback();
}
return { id: 'mock-listener' };
}),
Event: { SDK_READY: 'SDK_READY' },
__getStatus: () => ({isReady: true}),
// Mock treatment evaluation methods
getTreatment: jest.fn((splitName) => {
if (splitName === 'my_feature') return 'on';
if (splitName === 'some_other_feature') return 'off';
if (splitName === 'int_feature') return '32';
if (splitName === 'obj_feature') return '{"key": "value"}';
return 'control';
}),
// Mock for cleanup
destroy: jest.fn(() => Promise.resolve())
};
// Create the provider with our mock Split client
provider = new OpenFeatureSplitProvider({ client: () => mockSplitClient});
// Register with OpenFeature
OpenFeature.setProviderAndWait(provider);
OpenFeature.setContext({ targetingKey: 'user1' })
// Get the client
client = OpenFeature.getClient('mock-test');
});
afterEach(async () => {
await mockSplitClient.destroy();
});
test('boolean evaluation should work', async () => {
const result = await client.getBooleanValue('my_feature', false);
expect(result).toBe(true);
expect(mockSplitClient.getTreatment).toHaveBeenCalledWith('my_feature', {});
});
test('boolean evaluation should handle off value', async () => {
const result = await client.getBooleanValue('some_other_feature', true);
expect(result).toBe(false);
});
test('string evaluation should work', async () => {
const result = await client.getStringValue('some_other_feature', 'default');
expect(result).toBe('off');
});
test('number evaluation should work', async () => {
const result = await client.getNumberValue('int_feature', 0);
expect(result).toBe(32);
});
test('object evaluation should work', async () => {
const result = await client.getObjectValue('obj_feature', {});
expect(result).toEqual({ key: 'value' });
});
test('boolean details should include metadata', async () => {
const details = await client.getBooleanDetails('my_feature', false);
expect(details.value).toBe(true);
expect(details.variant).toBe('on');
expect(details.flagKey).toBe('my_feature');
expect(details.reason).toBe('TARGETING_MATCH');
});
test('control treatment should be handled correctly', async () => {
try {
provider.resolveBooleanEvaluation('non_existent_feature', false, { targetingKey: 'user1' });
} catch (error) {
expect(error.name).toBe('FlagNotFoundError');
expect(error.message).toContain('control');
}
});
});