|
| 1 | +// @ts-ignore |
| 2 | +import { transform, matches, Store } from '../tsub' |
| 3 | +/** |
| 4 | + * These were taken from the tsub library. https://github.com/segmentio/tsub-js/blob/master/src/__tests__/index.test.ts |
| 5 | + * Tests exist as a smoke test to ensure that the vendoring/bundling does not introduce any regressions. |
| 6 | + * Not meant to be comprehensive. |
| 7 | + * |
| 8 | + */ |
| 9 | +test('transform and matches are exported', () => { |
| 10 | + expect(transform).toBeTruthy() |
| 11 | + expect(matches).toBeTruthy() |
| 12 | +}) |
| 13 | + |
| 14 | +test('we can use the exported transformer', () => { |
| 15 | + const result = transform( |
| 16 | + { |
| 17 | + removeMe: true, |
| 18 | + dontRemoveMe: true, |
| 19 | + }, |
| 20 | + [ |
| 21 | + { |
| 22 | + type: 'drop_properties', |
| 23 | + config: { drop: { '': ['removeMe'] } }, |
| 24 | + }, |
| 25 | + ] |
| 26 | + ) |
| 27 | + |
| 28 | + expect(result.dontRemoveMe).toBeTruthy() |
| 29 | + expect(result.removeMe).toBeUndefined() |
| 30 | +}) |
| 31 | + |
| 32 | +test('we can use the exported matcher', () => { |
| 33 | + // FQL: contains(email, ".com") |
| 34 | + let result = matches( |
| 35 | + { |
| 36 | + |
| 37 | + }, |
| 38 | + { |
| 39 | + ir: `["contains", "email", {"value": ".com"}]`, |
| 40 | + type: 'fql', |
| 41 | + } |
| 42 | + ) |
| 43 | + |
| 44 | + expect(result).toBe(true) |
| 45 | + |
| 46 | + result = matches( |
| 47 | + { |
| 48 | + |
| 49 | + }, |
| 50 | + { |
| 51 | + ir: `["contains", "email", {"value": ".com"}]`, |
| 52 | + type: 'fql', |
| 53 | + } |
| 54 | + ) |
| 55 | + |
| 56 | + expect(result).toBe(false) |
| 57 | +}) |
| 58 | + |
| 59 | +test('we can use the exported Store', () => { |
| 60 | + const routingRules = [ |
| 61 | + { |
| 62 | + matchers: [ |
| 63 | + { |
| 64 | + ir: '["=","event",{"value":"Test"}]', |
| 65 | + type: 'fql', |
| 66 | + config: { expr: 'event = "Test"' }, |
| 67 | + }, |
| 68 | + ], |
| 69 | + scope: 'destinations', |
| 70 | + target_type: 'workspace::project::destination', |
| 71 | + transformers: [[{ type: 'drop' }]], |
| 72 | + destinationName: 'Google Analytics', |
| 73 | + }, |
| 74 | + ] |
| 75 | + |
| 76 | + const store = new Store(routingRules) |
| 77 | + |
| 78 | + let result = store.getRulesByDestinationName('Google Analytics') |
| 79 | + expect(result).toStrictEqual(routingRules) |
| 80 | + |
| 81 | + result = store.getRulesByDestinationName('Adobe Analytics') |
| 82 | + expect(result).toStrictEqual([]) |
| 83 | +}) |
| 84 | + |
| 85 | +test('we can use all exports to full evaluate a rule on a payload', () => { |
| 86 | + const routingRules = [ |
| 87 | + { |
| 88 | + matchers: [ |
| 89 | + { |
| 90 | + ir: '["=","event",{"value":"Test"}]', |
| 91 | + type: 'fql', |
| 92 | + config: { expr: 'event = "Test"' }, |
| 93 | + }, |
| 94 | + ], |
| 95 | + scope: 'destinations', |
| 96 | + target_type: 'workspace::project', |
| 97 | + transformers: [[{ type: 'drop' }]], |
| 98 | + }, |
| 99 | + ] |
| 100 | + |
| 101 | + const store = new Store(routingRules) |
| 102 | + |
| 103 | + // Testing that it fetches rules without a destinationName as well |
| 104 | + const storeRules = store.getRulesByDestinationName('Google Analytics') |
| 105 | + |
| 106 | + const matchers = storeRules[0].matchers |
| 107 | + const transformers = storeRules[0].transformers |
| 108 | + |
| 109 | + const payloadToDrop = { |
| 110 | + event: 'Test', |
| 111 | + } |
| 112 | + |
| 113 | + const payloadToKeep = { |
| 114 | + event: 'Not Test', |
| 115 | + } |
| 116 | + |
| 117 | + // Payload that should drop |
| 118 | + if (matches(payloadToDrop, matchers[0])) { |
| 119 | + let payload = payloadToDrop |
| 120 | + for (const transformer of transformers) { |
| 121 | + payload = transform(payload, transformer) |
| 122 | + } |
| 123 | + expect(payload).toBeNull() |
| 124 | + } else { |
| 125 | + throw new Error('Matcher failed - expected a match!') |
| 126 | + } |
| 127 | + |
| 128 | + if (matches(payloadToKeep, matchers[0])) { |
| 129 | + throw new Error('Matcher failed - expected no match!') |
| 130 | + } |
| 131 | +}) |
0 commit comments