|
| 1 | +import createRulesEngine, { RulesEngine } from '../src'; |
| 2 | +import { createAjvValidator } from './validators'; |
| 3 | + |
| 4 | +describe('events', () => { |
| 5 | + let engine: RulesEngine; |
| 6 | + let log: jest.Mock; |
| 7 | + let call: jest.Mock; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + log = jest.fn(); |
| 11 | + call = jest.fn(); |
| 12 | + engine = createRulesEngine(createAjvValidator(), { |
| 13 | + actions: { log, call }, |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should unsubscribe', async () => { |
| 18 | + const rules = { |
| 19 | + salutation: { |
| 20 | + when: { |
| 21 | + myFacts: { |
| 22 | + firstName: { is: { type: 'string', pattern: '^J' } }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + then: { |
| 26 | + actions: [{ type: 'log', params: { message: 'Hi friend!' } }], |
| 27 | + }, |
| 28 | + otherwise: { |
| 29 | + actions: [{ type: 'call', params: { message: 'Who are you?' } }], |
| 30 | + }, |
| 31 | + }, |
| 32 | + }; |
| 33 | + engine.setRules(rules); |
| 34 | + const subscriber = jest.fn(); |
| 35 | + const unsub = engine.on('debug', subscriber); |
| 36 | + await engine.run({ firstName: 'John' }); |
| 37 | + expect(subscriber).toHaveBeenCalled(); |
| 38 | + unsub(); |
| 39 | + subscriber.mockClear(); |
| 40 | + await engine.run({ firstName: 'John' }); |
| 41 | + expect(subscriber).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should subscribe to debug events', async () => { |
| 45 | + const rules = { |
| 46 | + salutation: { |
| 47 | + when: { |
| 48 | + myFacts: { |
| 49 | + firstName: { is: { type: 'string', pattern: '^J' } }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + then: { |
| 53 | + actions: [{ type: 'log', params: { message: 'Hi friend!' } }], |
| 54 | + }, |
| 55 | + otherwise: { |
| 56 | + actions: [{ type: 'call', params: { message: 'Who are you?' } }], |
| 57 | + }, |
| 58 | + }, |
| 59 | + }; |
| 60 | + engine.setRules(rules); |
| 61 | + const subscriber = jest.fn(); |
| 62 | + const context = { firstName: 'John' }; |
| 63 | + engine.on('debug', subscriber); |
| 64 | + await engine.run(context); |
| 65 | + |
| 66 | + expect(subscriber).toHaveBeenNthCalledWith( |
| 67 | + 1, |
| 68 | + expect.objectContaining({ |
| 69 | + type: 'STARTING_RULE', |
| 70 | + rule: 'salutation', |
| 71 | + interpolated: rules.salutation.when, |
| 72 | + context, |
| 73 | + }), |
| 74 | + ); |
| 75 | + |
| 76 | + expect(subscriber).toHaveBeenNthCalledWith( |
| 77 | + 2, |
| 78 | + expect.objectContaining({ |
| 79 | + type: 'STARTING_FACT_MAP', |
| 80 | + rule: 'salutation', |
| 81 | + mapId: 'myFacts', |
| 82 | + factMap: rules.salutation.when.myFacts, |
| 83 | + }), |
| 84 | + ); |
| 85 | + |
| 86 | + expect(subscriber).toHaveBeenNthCalledWith( |
| 87 | + 3, |
| 88 | + expect.objectContaining({ |
| 89 | + type: 'STARTING_FACT', |
| 90 | + rule: 'salutation', |
| 91 | + mapId: 'myFacts', |
| 92 | + factName: 'firstName', |
| 93 | + }), |
| 94 | + ); |
| 95 | + |
| 96 | + expect(subscriber).toHaveBeenNthCalledWith( |
| 97 | + 4, |
| 98 | + expect.objectContaining({ |
| 99 | + type: 'EXECUTED_FACT', |
| 100 | + rule: 'salutation', |
| 101 | + mapId: 'myFacts', |
| 102 | + path: undefined, |
| 103 | + factName: 'firstName', |
| 104 | + value: context.firstName, |
| 105 | + resolved: context.firstName, |
| 106 | + }), |
| 107 | + ); |
| 108 | + |
| 109 | + expect(subscriber).toHaveBeenNthCalledWith( |
| 110 | + 5, |
| 111 | + expect.objectContaining({ |
| 112 | + type: 'EVALUATED_FACT', |
| 113 | + rule: 'salutation', |
| 114 | + mapId: 'myFacts', |
| 115 | + path: undefined, |
| 116 | + factName: 'firstName', |
| 117 | + value: 'John', |
| 118 | + resolved: 'John', |
| 119 | + is: { type: 'string', pattern: '^J' }, |
| 120 | + result: { result: true }, |
| 121 | + }), |
| 122 | + ); |
| 123 | + |
| 124 | + expect(subscriber).toHaveBeenNthCalledWith( |
| 125 | + 6, |
| 126 | + expect.objectContaining({ |
| 127 | + type: 'FINISHED_FACT_MAP', |
| 128 | + rule: 'salutation', |
| 129 | + mapId: 'myFacts', |
| 130 | + results: { |
| 131 | + firstName: { result: true, value: 'John', resolved: 'John' }, |
| 132 | + }, |
| 133 | + passed: true, |
| 134 | + error: false, |
| 135 | + }), |
| 136 | + ); |
| 137 | + |
| 138 | + expect(subscriber).toHaveBeenNthCalledWith( |
| 139 | + 7, |
| 140 | + expect.objectContaining({ |
| 141 | + type: 'FINISHED_RULE', |
| 142 | + rule: 'salutation', |
| 143 | + interpolated: rules.salutation.when, |
| 144 | + context, |
| 145 | + result: { |
| 146 | + actions: [{ type: 'log', params: { message: 'Hi friend!' } }], |
| 147 | + results: { |
| 148 | + myFacts: expect.objectContaining({ |
| 149 | + firstName: { |
| 150 | + result: true, |
| 151 | + value: 'John', |
| 152 | + resolved: 'John', |
| 153 | + }, |
| 154 | + }), |
| 155 | + }, |
| 156 | + }, |
| 157 | + }), |
| 158 | + ); |
| 159 | + }); |
| 160 | +}); |
0 commit comments