-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoggle-provider.test.ts
181 lines (163 loc) · 5.58 KB
/
toggle-provider.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
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
import { join } from 'path';
import { readFile } from 'fs/promises';
import nock from 'nock';
import { UnleashToggleProvider, BootstrapOptions, TogglesNotInitializedError } from '../index';
const config = {
url: 'https://toggles.test.url',
appName: 'test',
instanceId: 'test',
apiToken: 'test.token',
refreshInterval: 0, // disable interval fetch that blocks tests exit
disableMetrics: true, // disable interval fetch that blocks tests exit
logger: {
appName: 'toggle-provider.test',
logLevel: 'error',
logStyle: 'cli',
},
};
const bootstrap: BootstrapOptions = {
data: [
{
enabled: true,
name: 'Enabled testing feature',
description: '',
project: 'default',
stale: false,
type: 'release',
variants: [],
strategies: [],
impressionData: false,
},
{
enabled: false,
name: 'Disabled testing feature',
description: '',
project: 'default',
stale: false,
type: 'release',
variants: [],
strategies: [],
impressionData: false,
},
],
};
const toggleProvider = new UnleashToggleProvider(config);
const mockUnleashServer = () => {
nock(`${config.url}`).post('/client/register').reply(200, {});
nock(`${config.url}`).get('/client/features').reply(200, { features: [] });
};
describe('Toggle Provider', () => {
afterEach(() => {
toggleProvider.destroy();
nock.abortPendingRequests();
nock.cleanAll();
});
it('should throw an error when checking features before initializing', () => {
expect(() => toggleProvider.isEnabled('Disabled testing feature')).toThrow(TogglesNotInitializedError);
expect(() => toggleProvider.onChange(() => {})).toThrow(TogglesNotInitializedError);
});
describe('isEnabled', () => {
it('should recognize enabled feature as enabled', async () => {
mockUnleashServer();
await toggleProvider.init(bootstrap);
const isEnabled = toggleProvider.isEnabled('Enabled testing feature');
expect(isEnabled).toBe(true);
});
it('should recognize existing disabled feature as disabled', async () => {
mockUnleashServer();
await toggleProvider.init(bootstrap);
const isEnabled = toggleProvider.isEnabled('Disabled testing feature');
expect(isEnabled).toBe(false);
});
it('should recognize missing feature as disabled', async () => {
mockUnleashServer();
await toggleProvider.init(bootstrap);
const isEnabled = toggleProvider.isEnabled('Non-existing testing feature');
expect(isEnabled).toBe(false);
});
});
describe('onChange', () => {
it('should trigger callback function on a change to feature inventory', async () => {
mockUnleashServer();
const mockCallback = jest.fn();
const quickToggleProvider = new UnleashToggleProvider({ ...config, refreshInterval: 1500 });
await quickToggleProvider.init(bootstrap);
quickToggleProvider.onChange(mockCallback);
nock(`${config.url}`)
.get('/client/features')
.reply(200, {
features: [
{
enabled: true,
name: 'Changed testing feature',
description: '',
project: 'default',
stale: false,
type: 'release',
variants: [],
strategies: [],
impressionData: false,
},
],
});
const waiter = new Promise((resolve) => {
setTimeout(resolve, 3000);
});
await waiter;
quickToggleProvider.destroy();
expect(mockCallback).toBeCalledTimes(2); // 1 for bootstrap 1 for actual change
});
});
describe('syncOpenApiSpec', () => {
[
{
name: 'should keep object items with enabled feature toggle',
inputFixture: 'openapi.object.input.json',
outputFixture: 'openapi.object.input.json', // updated spec should equal input
sampleFeatureToggleEnabled: true,
},
{
name: 'should remove object items with disabled feature toggle',
inputFixture: 'openapi.object.input.json',
outputFixture: 'openapi.object.output.json',
sampleFeatureToggleEnabled: false,
},
{
name: 'should keep array items with enabled feature toggle',
inputFixture: 'openapi.array.input.json',
outputFixture: 'openapi.array.input.json', // updated spec should equal input
sampleFeatureToggleEnabled: true,
},
{
name: 'should remove array items with disabled feature toggle',
inputFixture: 'openapi.array.input.json',
outputFixture: 'openapi.array.output.json',
sampleFeatureToggleEnabled: false,
},
].forEach(({ name, inputFixture, outputFixture, sampleFeatureToggleEnabled }) => {
it(name, async () => {
const input = JSON.parse(await readFile(join(__dirname, 'fixtures', inputFixture), 'utf8'));
const output = JSON.parse(await readFile(join(__dirname, 'fixtures', outputFixture), 'utf8'));
const boot: BootstrapOptions = {
data: [
{
enabled: sampleFeatureToggleEnabled,
name: 'sample-feature-toggle',
description: '',
project: 'default',
stale: false,
type: 'release',
variants: [],
strategies: [],
impressionData: false,
},
],
};
mockUnleashServer();
await toggleProvider.init(boot);
const updatedOpenApiSpec = toggleProvider.syncOpenApiSpec(input);
expect(updatedOpenApiSpec).toStrictEqual(output);
});
});
});
});