|
| 1 | +import { EvaluationContext } from '@openfeature/nodejs-sdk'; |
| 2 | +import { GoFeatureFlagUser } from './model'; |
| 3 | +import { transformContext } from './context-transformer'; |
| 4 | + |
| 5 | +describe('contextTransformer', () => { |
| 6 | + it('should use the targetingKey as user key', () => { |
| 7 | + const got = transformContext({ |
| 8 | + targetingKey: 'user-key', |
| 9 | + } as EvaluationContext); |
| 10 | + const want: GoFeatureFlagUser = { |
| 11 | + key: 'user-key', |
| 12 | + anonymous: false, |
| 13 | + custom: {}, |
| 14 | + }; |
| 15 | + expect(got).toEqual(want); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should specify the anonymous field base on attributes', () => { |
| 19 | + const got = transformContext({ |
| 20 | + targetingKey: 'user-key', |
| 21 | + anonymous: true, |
| 22 | + } as EvaluationContext); |
| 23 | + const want: GoFeatureFlagUser = { |
| 24 | + key: 'user-key', |
| 25 | + anonymous: true, |
| 26 | + custom: {}, |
| 27 | + }; |
| 28 | + expect(got).toEqual(want); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should hash the context as key if no targetingKey provided', () => { |
| 32 | + const got = transformContext({ |
| 33 | + anonymous: true, |
| 34 | + firstname: 'John', |
| 35 | + lastname: 'Doe', |
| 36 | + |
| 37 | + } as EvaluationContext); |
| 38 | + |
| 39 | + const want: GoFeatureFlagUser = { |
| 40 | + key: 'dd3027562879ff6857cc6b8b88ced570546d7c0c', |
| 41 | + anonymous: true, |
| 42 | + custom: { |
| 43 | + firstname: 'John', |
| 44 | + lastname: 'Doe', |
| 45 | + |
| 46 | + }, |
| 47 | + }; |
| 48 | + expect(got).toEqual(want); |
| 49 | + }); |
| 50 | + it('should fill custom fields if extra field are present', () => { |
| 51 | + const got = transformContext({ |
| 52 | + targetingKey: 'user-key', |
| 53 | + anonymous: true, |
| 54 | + firstname: 'John', |
| 55 | + lastname: 'Doe', |
| 56 | + |
| 57 | + } as EvaluationContext); |
| 58 | + |
| 59 | + const want: GoFeatureFlagUser = { |
| 60 | + key: 'user-key', |
| 61 | + anonymous: true, |
| 62 | + custom: { |
| 63 | + firstname: 'John', |
| 64 | + lastname: 'Doe', |
| 65 | + |
| 66 | + }, |
| 67 | + }; |
| 68 | + expect(got).toEqual(want); |
| 69 | + }); |
| 70 | +}); |
0 commit comments