-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathindex.test.ts
89 lines (82 loc) · 2.43 KB
/
index.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
import test from 'ava';
import * as nock from 'nock';
import {
initialize,
isEnabled,
Strategy,
destroy,
getFeatureToggleDefinition,
getFeatureToggleDefinitions,
count,
countVariant,
getVariant,
startUnleash,
} from '../index';
let counter = 1;
const getUrl = () => {
const url = `http://test${counter}.app/`;
counter += 1;
return url;
};
const metricsUrl = '/client/metrics';
const nockMetrics = (url: string, code = 200) => nock(url).post(metricsUrl).reply(code, '');
const registerUrl = '/client/register';
const nockRegister = (url: string, code = 200) => nock(url).post(registerUrl).reply(code, '');
const nockFeatures = (url: string, code = 200) =>
nock(url).get('/client/features').reply(code, { features: [] });
test('should load main module', (t) => {
t.truthy(initialize);
t.truthy(startUnleash);
t.truthy(isEnabled);
t.truthy(Strategy);
t.truthy(destroy);
t.truthy(countVariant);
t.truthy(getVariant);
t.truthy(getFeatureToggleDefinition);
t.truthy(getFeatureToggleDefinitions);
t.truthy(count);
});
test('initialize should init with valid options', (t) => {
const url = getUrl();
nockMetrics(url);
nockRegister(url);
t.notThrows(() => initialize({ appName: 'my-app-name', url }));
destroy();
});
test('should call methods', (t) => {
const url = getUrl();
nockMetrics(url);
nockRegister(url);
t.notThrows(() => initialize({ appName: 'my-app-name', url }));
t.snapshot(isEnabled('some-feature'));
t.snapshot(getFeatureToggleDefinition('some-feature'));
t.snapshot(getVariant('some-feature'));
t.snapshot(count('some-feature', true));
t.snapshot(countVariant('some-feature', 'variant1'));
destroy();
});
test('should not return feature-toggle definition if there is no instance', (t) => {
// @ts-expect-error
t.is(getFeatureToggleDefinition(), undefined);
});
test.serial('should start unleash with promise', async (t) => {
const url = getUrl();
nockFeatures(url);
nockMetrics(url);
nockRegister(url);
const unleash = await startUnleash({ appName: 'my-app-name', url });
t.truthy(unleash);
destroy();
});
test.serial('should start unleash with promise multiple times', async (t) => {
const url = getUrl();
nockFeatures(url);
nockMetrics(url);
nockRegister(url);
const config = { appName: 'my-app-name', url };
const unleash1 = await startUnleash(config);
t.truthy(unleash1);
const unleash2 = await startUnleash(config);
t.truthy(unleash2);
destroy();
});