-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextToArray.test.js
More file actions
84 lines (70 loc) · 2.53 KB
/
Copy pathextToArray.test.js
File metadata and controls
84 lines (70 loc) · 2.53 KB
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
import {
extToArray,
deepPatch,
} from '../index';
const testCases = [
[[['a.b.c', 25]], [['a.b.c', 25]]],
[[['a.b.c']], [['a.b.c']]],
[{}, []],
[[], []],
[{ 'a.a1': 100, 10: 1000 }, [['10', 1000], ['a.a1', 100]]],
[{ 'a.a1': { z: 10, j: 50 } }, [['a.a1', { z: 10, j: 50 }]]],
[{ 'a.a1': undefined }, [['a.a1']]],
[{ 'a.a1': null }, [['a.a1', null]]],
[{ 'a.a1': deepPatch({ z: 10, z2: { z21: 100 } }) }, [['a.a1.z', 10], ['a.a1.z2.z21', 100]]],
[[['a.a1', deepPatch({ z: 10, z2: { z21: 100 } })]], [['a.a1.z', 10], ['a.a1.z2.z21', 100]]],
[[[['a', 'a1'], deepPatch({ z: 10, z2: { z21: 100 } })]], [['a.a1.z', 10], ['a.a1.z2.z21', 100]]],
[{ '': deepPatch({ z: 10, z2: { z21: 100 } }) }, [['z', 10], ['z2.z21', 100]]],
[[['', deepPatch({ z: 10, z2: { z21: 100 } })]], [['z', 10], ['z2.z21', 100]]],
[[[null, deepPatch({ z: 10, z2: { z21: 100 } })]], [['z', 10], ['z2.z21', 100]]],
[[[[], deepPatch({ z: 10, z2: { z21: 100 } })]], [['z', 10], ['z2.z21', 100]]],
[[deepPatch({ z: 10, z2: { z21: 100 } })], [['z', 10], ['z2.z21', 100]]],
[deepPatch({ z: 10, z2: { z21: 100 } }), [['z', 10], ['z2.z21', 100]]],
[{
'a.a1': deepPatch({ z: 10, z2: { z21: 100 } }),
'a.a1.z': 9999,
}, [['a.a1.z', 10], ['a.a1.z2.z21', 100], ['a.a1.z', 9999]]],
];
const errorCases = [
[null, []],
[undefined, []],
[1, []],
['1123', []],
[11.5, []],
[new Error(), []],
];
const consoleError = console.error;
describe('extToArray', () => {
describe('returns error', () => {
beforeAll(() => {
jest.spyOn(console, 'error');
console.error.mockImplementation((error) => {
if (error.message.startsWith('Changes should be Object or Array')) {
// eslint-disable-next-line no-console
console.debug(`Expected test error: ${error.message}`);
} else {
consoleError(error);
}
});
});
afterAll(() => {
console.error.mockReset();
});
test.each(errorCases)('extToArray(%j) === %j', (data, expected) => {
expect(extToArray(data)).toEqual(expected);
});
});
test.each(testCases)('extToArray(%j) === %j', (data, expected) => {
expect(extToArray(data)).toEqual(expected);
});
test('should return the same array', () => {
const data = [['a', 10], ['b'], ['c.11', 20]];
expect(extToArray(data)).toBe(data);
});
test('should return array with the same Object', () => {
const value = { z: 1, z2: 2 };
const data = { 'a.a2': value };
const result = extToArray(data);
expect(result[0][1]).toBe(value);
});
});