-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath-pointer.spec.js
More file actions
113 lines (82 loc) · 2.59 KB
/
path-pointer.spec.js
File metadata and controls
113 lines (82 loc) · 2.59 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
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
import PathPointer, { createGetter, createSetter } from './path-pointer.js';
describe('PathPointer', () => {
it('should get "one.nested" prop', () => {
const o = { one: true };
const v = createGetter(o, 'one')();
expect(v).toBe(o.one);
})
it('should get "one.two.nested" prop', () => {
const o = { one: { two: true } };
const v = createGetter(o, 'one.two')();
expect(v).toBe(o.one.two);
})
it('should get "one.two.undefined" prop', () => {
const o = { one: { two: true } };
const v = createGetter(o, 'one.two.undefined')();
expect(v).toBe(o.one.two.undefined);
})
it('should get "one.two.null" prop', () => {
const o = { one: { two: null } };
const v = createGetter(o, 'one.two.undefined')();
expect(v).toBe(undefined);
})
it('should set "one.nested" prop', () => {
const o = {};
const v = createSetter(o, 'one')(true);
expect(v).toBe(o);
expect(o.one).toBe(true);
})
it('should set "one.two.nested" prop', () => {
const o = { one: null };
const v = createSetter(o, 'one.two')(true);
expect(v).toBe(o.one);
expect(o.one.two).toBe(true);
})
it('should set "one[1].nested" prop', () => {
const o = { one: null };
const v = createSetter(o, 'one[1].value')(true);
expect(v).toBe(o.one[1]);
expect(o.one[1].value).toBe(true);
})
it('should set "[1].value" prop', () => {
const o = { one: null };
const v = createSetter(o, '[1].value')(true);
expect(v).toBe(o[1]);
expect(o[1].value).toBe(true);
})
it('should construct new PathPointer(object, path)', () => {
const o = { one: { two: true } };
const pointer = new PathPointer(o, 'one.two');
pointer.set(2);
const v = pointer.get();
expect(v).toBe(2);
})
it('should construct new PathPointer(path) \\wo object', () => {
const o = { one: { two: true } };
const pointer = new PathPointer('one.two');
pointer.set(o, 2);
const v = pointer.get(o);
expect(v).toBe(2);
})
it('should get quoted "one[\'two\'] prop"', () => {
const o = { one: { two: true } };
const v = createGetter(o, 'one[\'two\']')();
expect(v).toBe(true);
})
it('should push into "one[]"', () => {
const o = {};
const setter = createSetter(o, 'one[]');
setter(1);
expect(o.one[0]).toBe(1);
setter(2);
expect(o.one[1]).toBe(2);
})
it('should set {value} pushed into "one[]"', () => {
const o = {};
const setter = createSetter(o, 'one[].value');
setter(1);
expect(o.one[0].value).toBe(1);
setter(2);
expect(o.one[1].value).toBe(2);
})
});