-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
graph.spec.ts
357 lines (327 loc) · 13.2 KB
/
graph.spec.ts
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import { FormGraph } from '../shared/graph'
import { FormPathPattern, FormPath } from '@uform/shared'
import { IField, IVirtualField } from '../types'
import createForm from '../'
import { FormState } from '../state/form'
test('constructor with strategy',()=>{
function matchStrategy(
pattern: FormPathPattern,
node: IField | IVirtualField
) {
const matchPattern = FormPath.parse(pattern)
return node.getSourceState(
state => matchPattern.match(state.name) || matchPattern.match(state.path)
)
}
const form = createForm()
const vf1 =form.registerVirtualField({ name: 'a' })
const graph = new FormGraph({ matchStrategy })
graph.appendNode("a", vf1)
expect(graph.select("a")).toEqual(vf1)
})
test('constructor without strategy',()=>{
const form = createForm()
const vf1 =form.registerVirtualField({ name: 'a' })
const graph = new FormGraph()
graph.appendNode(vf1.state.path, vf1)
expect(graph.select(vf1.state.path)).toEqual(vf1)
})
test('appendNode',()=>{
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf2 = form.registerVirtualField({ name: 'b' })
const vf2Children = form.registerVirtualField({ name: 'b.b' })
const graph = new FormGraph()
graph.appendNode("", state)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf2.state.path, vf2)
graph.appendNode(vf1Children.state.path, vf1Children)
graph.appendNode(vf2Children.state.path, vf2Children)
// DFS
expect(graph.selectChildren("")[0]).toEqual(vf1)
expect(graph.selectChildren("")[1]).toEqual(vf1Children)
expect(graph.selectChildren("")[2]).toEqual(vf2)
expect(graph.selectChildren("")[3]).toEqual(vf2Children)
expect(graph.selectChildren(vf1.state.path)).toEqual([vf1Children])
expect(graph.selectChildren(vf2.state.path)).toEqual([vf2Children])
expect(graph.selectChildren(vf1Children.state.path)).toEqual([])
expect(graph.selectChildren(vf2Children.state.path)).toEqual([])
})
test('appendNode disOreder',()=>{
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf2 = form.registerVirtualField({ name: 'b' })
const graph = new FormGraph()
// 极端情况,先加子节点,再加父节点不会生效,保证上层代码正常即可
graph.appendNode(vf1Children.state.path, vf1Children)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf2.state.path, vf2)
expect(graph.selectChildren(vf1.state.path)).toEqual([])
expect(graph.selectChildren("")).toEqual([])
graph.appendNode("", state)
expect(graph.selectChildren("")).toEqual([])
})
test('select',()=>{
const form = createForm()
const vf1 =form.registerVirtualField({ name: 'a' })
const graph = new FormGraph()
graph.appendNode(vf1.state.path, vf1)
expect(graph.select(vf1.state.path)).toEqual(vf1)
expect(graph.select("b")).toEqual(undefined)
})
test('get',()=>{
const form = createForm()
const vf1 =form.registerVirtualField({ name: 'a' })
const graph = new FormGraph()
graph.appendNode(vf1.state.path, vf1)
expect(graph.get(vf1.state.path)).toEqual(vf1)
expect(graph.get("b")).toEqual(undefined)
})
test('exist',()=>{
const form = createForm()
const vf1 =form.registerVirtualField({ name: 'a' })
const graph = new FormGraph()
graph.appendNode(vf1.state.path, vf1)
expect(graph.exist(vf1.state.path)).toEqual(true)
expect(graph.exist("b")).toEqual(false)
})
test('replace',()=>{
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf2 = form.registerVirtualField({ name: 'b' })
const graph = new FormGraph()
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf1Children.state.path, vf1Children)
expect(graph.get(vf1.state.path)).toEqual(vf1)
expect(graph.get(vf1Children.state.path)).toEqual(vf1Children)
graph.replace(vf1.state.path, vf2)
expect(graph.get(vf1.state.path)).toEqual(vf2)
// 完全打平, 更换parent不会影响子类
expect(graph.get(vf1Children.state.path)).toEqual(vf1Children)
})
test('remove',()=>{
const form = createForm()
const vf1 =form.registerVirtualField({ name: 'a' })
const graph = new FormGraph()
graph.appendNode(vf1.state.path, vf1)
expect(graph.exist(vf1.state.path)).toEqual(true)
graph.remove(vf1.state.path)
expect(graph.exist(vf1.state.path)).toEqual(false)
})
test('remove deep', () => {
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf1GrandChildren = form.registerVirtualField({ name: 'a.a.a' })
const graph = new FormGraph()
graph.appendNode("", state)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf1Children.state.path, vf1Children)
graph.appendNode(vf1GrandChildren.state.path, vf1GrandChildren)
graph.remove(vf1.state.path)
expect(graph.exist(vf1.state.path)).toEqual(false)
expect(graph.exist(vf1Children.state.path)).toEqual(false)
expect(graph.exist(vf1GrandChildren.state.path)).toEqual(false)
})
test('map',()=>{
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf2 = form.registerVirtualField({ name: 'b' })
const graph = new FormGraph()
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf2.state.path, vf2)
// nodes 是kv结构, map后也是kv结构
const result = graph.map(node => node)
expect(result).toEqual({ a: vf1, b: vf2 })
})
test('reduce',()=>{
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf2 = form.registerVirtualField({ name: 'b' })
const graph = new FormGraph()
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf2.state.path, vf2)
const result = graph.reduce((buf, node) => [...buf].concat(node), [])
expect(result).toEqual([vf1, vf2])
})
test('selectChildren',()=>{
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf2 = form.registerVirtualField({ name: 'b' })
const graph = new FormGraph()
graph.appendNode("", state)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf2.state.path, vf2)
expect(graph.selectChildren("")).toEqual([vf1, vf2])
expect(graph.selectChildren(vf1.state.path)).toEqual([])
expect(graph.selectChildren(vf2.state.path)).toEqual([])
})
test('selectParent',()=>{
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf2 = form.registerVirtualField({ name: 'b' })
const vf2Children = form.registerVirtualField({ name: 'b.b' })
const graph = new FormGraph()
graph.appendNode("", state)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf2.state.path, vf2)
graph.appendNode(vf1Children.state.path, vf1Children)
graph.appendNode(vf2Children.state.path, vf2Children)
expect(graph.selectParent("")).toEqual(undefined)
expect(graph.selectParent(vf1.state.path)).toEqual(state)
expect(graph.selectParent(vf2.state.path)).toEqual(state)
expect(graph.selectParent(vf1Children.state.path)).toEqual(vf1)
expect(graph.selectParent(vf2Children.state.path)).toEqual(vf2)
})
test('eachChildren eacher',()=>{
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf1GrandChildren = form.registerVirtualField({ name: 'a.a.a' })
const graph = new FormGraph()
graph.appendNode("", state)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf1Children.state.path, vf1Children)
graph.appendNode(vf1GrandChildren.state.path, vf1GrandChildren)
// shallow mode: recursion = false
const shallowChilds = []
graph.eachChildren((node, path) => {
shallowChilds.push({ node, path })
}, false)
expect(shallowChilds).toEqual([{ node: vf1, path: FormPath.getPath(vf1.state.path)}])
// deep mode: recursion = true
const deepChilds = []
graph.eachChildren((node, path) => {
deepChilds.push({ node, path })
}, true)
expect(deepChilds).toEqual([
{ node: vf1, path: FormPath.getPath(vf1.state.path)},
{ node: vf1Children, path: FormPath.getPath(vf1Children.state.path)},
{ node: vf1GrandChildren, path: FormPath.getPath(vf1GrandChildren.state.path)},
])
})
test('eachChildren path eacher',()=>{
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf1GrandChildren = form.registerVirtualField({ name: 'a.a.a' })
const graph = new FormGraph()
graph.appendNode("", state)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf1Children.state.path, vf1Children)
graph.appendNode(vf1GrandChildren.state.path, vf1GrandChildren)
// deep mode: recursion = true
const deepChilds = []
graph.eachChildren("a", (node, path) => {
deepChilds.push({ node, path })
}, true)
expect(deepChilds).toEqual([
{ node: vf1Children, path: FormPath.getPath(vf1Children.state.path)},
{ node: vf1GrandChildren, path: FormPath.getPath(vf1GrandChildren.state.path)},
])
})
test('eachChildren path selector eacher',()=>{
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf1GrandChildren = form.registerVirtualField({ name: 'a.a.a' })
const graph = new FormGraph()
graph.appendNode("", state)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf1Children.state.path, vf1Children)
graph.appendNode(vf1GrandChildren.state.path, vf1GrandChildren)
// shallow mode: recursion = false
const shallowChilds = []
graph.eachChildren("a", "a.a.a", (node, path) => {
shallowChilds.push({ node, path })
}, true)
expect(shallowChilds).toEqual([])
// deep mode: recursion = true
const deepChilds = []
graph.eachChildren("a", "a.a", (node, path) => {
deepChilds.push({ node, path })
}, true)
expect(deepChilds).toEqual([
{ node: vf1Children, path: FormPath.getPath(vf1Children.state.path)},
])
})
test('eachParent',()=>{
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf1GrandChildren = form.registerVirtualField({ name: 'a.a.a' })
const graph = new FormGraph()
graph.appendNode("", state)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf1Children.state.path, vf1Children)
graph.appendNode(vf1GrandChildren.state.path, vf1GrandChildren)
const rootEacher = jest.fn()
graph.eachParent("", rootEacher)
expect(rootEacher).toBeCalledTimes(0)
//expect(rootEacher).toBeCalledWith(state, FormPath.getPath(""))
const vf1Eacher = jest.fn()
graph.eachParent(vf1.state.path, vf1Eacher)
expect(vf1Eacher).toBeCalledTimes(1)
const vf1CEacher = jest.fn()
graph.eachParent(vf1Children.state.path, vf1CEacher)
expect(vf1CEacher).toBeCalledTimes(2)
const vf1GEacher = jest.fn()
graph.eachParent(vf1GrandChildren.state.path, vf1GEacher)
expect(vf1GEacher).toBeCalledTimes(3)
})
test('getLatestParent',()=>{
const state = new FormState({})
const form = createForm()
const vf1 = form.registerVirtualField({ name: 'a' })
const vf1Children = form.registerVirtualField({ name: 'a.a' })
const vf1GrandChildren = form.registerVirtualField({ name: 'a.a.a' })
const vf2 = form.registerVirtualField({ name: 'b' })
const vf2Children = form.registerVirtualField({ name: 'b.b' })
const graph = new FormGraph()
graph.appendNode("", state)
graph.appendNode(vf1.state.path, vf1)
graph.appendNode(vf2.state.path, vf2)
graph.appendNode(vf1Children.state.path, vf1Children)
graph.appendNode(vf1GrandChildren.state.path, vf1GrandChildren)
graph.appendNode(vf2Children.state.path, vf2Children)
expect(graph.getLatestParent("")).toEqual(undefined)
const rootPath = FormPath.getPath("")
const v1Path = FormPath.getPath(vf1.state.path)
const v2Path = FormPath.getPath(vf2.state.path)
const v1CPath = FormPath.getPath(vf1Children.state.path)
const v2CPath = FormPath.getPath(vf2Children.state.path)
const root = {
path: rootPath,
ref: { path: rootPath, children: [v1Path, v2Path] }
}
expect(graph.getLatestParent(vf1.state.path)).toEqual(root)
expect(graph.getLatestParent(vf2.state.path)).toEqual(root)
expect(graph.getLatestParent(vf1Children.state.path)).toEqual({
path: v1Path,
ref: { path: v1Path, children: [v1CPath], parent: graph.getLatestParent(vf1.state.path).ref }
})
expect(graph.getLatestParent(vf2Children.state.path)).toEqual({
path: v2Path,
ref: { path: v2Path, children: [v2CPath], parent: graph.getLatestParent(vf2.state.path).ref }
})
expect(graph.getLatestParent(vf1GrandChildren.state.path)).toEqual({
path: v1CPath,
ref: { path: v1CPath,
children: [FormPath.getPath(vf1GrandChildren.state.path)],
parent: graph.getLatestParent(vf1Children.state.path).ref
}
})
})