|
| 1 | +import { ConfigCatProvider } from './config-cat-provider'; |
| 2 | +import { ParseError, TypeMismatchError } from '@openfeature/js-sdk'; |
| 3 | +import { |
| 4 | + IConfigCatClient, |
| 5 | + getClient, |
| 6 | + createFlagOverridesFromMap, |
| 7 | + OverrideBehaviour, |
| 8 | + createConsoleLogger, |
| 9 | +} from 'configcat-js'; |
| 10 | +import { LogLevel } from 'configcat-common'; |
| 11 | + |
| 12 | +describe('ConfigCatProvider', () => { |
| 13 | + const targetingKey = "abc"; |
| 14 | + |
| 15 | + let client: IConfigCatClient; |
| 16 | + let provider: ConfigCatProvider; |
| 17 | + |
| 18 | + const values = { |
| 19 | + booleanFalse: false, |
| 20 | + booleanTrue: true, |
| 21 | + number1: 1, |
| 22 | + number2: 2, |
| 23 | + stringTest: 'Test', |
| 24 | + jsonValid: JSON.stringify({ valid: true }), |
| 25 | + jsonInvalid: '{test:123', |
| 26 | + jsonPrimitive: JSON.stringify(123), |
| 27 | + }; |
| 28 | + |
| 29 | + beforeAll(() => { |
| 30 | + client = getClient('__key__', undefined, { |
| 31 | + logger: createConsoleLogger(LogLevel.Off), |
| 32 | + offline: true, |
| 33 | + flagOverrides: createFlagOverridesFromMap(values, OverrideBehaviour.LocalOnly), |
| 34 | + }); |
| 35 | + provider = ConfigCatProvider.createFromClient(client); |
| 36 | + }); |
| 37 | + |
| 38 | + afterAll(() => { |
| 39 | + client.dispose(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should be an instance of ConfigCatProvider', () => { |
| 43 | + expect(provider).toBeInstanceOf(ConfigCatProvider); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('method resolveBooleanEvaluation', () => { |
| 47 | + it('should return default value for missing value', async () => { |
| 48 | + const value = await provider.resolveBooleanEvaluation('nonExistent', false, { targetingKey }); |
| 49 | + expect(value).toHaveProperty('value', false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return right value if key exists', async () => { |
| 53 | + const value = await provider.resolveBooleanEvaluation('booleanTrue', false, { targetingKey }); |
| 54 | + expect(value).toHaveProperty('value', values.booleanTrue); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should throw TypeMismatchError if type is different than expected', async () => { |
| 58 | + await expect(provider.resolveBooleanEvaluation('number1', false, { targetingKey })).rejects.toThrow( |
| 59 | + TypeMismatchError |
| 60 | + ); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('method resolveStringEvaluation', () => { |
| 65 | + it('should return default value for missing value', async () => { |
| 66 | + const value = await provider.resolveStringEvaluation('nonExistent', 'default', { targetingKey }); |
| 67 | + expect(value).toHaveProperty('value', 'default'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return right value if key exists', async () => { |
| 71 | + const value = await provider.resolveStringEvaluation('stringTest', 'default', { targetingKey }); |
| 72 | + expect(value).toHaveProperty('value', values.stringTest); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw TypeMismatchError if type is different than expected', async () => { |
| 76 | + await expect(provider.resolveStringEvaluation('number1', 'default', { targetingKey })).rejects.toThrow( |
| 77 | + TypeMismatchError |
| 78 | + ); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('method resolveNumberEvaluation', () => { |
| 83 | + it('should return default value for missing value', async () => { |
| 84 | + const value = await provider.resolveNumberEvaluation('nonExistent', 0, { targetingKey }); |
| 85 | + expect(value).toHaveProperty('value', 0); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return right value if key exists', async () => { |
| 89 | + const value = await provider.resolveNumberEvaluation('number1', 0, { targetingKey }); |
| 90 | + expect(value).toHaveProperty('value', values.number1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should throw TypeMismatchError if type is different than expected', async () => { |
| 94 | + await expect(provider.resolveNumberEvaluation('stringTest', 0, { targetingKey })).rejects.toThrow( |
| 95 | + TypeMismatchError |
| 96 | + ); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('method resolveObjectEvaluation', () => { |
| 101 | + it('should return default value for missing value', async () => { |
| 102 | + const value = await provider.resolveObjectEvaluation('nonExistent', {}, { targetingKey }); |
| 103 | + expect(value).toHaveProperty('value', {}); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should return right value if key exists', async () => { |
| 107 | + const value = await provider.resolveObjectEvaluation('jsonValid', {}, { targetingKey }); |
| 108 | + expect(value).toHaveProperty('value', JSON.parse(values.jsonValid)); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should throw ParseError if string is not valid JSON', async () => { |
| 112 | + await expect(provider.resolveObjectEvaluation('jsonInvalid', {}, { targetingKey })).rejects.toThrow(ParseError); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should throw TypeMismatchError if string is only a JSON primitive', async () => { |
| 116 | + await expect(provider.resolveObjectEvaluation('jsonPrimitive', {}, { targetingKey })).rejects.toThrow( |
| 117 | + TypeMismatchError |
| 118 | + ); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments