|
| 1 | +import { translateContext } from '../src/translateContext'; |
| 2 | +import TestLogger from './TestLogger'; |
| 3 | + |
| 4 | +it('Uses the targetingKey as the user key', () => { |
| 5 | + const logger = new TestLogger(); |
| 6 | + expect(translateContext(logger, { targetingKey: 'the-key' })).toEqual({ |
| 7 | + key: 'the-key', |
| 8 | + kind: 'user', |
| 9 | + }); |
| 10 | + expect(logger.logs.length).toEqual(0); |
| 11 | +}); |
| 12 | + |
| 13 | +it('gives targetingKey precedence over key', () => { |
| 14 | + const logger = new TestLogger(); |
| 15 | + expect(translateContext(logger, { targetingKey: 'target-key', key: 'key-key' })).toEqual({ |
| 16 | + key: 'target-key', |
| 17 | + kind: 'user', |
| 18 | + }); |
| 19 | + // Should log a warning about both being defined. |
| 20 | + expect(logger.logs.length).toEqual(1); |
| 21 | +}); |
| 22 | + |
| 23 | +describe.each([ |
| 24 | + ['name', 'value2'], |
| 25 | + ['firstName', 'value3'], |
| 26 | + ['lastName', 'value4'], |
| 27 | + ['email', 'value5'], |
| 28 | + ['avatar', 'value6'], |
| 29 | + ['ip', 'value7'], |
| 30 | + ['country', 'value8'], |
| 31 | + ['anonymous', true], |
| 32 | +])('given correct built-in attributes', (key, value) => { |
| 33 | + const logger = new TestLogger(); |
| 34 | + it('translates the key correctly', () => { |
| 35 | + expect(translateContext(logger, { targetingKey: 'the-key', [key]: value })).toEqual({ |
| 36 | + key: 'the-key', |
| 37 | + [key]: value, |
| 38 | + kind: 'user', |
| 39 | + }); |
| 40 | + expect(logger.logs.length).toEqual(0); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +it.each(['key', 'targetingKey'])('handles key or targetingKey', (key) => { |
| 45 | + const logger = new TestLogger(); |
| 46 | + expect(translateContext(logger, { [key]: 'the-key' })).toEqual({ |
| 47 | + key: 'the-key', |
| 48 | + kind: 'user', |
| 49 | + }); |
| 50 | + expect(logger.logs.length).toEqual(0); |
| 51 | +}); |
| 52 | + |
| 53 | +describe.each([ |
| 54 | + ['name', 17], |
| 55 | + ['anonymous', 'value'], |
| 56 | +])('given incorrect built-in attributes', (key, value) => { |
| 57 | + it('the bad key is omitted', () => { |
| 58 | + const logger = new TestLogger(); |
| 59 | + expect(translateContext(logger, { targetingKey: 'the-key', [key]: value })).toEqual({ |
| 60 | + key: 'the-key', |
| 61 | + kind: 'user', |
| 62 | + }); |
| 63 | + expect(logger.logs[0]).toMatch(new RegExp(`The attribute '${key}' must be of type.*`)); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +it('accepts custom attributes', () => { |
| 68 | + const logger = new TestLogger(); |
| 69 | + expect(translateContext(logger, { targetingKey: 'the-key', someAttr: 'someValue' })).toEqual({ |
| 70 | + key: 'the-key', |
| 71 | + kind: 'user', |
| 72 | + someAttr: 'someValue', |
| 73 | + }); |
| 74 | + expect(logger.logs.length).toEqual(0); |
| 75 | +}); |
| 76 | + |
| 77 | +it('accepts string/boolean/number arrays', () => { |
| 78 | + const logger = new TestLogger(); |
| 79 | + expect( |
| 80 | + translateContext(logger, { |
| 81 | + targetingKey: 'the-key', |
| 82 | + strings: ['a', 'b', 'c'], |
| 83 | + numbers: [1, 2, 3], |
| 84 | + booleans: [true, false], |
| 85 | + }), |
| 86 | + ).toEqual({ |
| 87 | + key: 'the-key', |
| 88 | + kind: 'user', |
| 89 | + strings: ['a', 'b', 'c'], |
| 90 | + numbers: [1, 2, 3], |
| 91 | + booleans: [true, false], |
| 92 | + }); |
| 93 | + expect(logger.logs.length).toEqual(0); |
| 94 | +}); |
| 95 | + |
| 96 | +it('converts date to ISO strings', () => { |
| 97 | + const date = new Date(); |
| 98 | + const logger = new TestLogger(); |
| 99 | + expect(translateContext(logger, { targetingKey: 'the-key', date })).toEqual({ |
| 100 | + key: 'the-key', |
| 101 | + kind: 'user', |
| 102 | + date: date.toISOString(), |
| 103 | + }); |
| 104 | + expect(logger.logs.length).toEqual(0); |
| 105 | +}); |
| 106 | + |
| 107 | +it('can convert a single kind context', () => { |
| 108 | + const evaluationContext = { |
| 109 | + kind: 'organization', |
| 110 | + targetingKey: 'my-org-key', |
| 111 | + }; |
| 112 | + |
| 113 | + const expectedContext = { |
| 114 | + kind: 'organization', |
| 115 | + key: 'my-org-key', |
| 116 | + }; |
| 117 | + |
| 118 | + const logger = new TestLogger(); |
| 119 | + expect(translateContext(logger, evaluationContext)).toEqual(expectedContext); |
| 120 | + expect(logger.logs.length).toEqual(0); |
| 121 | +}); |
| 122 | + |
| 123 | +it('can convert a multi-context', () => { |
| 124 | + const evaluationContext = { |
| 125 | + kind: 'multi', |
| 126 | + organization: { |
| 127 | + targetingKey: 'my-org-key', |
| 128 | + myCustomAttribute: 'myAttributeValue', |
| 129 | + }, |
| 130 | + user: { |
| 131 | + targetingKey: 'my-user-key', |
| 132 | + }, |
| 133 | + }; |
| 134 | + |
| 135 | + const expectedContext = { |
| 136 | + kind: 'multi', |
| 137 | + organization: { |
| 138 | + key: 'my-org-key', |
| 139 | + myCustomAttribute: 'myAttributeValue', |
| 140 | + }, |
| 141 | + user: { |
| 142 | + key: 'my-user-key', |
| 143 | + }, |
| 144 | + }; |
| 145 | + |
| 146 | + const logger = new TestLogger(); |
| 147 | + expect(translateContext(logger, evaluationContext)).toEqual(expectedContext); |
| 148 | + expect(logger.logs.length).toEqual(0); |
| 149 | +}); |
| 150 | + |
| 151 | +it('can handle privateAttributes in a single context', () => { |
| 152 | + const evaluationContext = { |
| 153 | + kind: 'organization', |
| 154 | + name: 'the-org-name', |
| 155 | + targetingKey: 'my-org-key', |
| 156 | + myCustomAttribute: 'myCustomValue', |
| 157 | + privateAttributes: ['myCustomAttribute'], |
| 158 | + }; |
| 159 | + |
| 160 | + const expectedContext = { |
| 161 | + kind: 'organization', |
| 162 | + name: 'the-org-name', |
| 163 | + key: 'my-org-key', |
| 164 | + myCustomAttribute: 'myCustomValue', |
| 165 | + _meta: { |
| 166 | + privateAttributes: ['myCustomAttribute'], |
| 167 | + }, |
| 168 | + }; |
| 169 | + |
| 170 | + const logger = new TestLogger(); |
| 171 | + expect(translateContext(logger, evaluationContext)).toEqual(expectedContext); |
| 172 | + expect(logger.logs.length).toEqual(0); |
| 173 | +}); |
| 174 | + |
| 175 | +it('detects a cycle and logs an error', () => { |
| 176 | + const a: any = { |
| 177 | + b: { c: {} }, |
| 178 | + }; |
| 179 | + |
| 180 | + a.b.c = a; |
| 181 | + const evaluationContext = { |
| 182 | + key: 'a-key', |
| 183 | + kind: 'singularity', |
| 184 | + a, |
| 185 | + }; |
| 186 | + |
| 187 | + const expectedContext = { |
| 188 | + key: 'a-key', |
| 189 | + kind: 'singularity', |
| 190 | + a: { b: {} }, |
| 191 | + }; |
| 192 | + |
| 193 | + const logger = new TestLogger(); |
| 194 | + expect(translateContext(logger, evaluationContext)).toEqual(expectedContext); |
| 195 | + expect(logger.logs.length).toEqual(1); |
| 196 | +}); |
| 197 | + |
| 198 | +it('allows references in different branches', () => { |
| 199 | + const a = { test: 'test' }; |
| 200 | + |
| 201 | + const evaluationContext = { |
| 202 | + key: 'a-key', |
| 203 | + kind: 'singularity', |
| 204 | + b: { a }, |
| 205 | + c: { a }, |
| 206 | + }; |
| 207 | + |
| 208 | + const expectedContext = { |
| 209 | + key: 'a-key', |
| 210 | + kind: 'singularity', |
| 211 | + b: { a: { test: 'test' } }, |
| 212 | + c: { a: { test: 'test' } }, |
| 213 | + }; |
| 214 | + |
| 215 | + const logger = new TestLogger(); |
| 216 | + expect(translateContext(logger, evaluationContext)).toEqual(expectedContext); |
| 217 | + expect(logger.logs.length).toEqual(0); |
| 218 | +}); |
0 commit comments