/
mutator-spec.js
296 lines (274 loc) · 7.65 KB
/
mutator-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* global jest, beforeEach, describe, it, expect */
jest.unmock('../../../src/utils/mutator');
import Mutator from '../../../src/utils/mutator';
describe('mutator tests', () => {
let newMutator = (fn) => {
let target = {};
let mutator = new Mutator(target);
return () => fn(target, mutator);
};
describe('traversal', () => {
it(
'in should allow navigating in object',
newMutator((target, mutator) => {
target.prop1 = { prop2: 10 };
expect(mutator.in('prop1').getValue().prop2).toEqual(10);
expect(
mutator
.in('prop1')
.in('prop2')
.getValue(),
).toEqual(10);
}),
);
it(
'up should allow navigating in object',
newMutator((target, mutator) => {
target.prop1 = { prop2: 10 };
expect(
mutator
.in('prop1')
.in('prop2')
.up()
.getValue().prop2,
).toEqual(10);
}),
);
it(
'in & up shoul work with arrays',
newMutator((target, mutator) => {
target.propArray = [5, 10];
expect(
mutator
.in('propArray')
.in(1)
.getValue(),
).toEqual(10);
expect(
mutator
.in('propArray')
.in(1)
.up()
.getValue()[1],
).toEqual(10);
}),
);
});
describe('insert', () => {
it(
'should add a field to object',
newMutator((target, mutator) => {
mutator.insert('myField', 10);
expect(target.myField).toEqual(10);
}),
);
it(
'should be able to add a nested field',
newMutator((target, mutator) => {
mutator
.insert('someField', {})
.in('someField')
.insert('nested', 10);
expect(target.someField.nested).toEqual(10);
}),
);
it(
'should be able to add item to an array at index',
newMutator((target, mutator) => {
mutator
.insert('someFieldArray', [])
.in('someFieldArray')
.insert(0, 5)
.insert(1, 10);
expect(target.someFieldArray[0]).toEqual(5);
expect(target.someFieldArray[1]).toEqual(10);
}),
);
});
describe('delete', () => {
it(
'should be able to delete an item',
newMutator((target, mutator) => {
target.someKey = 5;
mutator.in('someKey').delete();
expect(target.someKey).toEqual(undefined);
}),
);
it(
'should be able to delete nested item',
newMutator((target, mutator) => {
target.someKey = { a: 10 };
expect(Object.keys(target.someKey).length).toEqual(1);
mutator
.in('someKey')
.in('a')
.delete();
expect(Object.keys(target.someKey).length).toEqual(0);
}),
);
it(
'should be able to delete an item from array',
newMutator((target, mutator) => {
target.someKey = [0, 1, 2];
mutator
.in('someKey')
.in(1)
.delete();
expect(target.someKey).toEqual([0, 2]);
}),
);
});
describe('update value', () => {
it(
'should be able to update a field value',
newMutator((target, mutator) => {
target.someKey = 5;
mutator.in('someKey').updateValue(10);
expect(target.someKey).toEqual(10);
}),
);
it(
'should be able to update a value in array',
newMutator((target, mutator) => {
target.someArray = [10, 20];
mutator
.in('someArray')
.in(1)
.updateValue(30);
expect(target.someArray[1]).toEqual(30);
}),
);
});
describe('adjust value', () => {
it(
'should be able to adjust a field value',
newMutator((target, mutator) => {
target.someKey = 5;
mutator.in('someKey').adjustValue(x => x + 5);
expect(target.someKey).toEqual(10);
}),
);
it(
'should be able to adjust a value in array',
newMutator((target, mutator) => {
target.someArray = [10, 20];
mutator
.in('someArray')
.in(1)
.adjustValue(x => x + 10);
expect(target.someArray[1]).toEqual(30);
}),
);
});
describe('update key', () => {
it(
'should be able to update a key',
newMutator((target, mutator) => {
target.someKey = 5;
mutator.in('someKey').updateKey('newKey');
expect(target.someKey).toEqual(undefined);
expect(target.newKey).toEqual(5);
}),
);
it(
'should update the mutator path after key is updated',
newMutator((target, mutator) => {
target.someKey = 5;
mutator
.in('someKey')
.updateKey('newKey')
.updateValue(10);
expect(target.newKey).toEqual(10);
}),
);
it(
'should preserve order in object',
newMutator((target, mutator) => {
target.largeObj = { a: 5, b: 10, c: 30, d: 40 };
mutator
.in('largeObj')
.in('c')
.updateKey('e');
expect(Object.keys(target.largeObj)).toEqual(['a', 'b', 'e', 'd']);
}),
);
});
describe('prepend', () => {
it(
'should be able to insert item to the start of an array',
newMutator((target, mutator) => {
target.someProp = [2, 3, 4];
mutator.in('someProp').prepend(1);
expect(target.someProp).toEqual([1, 2, 3, 4]);
}),
);
});
describe('append', () => {
it(
'should be able to insert item to the end of an array',
newMutator((target, mutator) => {
target.someProp = [1, 2, 3];
mutator.in('someProp').append(4);
expect(target.someProp).toEqual([1, 2, 3, 4]);
}),
);
});
describe('replace keys', () => {
it(
'should be able replace two field values',
newMutator((target, mutator) => {
target.someKey = 5;
target.otherKey = 10;
mutator.replaceKeys('someKey', 'otherKey');
expect(target.someKey).toEqual(10);
expect(target.otherKey).toEqual(5);
}),
);
it(
'should work on arrays',
newMutator((target, mutator) => {
target.myArray = [5, 10, 15];
mutator.in('myArray').replaceKeys(0, '2');
expect(target.myArray).toEqual([15, 10, 5]);
}),
);
});
describe('stateless mutator', () => {
it('should send a mutation event with updated value', () => {
let target = { a: 5 };
let mock = jest.fn();
let mutator = Mutator.stateless(() => target, mock);
mutator.in('a').updateValue(10);
expect(mock.mock.calls.length).toEqual(1);
expect(mock.mock.calls[0][0].a).toEqual(10);
});
it('should not change the orignal target', () => {
let target = { a: 5 };
let mock = jest.fn();
let mutator = Mutator.stateless(() => target, mock);
mutator.in('a').updateValue(10);
expect(mock.mock.calls[0][0].a).toEqual(10);
expect(target.a).toEqual(5);
});
it('apply should call method onMutation event only once per transcation', () => {
let target = { a: 1, b: 1 };
let mock = jest.fn();
let mutator = Mutator.stateless(() => target, mock);
mutator.in('a').updateValue(1);
mutator.in('b').updateValue(1);
expect(mock.mock.calls.length).toEqual(2);
mutator.apply(m =>
m
.in('a')
.updateValue(2)
.up()
.in('b')
.updateValue(2),
);
expect(mock.mock.calls.length).toEqual(3);
const applyMutation = mock.mock.calls[2][0];
expect(applyMutation.a).toEqual(2);
expect(applyMutation.b).toEqual(2);
});
});
});