-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathglobal.test.ts
105 lines (94 loc) · 2.52 KB
/
global.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
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
import test from 'ava';
import * as nock from 'nock';
import { tmpdir } from 'os';
import { join } from 'path';
import { mkdirp } from 'mkdirp';
import { writeFileSync } from 'fs';
import { Unleash } from '../unleash';
import { initialize, isEnabled, destroy } from '../index';
function getRandomBackupPath() {
const path = join(tmpdir(), `test-tmp-${Math.round(Math.random() * 100000)}`);
mkdirp.sync(path);
return path;
}
const defaultToggles = [
{
name: 'feature',
enabled: true,
strategies: [],
},
];
let counter = 0;
function mockNetwork(toggles = defaultToggles) {
counter += 1;
const url = `http://unleash-${counter}.app`;
nock(url).get('/client/features').reply(200, { features: toggles });
return url;
}
test('should be able to call api', (t) => {
const url = mockNetwork();
initialize({
appName: 'foo',
metricsInterval: 0,
url,
backupPath: getRandomBackupPath(),
}).on('error', (err) => {
throw err;
});
t.true(isEnabled('unknown') === false);
destroy();
});
test('should load if backup file is corrupted', (t) =>
new Promise((resolve) => {
const url = mockNetwork();
const backupPath = join(tmpdir());
const backupFile = join(backupPath, `/unleash-backup-with-corrupted-JSON.json`);
writeFileSync(backupFile, '{broken-json');
const instance = new Unleash({
appName: 'with-corrupted-JSON',
metricsInterval: 0,
url,
backupPath,
refreshInterval: 0,
});
instance
.on('error', (err) => {
destroy();
throw err;
})
.on('warn', (err) => {
if (err?.message?.includes('Unleash storage failed parsing file')) {
destroy();
t.pass();
resolve();
}
});
}));
// FIXME: This test is flaky
test.skip('should be able to call isEnabled eventually', (t) =>
new Promise((resolve) => {
const url = mockNetwork();
initialize({
appName: 'foo',
metricsInterval: 0,
url,
backupPath: getRandomBackupPath(),
})
.on('error', (err) => {
throw err;
})
.on('synchronized', async () => {
t.true(isEnabled('feature') === true);
t.pass();
resolve();
destroy();
});
t.true(isEnabled('feature') === false);
}));
test('should return fallbackValue if init was not called', (t) =>
new Promise((resolve) => {
t.true(isEnabled('feature') === false);
t.true(isEnabled('feature', {}, false) === false);
t.true(isEnabled('feature', {}, true) === true);
resolve();
}));