feature-toggle-api 3.4.2 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +115 -96
  2. package/dist/feature-toggle.d.ts +120 -0
  3. package/dist/feature-toggle.js +388 -0
  4. package/dist/feature-toggle.min.cjs +2 -0
  5. package/dist/feature-toggle.min.cjs.map +1 -0
  6. package/dist/feature-toggle.min.js +2 -0
  7. package/dist/feature-toggle.min.js.map +1 -0
  8. package/dist/feature-toggle.umd.min.js +2 -0
  9. package/dist/feature-toggle.umd.min.js.map +1 -0
  10. package/dist/html-plugin.umd.min.js +2 -0
  11. package/dist/html-plugin.umd.min.js.map +1 -0
  12. package/dist/url-plugin.umd.min.js +2 -0
  13. package/dist/url-plugin.umd.min.js.map +1 -0
  14. package/examples/01_basic_esmodule.js +10 -0
  15. package/examples/02_basic_cjsmodule.cjs +6 -0
  16. package/examples/{basic.html → 03_basic_scripttag.html} +3 -2
  17. package/examples/{example-htmlplugin.html → 04_example-htmlplugin.html} +2 -2
  18. package/examples/{example-urlplugin.html → 05_example-urlplugin.html} +2 -2
  19. package/examples/{withListener.html → 06_withListener.html} +1 -1
  20. package/package.json +32 -43
  21. package/rollup.config.js +107 -0
  22. package/src/{index.js → featureToggle.ts} +172 -51
  23. package/src/index.ts +10 -0
  24. package/src/plugins/htmlplugin/{plugin-html.js → plugin-html.ts} +22 -10
  25. package/src/plugins/urlplugin/{plugin-url.js → plugin-url.ts} +36 -10
  26. package/tests/featuretoggle.test.ts +407 -0
  27. package/tests/polyfills.ts +20 -0
  28. package/tsconfig.cjs.json +12 -0
  29. package/tsconfig.json +12 -0
  30. package/feature-toggle-api.js +0 -243
  31. package/feature-toggle-api.min.js +0 -1
  32. package/feature-toggle-api.module.js +0 -243
  33. package/spec/support/index.spec.js +0 -378
  34. package/spec/support/jasmine.json +0 -11
  35. package/spec/support/polyfills.js +0 -6
  36. package/src/index.cjs.js +0 -8
@@ -1,378 +0,0 @@
1
- const featureToggleApi = require('../../feature-toggle-api.module.js');
2
- const urlplugin = require('../../src/plugins/urlplugin/plugin-url.js');
3
- const window = require('./polyfills.js')
4
-
5
- describe("Initialisation / Basic Tests", function() {
6
- it("should return a function", function() {
7
- const type = typeof featureToggleApi;
8
- expect(type).toBe('function');
9
- });
10
-
11
- it("should return 'feature-toggle-api' for property name", function() {
12
- const api = new featureToggleApi();
13
- expect(api.name).toBe('feature-toggle-api');
14
- });
15
-
16
- it("should return true if feature is initialized in constructor", function() {
17
- const api = new featureToggleApi({ feature: true });
18
- expect(api.isVisible('feature')).toBe(true);
19
- });
20
-
21
- it("should return true if feature with variant is initialized in constructor", function() {
22
- const api = new featureToggleApi({ "feature#variant": true });
23
- expect(api.isVisible('feature', 'variant')).toBe(true);
24
- });
25
-
26
- it("should return true if feature with variant is initialized in constructor with function", function() {
27
- const api = new featureToggleApi({ "feature#variant": true });
28
- expect(api.isVisible('feature', 'variant')).toBe(true);
29
- });
30
- });
31
-
32
- describe("Basic visibility of features", function() {
33
- const api = new featureToggleApi();
34
-
35
- it("should return true if parameter is boolean", function() {
36
- api.visibility('featurebool', true);
37
- const visibility = api.isVisible('featurebool');
38
- expect(visibility).toBe(true);
39
- });
40
-
41
- it("should ignore case for the api-keys in constructor", function() {
42
- const api = new featureToggleApi({ ignoreCase: true });
43
- const visibility1 = api.isVisible('ignorecase');
44
- const visibility2 = api.isVisible('iGNOrEcaSe');
45
- expect(visibility1).toBe(true);
46
- expect(visibility2).toBe(true);
47
- });
48
-
49
- it("should ignore case for the api-keys", function() {
50
- api.visibility('ignoreCase', true);
51
- const visibility1 = api.isVisible('ignorecase');
52
- const visibility2 = api.isVisible('iGNOrEcaSe');
53
- expect(visibility1).toBe(true);
54
- expect(visibility2).toBe(true);
55
- });
56
-
57
- it("should return true if parameter is function", function() {
58
- api.visibility('featurebool', function() { return true });
59
- const visibility = api.isVisible('featurebool');
60
- expect(visibility).toBe(true);
61
- });
62
-
63
- it("should return true if feature is true", function() {
64
- api.visibility('featureTrue', true);
65
- const visibility = api.isVisible('featureTrue');
66
- expect(visibility).toBe(true);
67
- });
68
-
69
- it("should return false if feature is false", function() {
70
- api.visibility('featureFalse', false);
71
- const visibility = api.isVisible('featureFalse');
72
- expect(visibility).toBe(false);
73
- });
74
-
75
- it("should return false if feature does not exist", function() {
76
- const visibility = api.isVisible('featureNotExists');
77
- expect(visibility).toBe(false);
78
- });
79
-
80
- it("should return true if feature with variant is true", function() {
81
- api.visibility('featureTrue', 'variant', true);
82
- const visibility = api.isVisible('featureTrue', 'variant');
83
- expect(visibility).toBe(true);
84
- });
85
-
86
- it("should return false if feature with variant is false", function() {
87
- api.visibility('featureFalse', 'variant', false);
88
- const visibility = api.isVisible('featureFalse', 'variant');
89
- expect(visibility).toBe(false);
90
- });
91
-
92
- it("should return false if feature with variant does not exist", function() {
93
- const visibility = api.isVisible('featureNotExists', 'variant', false);
94
- expect(visibility).toBe(false);
95
- });
96
-
97
- it("should return true if feature with variant is requested but only featurerule exists", function() {
98
- api.visibility('featureTrue', true);
99
- const visibility = api.isVisible('featureTrue', 'variant');
100
- expect(visibility).toBe(true);
101
- });
102
-
103
- it("should return false if data returns false", function() {
104
- api.visibility('featureFalse', 'variant', function(rule) { return rule.data == 'succeed' });
105
- const visibility = api.isVisible('featureFalse', 'variant', 'fail');
106
- expect(visibility).toBe(false);
107
- });
108
-
109
- it("should return false if data returns true", function() {
110
- api.visibility('featureFalse', 'variant', function(rule) { return rule.data == 'succeed' });
111
- const visibility = api.isVisible('featureFalse', 'variant', 'succeed');
112
- expect(visibility).toBe(true);
113
- });
114
-
115
- it("should pass correct parameters for visibilityrule", function() {
116
- api.visibility('featureFalse2', 'variant', function(rule) {
117
- if (rule._internalCall)
118
- return;
119
- expect(rule.name).toBe('featureFalse2');
120
- expect(rule.variant).toBe('variant');
121
- expect(rule.data).toBe('succeed');
122
- });
123
-
124
- api.isVisible('featureFalse2', 'variant', 'succeed');
125
-
126
- });
127
- });
128
-
129
- describe("Default Visibility", function() {
130
- const api = new featureToggleApi();
131
- api.defaultVisibility((result) => {
132
- return true;
133
- });
134
- it("should return true if feature does not exist", function() {
135
- const visibility = api.isVisible('featureNotExists');
136
- expect(visibility).toBe(true);
137
- });
138
-
139
- it("should return true if feature withVariant does not exist", function() {
140
- const visibility = api.isVisible('featureNotExists', 'variant');
141
- expect(visibility).toBe(true);
142
- });
143
-
144
- it("should return false if feature is False", function() {
145
- api.visibility('featureFalse', false);
146
- const visibility = api.isVisible('featureFalse');
147
- expect(visibility).toBe(false);
148
- });
149
-
150
- it("should return false if feature with variant isFalse", function() {
151
- api.visibility('featureFalse2', false)
152
- const visibility = api.isVisible('featureFalse2', 'variant', false);
153
- expect(visibility).toBe(false);
154
- });
155
- });
156
-
157
- describe("Required Visibility", function() {
158
- const api = new featureToggleApi();
159
- api.requiredVisibility((result) => {
160
- return result.variant == "valid";
161
- });
162
-
163
- it("should return false if feature does not exist and requirerule matches", function() {
164
- api.visibility('featureNotExists', 'valid', false);
165
- const visibility = api.isVisible('featureNotExists', 'valid');
166
- expect(visibility).toBe(false);
167
- });
168
- it("should return false if feature does not exist and requirerule matches not", function() {
169
- api.visibility('featureNotExists', 'invalid', false);
170
- const visibility = api.isVisible('featureNotExists', 'invalid');
171
- expect(visibility).toBe(false);
172
- });
173
-
174
-
175
- it("should return false if only required rule matches", function() {
176
- api.visibility('featureFalse', false);
177
- const visibility = api.isVisible('featureFalse', 'valid');
178
- expect(visibility).toBe(false);
179
- });
180
-
181
- it("should return true if required rule matches and visibiltiyrule", function() {
182
- api.visibility('featureTrue', true);
183
- const visibility = api.isVisible('featureTrue', 'valid');
184
- expect(visibility).toBe(true);
185
- });
186
-
187
- it("should return false if defaultVisibility returns true", function() {
188
- api.defaultVisibility((result) => {
189
- return true;
190
- });
191
- const visibility = api.isVisible('featureTrue', 'invalid');
192
- expect(visibility).toBe(false);
193
- });
194
- });
195
-
196
- describe("Listener", function() {
197
- const api = new featureToggleApi({
198
- feature: true,
199
- "feature2#variant": true
200
- });
201
-
202
- it("should have an event called on('visibilityrule')", function() {
203
- api.on('visibilityrule', function() {
204
-
205
- });
206
- });
207
-
208
- it("should access function twice for two initial eventnames", function() {
209
- var listenedEvents = [];
210
-
211
- api.on('visibilityrule', function(event) {
212
- listenedEvents.push(event.name + "," + event.variant + "," + event.result);
213
- })
214
-
215
- expect(JSON.stringify(listenedEvents)).toBe(JSON.stringify(['feature,undefined,true', 'feature2,variant,true']));
216
- });
217
-
218
- it("should access function twice if visibilityrule is added before listener", function() {
219
- var listenedEvents = [];
220
- var localapi = new featureToggleApi({ feature: true });
221
- localapi.visibility("feature2", "variant", true);
222
- localapi.on('visibilityrule', function(event) {
223
- listenedEvents.push(event.name + "," + event.variant + "," + event.result);
224
- })
225
-
226
- expect(JSON.stringify(listenedEvents)).toBe(JSON.stringify(['feature,undefined,true', 'feature2,variant,true']));
227
- });
228
-
229
- it("should access function twice if visibilityrule is added after listener", function() {
230
- var listenedEvents = [];
231
- var localapi = new featureToggleApi({ feature: true });
232
- localapi.on('visibilityrule', function(event) {
233
- listenedEvents.push(event.name + "," + event.variant + "," + event.result);
234
- });
235
- localapi.visibility("feature2", "variant", true);
236
-
237
- expect(JSON.stringify(listenedEvents)).toBe(JSON.stringify(['feature,undefined,true', 'feature2,variant,true']));
238
- });
239
-
240
- it("should pass parameters as an object with result,name,variant,data", function() {
241
- var api = new featureToggleApi();
242
- api.visibility('feature', 'variant', 'gruempfel', function(rule) {
243
- return (rule.data + rule.name + rule.variant) == 'gruempfelfeaturevariant';
244
- });
245
-
246
- api.on('visibilityrule', function(event) {
247
- expect(event.result).toBe(true);
248
- expect(event.name).toBe("feature");
249
- expect(event.variant).toBe("variant");
250
- expect(event.data).toBe("gruempfel");
251
- })
252
- });
253
-
254
- it("should be executed, when data has changed", function() {
255
- var api = new featureToggleApi();
256
- var totalData = '';
257
-
258
- api.on('visibilityrule', function(event) {
259
- totalData += event.data + ',';
260
- });
261
-
262
- api.visibility('feature', 'variant', 'gruempfel', true);
263
- api.setData('feature', 'variant', 'newgruempfel');
264
- api.visibility('feature2', null, 'gruempfel2', true);
265
- api.setData('feature2', 'newgruempfel2');
266
- expect(totalData).toBe('gruempfel,newgruempfel,gruempfel2,newgruempfel2,');
267
- });
268
-
269
- it("should trigger a custom defined event", function() {
270
- var api = new featureToggleApi();
271
-
272
- let customEvent = false;
273
- api.on('customevent', function(param) {
274
- customEvent = param;
275
- });
276
-
277
- api.trigger('customevent', 'fired');
278
- expect(customEvent).toBe('fired');
279
- });
280
- });
281
-
282
- describe("Plugins", function() {
283
-
284
- it("function addPlugin should exist", function() {
285
- var api = new featureToggleApi();
286
-
287
- expect(api.addPlugin).not.toBe(null);
288
- });
289
-
290
- it("should add attribute 'newplugin' to the api", function() {
291
- function newPlugin(api, window) {
292
- api.newplugin = true;
293
- }
294
-
295
- var api = new featureToggleApi();
296
- api.addPlugin(newPlugin);
297
- expect(api.newplugin).toBe(true);
298
- });
299
-
300
- it("should add plugins via attribute constructor config", function() {
301
- function newPlugin(api, window) {
302
- api.newplugin = true;
303
- }
304
-
305
- var api = new featureToggleApi({ _plugins: [newPlugin] });
306
-
307
- expect(api.newplugin).toBe(true);
308
- expect(api.isVisible('_plugin')).toBe(false);
309
- });
310
-
311
- it("should only be executed once", function() {
312
- var api = new featureToggleApi();
313
- api.count = 0;
314
-
315
- function countPlugin(api) {
316
- api.count += 1;
317
- }
318
-
319
- api.addPlugin(countPlugin);
320
- api.addPlugin(countPlugin);
321
- expect(api.count).toBe(1);
322
- });
323
-
324
- it("should trigger the init-event", function() {
325
- function newPlugin(api) {
326
- api.on('init', function() {
327
- api.initTriggered = true;
328
- })
329
- }
330
-
331
- var api = new featureToggleApi({ _plugins: [newPlugin] });
332
- expect(api.initTriggered).toBe(true);
333
- });
334
- });
335
-
336
- describe("URL-Plugin", function() {
337
- it("should return empty string for url", function() {
338
- var api = new featureToggleApi({
339
- _plugins: [urlplugin({ window })]
340
- });
341
- expect(api.url).toBe("");
342
- })
343
- it("should have api.url = anydomain.de if set in pluginconfig", function() {
344
- var api = new featureToggleApi({
345
- _plugins: [urlplugin({
346
- url: "http://anydomain.de",
347
- window
348
- })]
349
- });
350
- expect(api.url).toBe("http://anydomain.de");
351
- })
352
- it("should have feature1 visible if param feature1=true exists", function() {
353
- var api = new featureToggleApi({
354
- _plugins: [urlplugin({ url: 'anydomain.de?feature1=true', window })]
355
- });
356
- const visibilityF1 = api.isVisible("feature1");
357
- const visibilityF2 = api.isVisible("feature2");
358
- expect(visibilityF1).toBe(true);
359
- expect(visibilityF2).toBe(false);
360
- })
361
- it("should only regard params with prefix", function() {
362
- var api = new featureToggleApi({
363
- _plugins: [urlplugin({
364
- url: 'anydomain.de?feature1=true&_feature2=true',
365
- prefix: '_',
366
- window
367
- })]
368
- });
369
- const visibilityF1 = api.isVisible("feature1");
370
- const visibilityF2 = api.isVisible("feature2");
371
- const visibility_F1 = api.isVisible("_feature1");
372
- const visibility_F2 = api.isVisible("_feature2");
373
- expect(visibilityF1).toBe(false);
374
- expect(visibilityF2).toBe(true);
375
- expect(visibility_F1).toBe(false);
376
- expect(visibility_F2).toBe(false);
377
- })
378
- });
@@ -1,11 +0,0 @@
1
- {
2
- "spec_dir": "spec",
3
- "spec_files": [
4
- "**/*[sS]pec.js"
5
- ],
6
- "helpers": [
7
- "helpers/**/*.js"
8
- ],
9
- "stopSpecOnExpectationFailure": false,
10
- "random": true
11
- }
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- isMocked: true,
3
- decodeURIComponent:function(param1){
4
- return param1;
5
- }
6
- }
package/src/index.cjs.js DELETED
@@ -1,8 +0,0 @@
1
-
2
- /*
3
- This is required to create the scripts feature-toggle-api.js and feature-toggle-api.min.js
4
- - the scripts that can be used in the brower.
5
- */
6
- import featureToggleApi from './index.js'
7
- window.featureToggleApi = featureToggleApi;
8
-