|
1 | 1 | import { UnleashWebProvider } from './unleash-web-provider'; |
2 | 2 | import fetchMock, { enableFetchMocks } from 'jest-fetch-mock'; |
3 | | -import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk'; |
| 3 | +import { OpenFeature, ProviderEvents, TypeMismatchError } from '@openfeature/web-sdk'; |
4 | 4 | import testdata from './testdata.json'; |
5 | 5 | import TestLogger from './test-logger'; |
6 | 6 |
|
@@ -178,83 +178,95 @@ describe('UnleashWebProvider evaluations', () => { |
178 | 178 | }); |
179 | 179 |
|
180 | 180 | describe('method resolveBooleanEvaluation', () => { |
181 | | - it('should return false for missing toggle', async () => { |
182 | | - const evaluation = await provider.resolveBooleanEvaluation('nonExistent'); |
| 181 | + it('should return false for missing toggle', () => { |
| 182 | + const evaluation = provider.resolveBooleanEvaluation('nonExistent'); |
183 | 183 | expect(evaluation).toHaveProperty(valueProperty, false); |
184 | 184 | }); |
185 | 185 |
|
186 | | - it('should return true if enabled toggle exists', async () => { |
187 | | - const evaluation = await provider.resolveBooleanEvaluation('simpleToggle'); |
| 186 | + it('should return true if enabled toggle exists', () => { |
| 187 | + const evaluation = provider.resolveBooleanEvaluation('simpleToggle'); |
188 | 188 | expect(evaluation).toHaveProperty(valueProperty, true); |
189 | 189 | }); |
190 | 190 |
|
191 | | - it('should return false if a disabled toggle exists', async () => { |
192 | | - const evaluation = await provider.resolveBooleanEvaluation('disabledToggle'); |
| 191 | + it('should return false if a disabled toggle exists', () => { |
| 192 | + const evaluation = provider.resolveBooleanEvaluation('disabledToggle'); |
193 | 193 | expect(evaluation).toHaveProperty(valueProperty, false); |
194 | 194 | }); |
195 | 195 | }); |
196 | 196 |
|
197 | 197 | describe('method resolveStringEvaluation', () => { |
198 | | - it('should return default value for missing value', async () => { |
199 | | - const evaluation = await provider.resolveStringEvaluation('nonExistent', 'defaultValue'); |
| 198 | + it('should return default value for missing value', () => { |
| 199 | + const evaluation = provider.resolveStringEvaluation('nonExistent', 'defaultValue'); |
200 | 200 | expect(evaluation).toHaveProperty(valueProperty, 'defaultValue'); |
201 | 201 | }); |
202 | 202 |
|
203 | | - it('should return right value if variant toggle exists and is enabled', async () => { |
204 | | - const evaluation = await provider.resolveStringEvaluation('variantToggleString', 'variant1'); |
| 203 | + it('should return right value if variant toggle exists and is enabled', () => { |
| 204 | + const evaluation = provider.resolveStringEvaluation('variantToggleString', 'variant1'); |
205 | 205 | expect(evaluation).toHaveProperty(valueProperty, 'some-text'); |
206 | 206 | }); |
207 | 207 |
|
208 | | - it('should return default value if a toggle is disabled', async () => { |
209 | | - const evaluation = await provider.resolveStringEvaluation('disabledVariant', 'defaultValue'); |
| 208 | + it('should return default value if a toggle is disabled', () => { |
| 209 | + const evaluation = provider.resolveStringEvaluation('disabledVariant', 'defaultValue'); |
210 | 210 | expect(evaluation).toHaveProperty(valueProperty, 'defaultValue'); |
211 | 211 | }); |
| 212 | + |
| 213 | + it('should throw TypeMismatchError if requested variant type is not a string', () => { |
| 214 | + expect(() => provider.resolveStringEvaluation('variantToggleJson', 'default string')).toThrow(TypeMismatchError); |
| 215 | + }); |
212 | 216 | }); |
213 | 217 |
|
214 | 218 | describe('method resolveNumberEvaluation', () => { |
215 | | - it('should return default value for missing value', async () => { |
216 | | - const evaluation = await provider.resolveNumberEvaluation('nonExistent', 5); |
| 219 | + it('should return default value for missing value', () => { |
| 220 | + const evaluation = provider.resolveNumberEvaluation('nonExistent', 5); |
217 | 221 | expect(evaluation).toHaveProperty(valueProperty, 5); |
218 | 222 | }); |
219 | 223 |
|
220 | | - it('should return integer value if variant toggle exists and is enabled', async () => { |
221 | | - const evaluation = await provider.resolveNumberEvaluation('variantToggleInteger', 0); |
| 224 | + it('should return integer value if variant toggle exists and is enabled', () => { |
| 225 | + const evaluation = provider.resolveNumberEvaluation('variantToggleInteger', 0); |
222 | 226 | expect(evaluation).toHaveProperty(valueProperty, 3); |
223 | 227 | }); |
224 | 228 |
|
225 | | - it('should return double value if variant toggle exists and is enabled', async () => { |
226 | | - const evaluation = await provider.resolveNumberEvaluation('variantToggleDouble', 0); |
| 229 | + it('should return double value if variant toggle exists and is enabled', () => { |
| 230 | + const evaluation = provider.resolveNumberEvaluation('variantToggleDouble', 0); |
227 | 231 | expect(evaluation).toHaveProperty(valueProperty, 1.2); |
228 | 232 | }); |
229 | 233 |
|
230 | | - it('should return default value if a toggle is disabled', async () => { |
231 | | - const evaluation = await provider.resolveNumberEvaluation('disabledVariant', 0); |
| 234 | + it('should return default value if a toggle is disabled', () => { |
| 235 | + const evaluation = provider.resolveNumberEvaluation('disabledVariant', 0); |
232 | 236 | expect(evaluation).toHaveProperty(valueProperty, 0); |
233 | 237 | }); |
| 238 | + |
| 239 | + it('should throw TypeMismatchError if requested variant type is not a number', () => { |
| 240 | + expect(() => provider.resolveNumberEvaluation('variantToggleCsv', 0)).toThrow(TypeMismatchError); |
| 241 | + }); |
234 | 242 | }); |
235 | 243 |
|
236 | 244 | describe('method resolveObjectEvaluation', () => { |
237 | | - it('should return default value for missing value', async () => { |
| 245 | + it('should return default value for missing value', () => { |
238 | 246 | const defaultValue = '{"notFound" : true}'; |
239 | | - const evaluation = await provider.resolveObjectEvaluation('nonExistent', JSON.parse(defaultValue)); |
| 247 | + const evaluation = provider.resolveObjectEvaluation('nonExistent', JSON.parse(defaultValue)); |
240 | 248 | expect(evaluation).toHaveProperty(valueProperty, JSON.parse(defaultValue)); |
241 | 249 | }); |
242 | 250 |
|
243 | | - it('should return json value if variant toggle exists and is enabled', async () => { |
| 251 | + it('should return json value if variant toggle exists and is enabled', () => { |
244 | 252 | const expectedVariant = '{hello: world}'; |
245 | | - const evaluation = await provider.resolveObjectEvaluation('variantToggleJson', JSON.parse('{"default": false}')); |
| 253 | + const evaluation = provider.resolveObjectEvaluation('variantToggleJson', JSON.parse('{"default": false}')); |
246 | 254 | expect(evaluation).toHaveProperty(valueProperty, expectedVariant); |
247 | 255 | }); |
248 | 256 |
|
249 | | - it('should return csv value if variant toggle exists and is enabled', async () => { |
250 | | - const evaluation = await provider.resolveObjectEvaluation('variantToggleCsv', 'a,b,c,d'); |
| 257 | + it('should return csv value if variant toggle exists and is enabled', () => { |
| 258 | + const evaluation = provider.resolveObjectEvaluation('variantToggleCsv', 'a,b,c,d'); |
251 | 259 | expect(evaluation).toHaveProperty(valueProperty, '1,2,3,4'); |
252 | 260 | }); |
253 | 261 |
|
254 | | - it('should return default value if a toggle is disabled', async () => { |
| 262 | + it('should return default value if a toggle is disabled', () => { |
255 | 263 | const defaultValue = '{foo: bar}'; |
256 | | - const evaluation = await provider.resolveObjectEvaluation('disabledVariant', defaultValue); |
| 264 | + const evaluation = provider.resolveObjectEvaluation('disabledVariant', defaultValue); |
257 | 265 | expect(evaluation).toHaveProperty(valueProperty, defaultValue); |
258 | 266 | }); |
| 267 | + |
| 268 | + it('should throw TypeMismatchError if requested variant type is not json or csv', () => { |
| 269 | + expect(() => provider.resolveObjectEvaluation('variantToggleInteger', 'a,b,c,d')).toThrow(TypeMismatchError); |
| 270 | + }); |
259 | 271 | }); |
260 | 272 | }); |
0 commit comments