1 | const {root, compile} = require('../../index');
|
2 | const {describeCompilers, evalOrLoad} = require('../test-utils');
|
3 |
|
4 | describe('trace', () => {
|
5 |
|
6 | afterEach(() => {
|
7 | jest.restoreAllMocks()
|
8 | })
|
9 | describe('plain', () => {
|
10 | it('should print label and info', () => {
|
11 | jest.spyOn(console, 'log').mockReturnValue(undefined)
|
12 |
|
13 | const model = {
|
14 | result: root.trace('a')
|
15 | }
|
16 |
|
17 | const optModel = evalOrLoad(compile(model))
|
18 | const initialData = {a: 1}
|
19 |
|
20 | const result = optModel(initialData)
|
21 |
|
22 | expect(result).toHaveProperty('result', {a: 1})
|
23 | expect(console.log).toHaveBeenCalledWith('a', {
|
24 | source: 'src/__tests__/trace.js:14:22',
|
25 | token: 'root',
|
26 | value: {a: 1}
|
27 | })
|
28 | })
|
29 |
|
30 | it('sould work everywhere', () => {
|
31 | jest.spyOn(console, 'log').mockReturnValue(undefined)
|
32 |
|
33 | const model = {
|
34 | withLabel: root.get('a').plus(1).trace('a:')
|
35 | }
|
36 |
|
37 | const optModel = evalOrLoad(compile(model))
|
38 | const initialData = {a: 1}
|
39 |
|
40 | const result = optModel(initialData)
|
41 | expect(result).toHaveProperty('withLabel', 2)
|
42 | expect(console.log).toHaveBeenCalledWith('a:', {
|
43 | source: 'src/__tests__/trace.js:34:34',
|
44 | token: 'plus',
|
45 | value: 2
|
46 | })
|
47 | })
|
48 | })
|
49 |
|
50 |
|
51 | describe('conditionalTrace', () => {
|
52 | describeCompilers(['simple', 'optimizing'], compiler => {
|
53 | it('should trace if the condition is met', () => {
|
54 | jest.spyOn(console, 'log').mockReturnValue(undefined)
|
55 |
|
56 | const model = {
|
57 | result: root.get('a').conditionalTrace(root.get('a'))
|
58 | };
|
59 |
|
60 | const optModel = evalOrLoad(compile(model, {compiler}));
|
61 | const initialData = {
|
62 | a: true
|
63 | };
|
64 |
|
65 | const result = optModel(initialData);
|
66 | expect(result).toHaveProperty('result', true)
|
67 | expect(console.log).toHaveBeenCalled();
|
68 | })
|
69 |
|
70 | it('should not trace if the condition is not met', () => {
|
71 | jest.spyOn(console, 'log').mockReturnValue(undefined)
|
72 |
|
73 | const model = {
|
74 | result: root.get('a').conditionalTrace(root.get('a'))
|
75 | };
|
76 |
|
77 | const optModel = evalOrLoad(compile(model, {compiler}));
|
78 | const initialData = {
|
79 | a: false
|
80 | };
|
81 |
|
82 | const result = optModel(initialData);
|
83 | expect(result).toHaveProperty('result', false)
|
84 | expect(console.log).not.toHaveBeenCalled();
|
85 | })
|
86 | })
|
87 | })
|
88 |
|
89 | describe('tapTrace', () => {
|
90 | describeCompilers(['simple', 'optimizing'], compiler => {
|
91 | it('should trace the result of the tap function', () => {
|
92 | jest.spyOn(console, 'log').mockReturnValue(undefined)
|
93 |
|
94 | const model = {
|
95 | result: root.get('a').tapTrace(x => x.plus(2))
|
96 | };
|
97 |
|
98 | const optModel = evalOrLoad(compile(model, {compiler}));
|
99 | const initialData = {
|
100 | a: 1
|
101 | };
|
102 |
|
103 | const result = optModel(initialData);
|
104 | expect(result).toHaveProperty('result', 1)
|
105 | expect(console.log).toHaveBeenCalledWith({source: expect.any(String), token: 'plus', value: 3});
|
106 | })
|
107 | })
|
108 | })
|
109 | })
|