|
1 | 1 | import { UnleashWebProvider } from './unleash-web-provider'; |
| 2 | +import fetchMock, { enableFetchMocks } from 'jest-fetch-mock'; |
| 3 | + |
| 4 | +import testdata from './testdata.json'; |
| 5 | +import TestLogger from './test-logger'; |
2 | 6 |
|
3 | 7 | describe('UnleashWebProvider', () => { |
4 | | - it('should be and instance of UnleashWebProvider', () => { |
5 | | - expect(new UnleashWebProvider()).toBeInstanceOf(UnleashWebProvider); |
| 8 | + const endpoint = 'http://localhost:4242'; |
| 9 | + const logger = new TestLogger(); |
| 10 | + const valueProperty = 'value'; |
| 11 | + |
| 12 | + let provider: UnleashWebProvider; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + fetchMock.mockClear(); |
| 16 | + fetchMock.mockReset(); |
| 17 | + }); |
| 18 | + |
| 19 | + beforeAll(async () => { |
| 20 | + enableFetchMocks(); |
| 21 | + //fetchMock.mockResponseOnce(JSON.stringify({"toggles":[]})); |
| 22 | + fetchMock.mockResponseOnce(JSON.stringify(testdata)); |
| 23 | + provider = new UnleashWebProvider({ url: endpoint, clientKey: 'clientsecret', appName: 'test',}, logger); |
| 24 | + await provider.initialize(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should be an instance of UnleashWebProvider', async () => { |
| 28 | + expect(provider).toBeInstanceOf(UnleashWebProvider); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('method resolveBooleanEvaluation', () => { |
| 32 | + it('should return false for missing toggle', async () => { |
| 33 | + const evaluation = await provider.resolveBooleanEvaluation('nonExistent'); |
| 34 | + expect(evaluation).toHaveProperty(valueProperty, false); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return true if enabled toggle exists', async () => { |
| 38 | + const evaluation = await provider.resolveBooleanEvaluation('simpleToggle'); |
| 39 | + expect(evaluation).toHaveProperty(valueProperty, true); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return false if a disabled toggle exists', async () => { |
| 43 | + const evaluation = await provider.resolveBooleanEvaluation('disabledToggle'); |
| 44 | + expect(evaluation).toHaveProperty(valueProperty, false); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('method resolveStringEvaluation', () => { |
| 49 | + it('should return default value for missing value', async () => { |
| 50 | + const evaluation = await provider.resolveStringEvaluation('nonExistent', 'defaultValue'); |
| 51 | + expect(evaluation).toHaveProperty(valueProperty, 'defaultValue'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return right value if variant toggle exists and is enabled', async () => { |
| 55 | + const evaluation = await provider.resolveStringEvaluation('variantToggleString', 'variant1'); |
| 56 | + expect(evaluation).toHaveProperty(valueProperty, 'some-text'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should return default value if a toggle is disabled', async () => { |
| 60 | + const evaluation = await provider.resolveStringEvaluation('disabledVariant', 'defaultValue'); |
| 61 | + expect(evaluation).toHaveProperty(valueProperty, 'defaultValue'); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('method resolveNumberEvaluation', () => { |
| 66 | + it('should return default value for missing value', async () => { |
| 67 | + const evaluation = await provider.resolveNumberEvaluation('nonExistent', 5); |
| 68 | + expect(evaluation).toHaveProperty(valueProperty, 5); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should return integer value if variant toggle exists and is enabled', async () => { |
| 72 | + const evaluation = await provider.resolveNumberEvaluation('variantToggleInteger', 0); |
| 73 | + expect(evaluation).toHaveProperty(valueProperty, 3); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return double value if variant toggle exists and is enabled', async () => { |
| 77 | + const evaluation = await provider.resolveNumberEvaluation('variantToggleDouble', 0); |
| 78 | + expect(evaluation).toHaveProperty(valueProperty, 1.2); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should return default value if a toggle is disabled', async () => { |
| 82 | + const evaluation = await provider.resolveNumberEvaluation('disabledVariant', 0); |
| 83 | + expect(evaluation).toHaveProperty(valueProperty, 0); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('method resolveObjectEvaluation', () => { |
| 88 | + it('should return default value for missing value', async () => { |
| 89 | + const defaultValue = '{"notFound" : true}'; |
| 90 | + const evaluation = await provider.resolveObjectEvaluation('nonExistent', JSON.parse(defaultValue)); |
| 91 | + expect(evaluation).toHaveProperty(valueProperty, JSON.parse(defaultValue)); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should return json value if variant toggle exists and is enabled', async () => { |
| 95 | + const expectedVariant = '{hello: world}'; |
| 96 | + const evaluation = await provider.resolveObjectEvaluation('variantToggleJson', JSON.parse('{"default": false}')); |
| 97 | + expect(evaluation).toHaveProperty(valueProperty, expectedVariant); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should return csv value if variant toggle exists and is enabled', async () => { |
| 101 | + const evaluation = await provider.resolveObjectEvaluation('variantToggleCsv', 'a,b,c,d'); |
| 102 | + expect(evaluation).toHaveProperty(valueProperty, '1,2,3,4'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should return default value if a toggle is disabled', async () => { |
| 106 | + const defaultValue = '{foo: bar}'; |
| 107 | + const evaluation = await provider.resolveObjectEvaluation('disabledVariant', defaultValue); |
| 108 | + expect(evaluation).toHaveProperty(valueProperty, defaultValue); |
| 109 | + }); |
6 | 110 | }); |
7 | 111 | }); |
0 commit comments