UNPKG

3.05 kBJavaScriptView Raw
1const {
2 compile,
3 and,
4 or,
5 root,
6 arg0,
7 arg1,
8 setter,
9 splice,
10 bind,
11 chain,
12 push,
13 template
14} = require('../../index');
15const {
16 expectTapFunctionToHaveBeenCalled,
17 describeCompilers,
18 evalOrLoad,
19 funcLibrary
20} = require('../test-utils');
21const _ = require('lodash');
22
23describe('setters', () => {
24 describeCompilers(['simple', 'optimizing', 'bytecode'], compiler => {
25 it('push', () => {
26 const model = {
27 data: root.get('data').map(v => v.call('tap')),
28 pushIt: push('data')
29 }
30 const optCode = evalOrLoad(compile(model, {
31 compiler
32 }));
33 const inst = optCode({
34 data: ['a', 'b']
35 }, funcLibrary);
36 expectTapFunctionToHaveBeenCalled(2, compiler)
37 inst.pushIt('c')
38 expectTapFunctionToHaveBeenCalled(1, compiler)
39 expect(inst.data).toEqual(['a', 'b', 'c']);
40 })
41 it('should allow variable args in between constant args', () => {
42 const model = {
43 data: root.get('a'),
44 setInner: setter('a', arg0, 'c')
45 }
46 const optCode = evalOrLoad(compile(model, {
47 compiler
48 }));
49 const inst = optCode({
50 a: {b: {c: 'nothing'}}
51 }, funcLibrary);
52 inst.setInner('b', 'hello')
53 expect(inst.data).toEqual({
54 b: {
55 c: 'hello'
56 }
57 });
58 })
59 it('should allow deep setting of an object', () => {
60 const model = {
61 outer: root.get('a').mapValues(a => a),
62 setInner: setter('a', 'b', 'c')
63 }
64 const optCode = evalOrLoad(compile(model, {
65 compiler
66 }));
67 const inst = optCode({
68 a: {}
69 }, funcLibrary);
70 inst.setInner('hello')
71 expect(inst.outer).toEqual({
72 b: {
73 c: 'hello'
74 }
75 });
76 })
77 it('should allow deep setting of an array', () => {
78 const model = {
79 outer: root.get('a').mapValues(a => a),
80 setInner: setter('a', 'b', 0, 'c')
81 }
82 const optCode = evalOrLoad(compile(model, {
83 compiler
84 }));
85 const inst = optCode({
86 a: {}
87 }, funcLibrary);
88 inst.setInner('hello')
89 expect(inst.outer).toEqual({
90 b: [{
91 c: 'hello'
92 }]
93 });
94 })
95
96 it('should allow deep splicing', () => {
97 const model = {
98 outer: root.get('a').mapValues(a => a),
99 spliceInner: splice('a', 'b')
100 }
101 const optCode = evalOrLoad(compile(model, {
102 compiler
103 }));
104 const inst = optCode({
105 a: {}
106 }, funcLibrary);
107 inst.spliceInner(0, 0, 'hello')
108 expect(inst.outer).toEqual({
109 b: ['hello']
110 });
111 })
112 it('should allow deep pushing', () => {
113 const model = {
114 outer: root.get('a').mapValues(a => a),
115 pushInner: push('a', 'b')
116 }
117 const optCode = evalOrLoad(compile(model, {
118 compiler
119 }));
120 const inst = optCode({
121 a: {}
122 }, funcLibrary);
123 inst.pushInner('hello')
124 expect(inst.outer).toEqual({
125 b: ['hello']
126 });
127 })
128 })
129})