|
| 1 | +import { FlagNotFoundError, ParseError } from '@openfeature/web-sdk'; |
1 | 2 | import { LocalStorageProvider } from './localstorage-provider'; |
2 | 3 |
|
3 | | -describe('LocalStorageProvider', () => { |
4 | | - it('should be and instance of LocalStorageProvider', () => { |
5 | | - expect(new LocalStorageProvider()).toBeInstanceOf(LocalStorageProvider); |
| 4 | +describe('LocalStorage Provider', () => { |
| 5 | + const localStorageProvider = new LocalStorageProvider(); |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + jest.resetModules(); |
| 9 | + localStorage.clear(); |
| 10 | + }); |
| 11 | + |
| 12 | + afterAll(() => { |
| 13 | + localStorage.clear(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should match the expected metadata name', () => { |
| 17 | + expect(localStorageProvider.metadata.name).toBe('localStorage'); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('resolveBooleanEvaluation', () => { |
| 21 | + it('should return true', () => { |
| 22 | + localStorage.setItem('openfeature.bool-value', 'true'); |
| 23 | + expect(localStorageProvider.resolveBooleanEvaluation('bool-value')).toMatchObject({ |
| 24 | + reason: 'STATIC', |
| 25 | + value: true, |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return false', () => { |
| 30 | + localStorage.setItem('openfeature.bool-value', 'false'); |
| 31 | + expect(localStorageProvider.resolveBooleanEvaluation('bool-value')).toMatchObject({ |
| 32 | + reason: 'STATIC', |
| 33 | + value: false, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should throw because the value was the wrong type', () => { |
| 38 | + localStorage.setItem('openfeature.bool-value', 'invalid'); |
| 39 | + expect(() => localStorageProvider.resolveBooleanEvaluation('bool-value')).toThrow(ParseError); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('resolveNumberEvaluation', () => { |
| 44 | + it('should return an integer', () => { |
| 45 | + localStorage.setItem('openfeature.num-value', '1'); |
| 46 | + expect(localStorageProvider.resolveNumberEvaluation('num-value')).toMatchObject({ |
| 47 | + reason: 'STATIC', |
| 48 | + value: 1, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return a float', () => { |
| 53 | + localStorage.setItem('openfeature.num-value', '1.25'); |
| 54 | + expect(localStorageProvider.resolveNumberEvaluation('num-value')).toMatchObject({ |
| 55 | + reason: 'STATIC', |
| 56 | + value: 1.25, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw because the value was the wrong type', () => { |
| 61 | + localStorage.setItem('openfeature.num-value', 'invalid'); |
| 62 | + expect(() => localStorageProvider.resolveNumberEvaluation('num-value')).toThrow(ParseError); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('resolveStringEvaluation', () => { |
| 67 | + it('should return a string', () => { |
| 68 | + localStorage.setItem('openfeature.str-value', 'openfeature'); |
| 69 | + expect(localStorageProvider.resolveStringEvaluation('str-value')).toMatchObject({ |
| 70 | + reason: 'STATIC', |
| 71 | + value: 'openfeature', |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('resolveObjectEvaluation', () => { |
| 77 | + it('should return a boolean', () => { |
| 78 | + localStorage.setItem('openfeature.obj-value', 'true'); |
| 79 | + expect(localStorageProvider.resolveObjectEvaluation('obj-value')).toMatchObject({ |
| 80 | + reason: 'STATIC', |
| 81 | + value: true, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return a number', () => { |
| 86 | + localStorage.setItem('openfeature.obj-value', '1'); |
| 87 | + expect(localStorageProvider.resolveObjectEvaluation('obj-value')).toMatchObject({ |
| 88 | + reason: 'STATIC', |
| 89 | + value: 1, |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should return a string', () => { |
| 94 | + localStorage.setItem('openfeature.obj-value', '"openfeature"'); |
| 95 | + expect(localStorageProvider.resolveObjectEvaluation('obj-value')).toMatchObject({ |
| 96 | + reason: 'STATIC', |
| 97 | + value: 'openfeature', |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should return an object', () => { |
| 102 | + localStorage.setItem('openfeature.obj-value', '{"name": "openfeature"}'); |
| 103 | + expect(localStorageProvider.resolveObjectEvaluation('obj-value')).toMatchObject({ |
| 104 | + reason: 'STATIC', |
| 105 | + value: { name: 'openfeature' }, |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should return an array', () => { |
| 110 | + localStorage.setItem('openfeature.obj-value', '["openfeature"]'); |
| 111 | + expect(localStorageProvider.resolveObjectEvaluation('obj-value')).toMatchObject({ |
| 112 | + reason: 'STATIC', |
| 113 | + value: ['openfeature'], |
| 114 | + }); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('options.prefix', () => { |
| 119 | + it('should find a flag when the prefix is empty', () => { |
| 120 | + const localStorageProviderCustomPrefix = new LocalStorageProvider({ prefix: '' }); |
| 121 | + localStorage.setItem('bool-value', 'true'); |
| 122 | + expect(localStorageProviderCustomPrefix.resolveBooleanEvaluation('bool-value')).toMatchObject({ |
| 123 | + reason: 'STATIC', |
| 124 | + value: true, |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should find a flag when the prefix is custom', () => { |
| 129 | + const localStorageProviderCustomPrefix = new LocalStorageProvider({ prefix: 'custom.' }); |
| 130 | + localStorage.setItem('custom.bool-value', 'true'); |
| 131 | + expect(localStorageProviderCustomPrefix.resolveBooleanEvaluation('bool-value')).toMatchObject({ |
| 132 | + reason: 'STATIC', |
| 133 | + value: true, |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should not find a flag when the prefix does not match', () => { |
| 138 | + const localStorageProviderCustomPrefix = new LocalStorageProvider({ prefix: 'custom.' }); |
| 139 | + localStorage.setItem('bool-value', 'true'); |
| 140 | + expect(() => localStorageProviderCustomPrefix.resolveBooleanEvaluation('bool-value')).toThrow(FlagNotFoundError); |
| 141 | + }); |
6 | 142 | }); |
7 | 143 | }); |
0 commit comments