-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathlazy-init.spec.js
More file actions
95 lines (77 loc) · 3.84 KB
/
lazy-init.spec.js
File metadata and controls
95 lines (77 loc) · 3.84 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
import { SplitFactory as SplitFactorySS } from '../../factory/node';
import { SplitFactory as SplitFactoryCS } from '../../factory/browser';
// Tests should finish without dangling timers or requests
export default function (settings, fetchMock, t) {
t.test('Server-side', async (assert) => {
let splitio;
for (let i = 0; i < 100; i++) {
splitio = SplitFactorySS({
core: {
authorizationKey: 'fake-token-' + i,
},
urls: {
sdk: 'https://not-called/api',
events: 'https://not-called/api',
auth: 'https://not-called/api',
}
}, (modules) => {
modules.lazyInit = true;
});
const manager = splitio.manager();
assert.deepEqual(manager.names(), [], 'We should not have done any request yet');
const client = splitio.client();
assert.equal(client.getTreatment('user-1', 'split_test'), 'control', 'We should get control');
assert.equal(client.track('user-1', 'user', 'my_event'), true, 'We should track the event');
}
fetchMock.getOnce('https://not-called/api/splitChanges?s=1.1&since=-1', { status: 200, body: { splits: [], since: -1, till: 1457552620999 } });
fetchMock.getOnce('https://not-called/api/splitChanges?s=1.1&since=1457552620999', { status: 200, body: { splits: [], since: 1457552620999, till: 1457552620999 } });
fetchMock.postOnce('https://not-called/api/testImpressions/bulk', 200);
fetchMock.postOnce('https://not-called/api/events/bulk', 200);
// Validate that init and destroy are idempotent
for (let i = 0; i < 3; i++) { splitio.init(); splitio.init(); splitio.destroy(); splitio.destroy(); }
splitio.init();
await splitio.client().ready();
assert.true(splitio.client().__getStatus().isReady, 'Split SDK is ready');
await splitio.destroy();
assert.end();
});
t.test('Client-side', async (assert) => {
let splitio;
for (let i = 0; i < 100; i++) {
splitio = SplitFactoryCS({
core: {
authorizationKey: 'fake-token-' + i,
key: 'user-' + i,
},
urls: {
sdk: 'https://not-called/api',
events: 'https://not-called/api',
auth: 'https://not-called/api',
}
}, (modules) => {
modules.lazyInit = true;
});
const manager = splitio.manager();
assert.deepEqual(manager.names(), [], 'We should not have done any request yet');
const client = splitio.client();
assert.equal(client.getTreatment('split_test'), 'control', 'We should get control');
assert.equal(client.track('user', 'my_event'), true, 'We should track the event');
const otherClient = splitio.client('other-user');
assert.equal(otherClient.getTreatment('split_test'), 'control', 'We should get control');
assert.equal(otherClient.track('user', 'my_event'), true, 'We should track the event');
}
fetchMock.getOnce('https://not-called/api/splitChanges?s=1.2&since=-1', { status: 200, body: { splits: [], since: -1, till: 1457552620999 } });
fetchMock.getOnce('https://not-called/api/splitChanges?s=1.2&since=1457552620999', { status: 200, body: { splits: [], since: 1457552620999, till: 1457552620999 } });
fetchMock.getOnce('https://not-called/api/memberships/user-99', { status: 200, body: {} });
fetchMock.getOnce('https://not-called/api/memberships/other-user', { status: 200, body: {} });
fetchMock.postOnce('https://not-called/api/testImpressions/bulk', 200);
fetchMock.postOnce('https://not-called/api/events/bulk', 200);
// Validate that init and destroy are idempotent
for (let i = 0; i < 3; i++) { splitio.init(); splitio.init(); splitio.destroy(); splitio.destroy(); }
splitio.init();
await splitio.client().ready();
assert.true(splitio.client().__getStatus().isReady, 'Split SDK is ready');
await splitio.destroy();
assert.end();
});
}