-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
183 lines (162 loc) · 4.75 KB
/
index.test.js
File metadata and controls
183 lines (162 loc) · 4.75 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
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
const {
dig, cursor,
setOptions, setFormatter,
_withOptions, _disableTrackKey
} = require('./index');
const object = {
owner: {
id: 14,
name: 'Jason',
age: 19
},
client: {
name: 'Yuzy',
job: "Student",
},
users: [
{
id: 1,
name: 'Oskang09',
age: 20,
email: 'developer.oskang@gmail.com',
},
{
id: 2,
name: 'Roger',
age: 24,
special: true,
}
],
community: [
'Oskang09',
'Roger',
'Jason'
]
};
test('Should dot string notation accepted.', () => {
const res = dig(object, 'owner.name');
expect(res).toBe(object.owner.name);
});
test('Should object dot string notation accepted.', () => {
const res = dig(object, { name: 'owner.name' });
expect(res).toStrictEqual({ name: object.owner.name });
});
test('Should array dot string notation accepted.', () => {
const res = dig(object, ['owner.name', 'owner.id']);
expect(res).toStrictEqual(['Jason', 14]);
});
test('Should equal symbol return object if target is array object based on condition.', () => {
const res = dig(object, 'users.id=1');
expect(res).toBe(object.users[0]);
});
test('Should equal symbol return array if target is array based on condition.', () => {
const res = dig(object, 'community.=Jason');
expect(res).toBe('Jason');
});
test('Should equal symbol return null when target not exists.', () => {
const res = dig(object, 'users.id=3');
expect(res).toBe(null);
});
test('Should arrayMap symbol return specific key', () => {
const res = dig(object, 'users.*.name');
expect(res).toStrictEqual(['Oskang09', 'Roger']);
});
test('Should comma symbol work with arrayMap symbol return multiple keys', () => {
const res = dig(object, 'users.*.name,id');
expect(res).toStrictEqual([
{ name: 'Oskang09', id: 1 },
{ name: 'Roger', id: 2 }
]);
});
test('Should pipe symbol return first truth value', () => {
const res = dig(object, 'client.address|name|job');
expect(res).toBe("Yuzy")
});
test('Should return null if target exists.', () => {
expect(dig(object, 'owner.name.id')).toBe(null);
});
test('Should return null if object is falsy value.', () => {
expect(dig(null, 'owner.name')).toBe(null);
});
test('Should run invoke if function and data found.', () => {
const log = jest.fn();
setFormatter({
logger: function (data) {
log(data);
return data;
}
});
dig(object, 'users.*.name&logger');
expect(log.mock.calls.length).toBe(2);
expect(log.mock.calls[0][0]).toBe("Oskang09");
expect(log.mock.calls[1][0]).toBe("Roger");
});
test('Should skip if function not found.', () => {
const log = jest.fn();
setFormatter({
logger: function (data) {
log(data);
return data;
}
});
dig(object, 'users.*.age&other,special&logger');
expect(log.mock.calls.length).toBe(2);
expect(log.mock.calls[0][0]).toBe(null);
expect(log.mock.calls[1][0]).toBe(true);
});
test('Should `cursor` build options object', () => {
const opts = cursor();
expect(opts.cursor).not.toBe(null);
})
test(`Should build cache when cursor not null`, () => {
const opts = cursor();
const object = {
name: 'Yuzy',
job: "Student",
friends: [
{
name: "Oska"
},
{
name: "WangLin"
}
]
};
const result = dig(object, 'name', opts);
dig(object, "friends.*.name", opts);
expect(result).toBe('Yuzy');
expect(opts.cursor).toStrictEqual({
name: 'Yuzy',
friends: object.friends,
"friends.*": object.friends,
"friends.*.name": ["Oska", "WangLin"]
});
});
test(`Should get from cache when cursor not null and key exist`, () => {
const opts = cursor();
const object = {
name: 'Yuzy',
job: "Student",
client: {
name: "Oska"
},
};
const result = dig(object, 'client.name', opts);
dig(object, 'client.name', opts)
expect(result).toBe('Oska');
expect(opts.cursor).toStrictEqual({
client: object.client,
'client.name': 'Oska'
});
});
test('Should `withOptions` set disableTracker on options', () => {
const options = cursor();
const newOptions = _withOptions(options, true);
const newOptions2 = _withOptions(options, false);
expect(newOptions[_disableTrackKey]).toBe(true);
expect(newOptions2[_disableTrackKey]).toBe(undefined);
});
test('Should `setOptions` update options object.', () => {
setOptions({ seperator: '->' });
expect(dig(object, 'owner->name')).toBe('Jason');
});