-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathutils.test.js
More file actions
150 lines (135 loc) · 3.89 KB
/
utils.test.js
File metadata and controls
150 lines (135 loc) · 3.89 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
import get from '../src/utils/get';
import set from '../src/utils/set';
import pickAttrs from '../src/pickAttrs';
describe('utils', () => {
it('get', () => {
// Empty path
expect(get(null, [])).toEqual(null);
expect(get(undefined, [])).toEqual(undefined);
expect(get(1, [])).toEqual(1);
// Part path
expect(get([1, 2], ['0'])).toEqual(1);
expect(get([1, 2], ['0', '1'])).toEqual(undefined);
expect(get({ a: { b: { c: 93 } } }, ['a', 'b'])).toEqual({ c: 93 });
expect(get({ a: { b: { c: 93 } } }, ['a', 'b', 'c'])).toEqual(93);
expect(get({ a: { b: { c: 93 } } }, ['a', 'b', 'c', 'd'])).toEqual(
undefined,
);
});
describe('set', () => {
it('basic', () => {
expect(set(null, [], 233)).toEqual(233);
expect(set({}, ['light'], 'bamboo')).toEqual({ light: 'bamboo' });
expect(set({}, ['light', 'bamboo'], 'generate')).toEqual({
light: { bamboo: 'generate' },
});
expect(
set({ other: { next: 233 } }, ['light', 'bamboo'], 'generate'),
).toEqual({
other: { next: 233 },
light: { bamboo: 'generate' },
});
expect(set([0, 1, 2], [1, 'light', 'bamboo'], 'next')).toEqual([
0,
{ light: { bamboo: 'next' } },
2,
]);
expect(
set({ light: 'bamboo', list: [0, 1, 2] }, ['list', '1'], 233),
).toEqual({
light: 'bamboo',
list: [0, 233, 2],
});
expect(set([[[[[0]]]]], [0, 0, 0, 0, 0], 'light')).toEqual([
[[[['light']]]],
]);
expect(set([[[[[0]]]]], [0, 0, 0, 0, 0, 0], 'bamboo')).toEqual([
[[[[['bamboo']]]]],
]);
});
it('remove if undefined', () => {
// Skip not exist path
expect(set({}, ['notExist'], undefined, true)).toEqual({});
// Delete value
const target = set(
{ keep: { light: 2333 } },
['keep', 'light'],
undefined,
true,
);
expect(target).toEqual({ keep: {} });
expect('light' in target.keep).toBeFalsy();
// Mid path not exist
const midTgt = set(
{ lv1: { lv2: {} } },
['lv1', 'lv2', 'lv3'],
undefined,
true,
);
expect(midTgt).toEqual({ lv1: { lv2: {} } });
expect('lv3' in midTgt.lv1.lv2).toBeFalsy();
// Long path not exist
const longNotExistTgt = set(
{ lv1: { lv2: {} } },
['lv1', 'lv2', 'lv3', 'lv4'],
undefined,
true,
);
expect(longNotExistTgt).toEqual({ lv1: { lv2: {} } });
expect('lv3' in longNotExistTgt.lv1.lv2).toBeFalsy();
// Long path remove
const longTgt = set(
{ lv1: { lv2: { lv3: { lv4: { lv: 5 } } } } },
['lv1', 'lv2', 'lv3', 'lv4'],
undefined,
true,
);
expect(longTgt).toEqual({ lv1: { lv2: { lv3: {} } } });
expect('lv4' in longTgt.lv1.lv2.lv3).toBeFalsy();
});
});
describe('pickAttrs', () => {
const originProps = {
onClick: null,
checked: true,
'data-my': 1,
'aria-this': 2,
skip: true,
role: 'button',
};
it('default', () => {
expect(pickAttrs(originProps)).toEqual({
onClick: null,
checked: true,
'data-my': 1,
'aria-this': 2,
role: 'button',
});
});
it('aria only', () => {
expect(pickAttrs(originProps, true)).toEqual({
'aria-this': 2,
role: 'button',
});
});
it('attr only', () => {
expect(pickAttrs(originProps, { attr: true })).toEqual({
onClick: null,
checked: true,
role: 'button',
});
});
it('aria & data', () => {
expect(pickAttrs(originProps, { aria: true, data: true })).toEqual({
'data-my': 1,
'aria-this': 2,
role: 'button',
});
});
it('data only', () => {
expect(pickAttrs(originProps, { data: true })).toEqual({
'data-my': 1,
});
});
});
});