-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
tree.spec.ts
501 lines (458 loc) · 14.5 KB
/
tree.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import { describe, expect, it } from 'vitest'
import { DEFAULT_OPTIONS } from '../options'
import { createPrefixTree } from './tree'
import { TreeNodeType } from './treeNodeValue'
describe('Tree', () => {
it('creates an empty tree', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
expect(tree.children.size).toBe(0)
})
it('creates a tree with a single static path', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('foo.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get('foo')!
expect(child).toBeDefined()
expect(child.value).toMatchObject({
rawSegment: 'foo',
path: '/foo',
_type: TreeNodeType.static,
})
expect(child.children.size).toBe(0)
})
it('creates a tree with a single param', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('[id].vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get('[id]')!
expect(child).toBeDefined()
expect(child.value).toMatchObject({
rawSegment: '[id]',
params: [{ paramName: 'id' }],
path: '/:id',
_type: TreeNodeType.param,
})
expect(child.children.size).toBe(0)
})
it('separate param names from static segments', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('[id]_a')
tree.insert('[a]e[b]f')
expect(tree.children.get('[id]_a')!.value).toMatchObject({
rawSegment: '[id]_a',
params: [{ paramName: 'id' }],
path: '/:id()_a',
_type: TreeNodeType.param,
})
expect(tree.children.get('[a]e[b]f')!.value).toMatchObject({
rawSegment: '[a]e[b]f',
params: [{ paramName: 'a' }, { paramName: 'b' }],
path: '/:a()e:b()f',
_type: TreeNodeType.param,
})
})
it('creates params in nested files', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const nestedId = tree.insert('nested/[id].vue')
expect(nestedId.value.isParam()).toBe(true)
expect(nestedId.params).toEqual([
expect.objectContaining({
isSplat: false,
modifier: '',
optional: false,
paramName: 'id',
repeatable: false,
}),
])
const nestedAId = tree.insert('nested/a/[id].vue')
expect(nestedAId.value.isParam()).toBe(true)
expect(nestedAId.params).toEqual([
expect.objectContaining({
isSplat: false,
modifier: '',
optional: false,
paramName: 'id',
repeatable: false,
}),
])
})
it('creates params in nested folders', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
let node = tree.insert('nested/[id]/index.vue')
const id = tree.children.get('nested')!.children.get('[id]')!
expect(id.value.isParam()).toBe(true)
expect(id.params).toEqual([
expect.objectContaining({
isSplat: false,
modifier: '',
optional: false,
paramName: 'id',
repeatable: false,
}),
])
expect(node.value.isParam()).toBe(false)
expect(node.params).toEqual([
expect.objectContaining({
isSplat: false,
modifier: '',
optional: false,
paramName: 'id',
repeatable: false,
}),
])
node = tree.insert('nested/[a]/other.vue')
expect(node.value.isParam()).toBe(false)
expect(node.params).toEqual([
expect.objectContaining({
isSplat: false,
modifier: '',
optional: false,
paramName: 'a',
repeatable: false,
}),
])
node = tree.insert('nested/a/[id]/index.vue')
expect(node.value.isParam()).toBe(false)
expect(node.params).toEqual([
expect.objectContaining({
isSplat: false,
modifier: '',
optional: false,
paramName: 'id',
repeatable: false,
}),
])
})
it('handles repeatable params one or more', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('[id]+.vue')
expect(tree.children.get('[id]+')!.value).toMatchObject({
rawSegment: '[id]+',
params: [
{
paramName: 'id',
repeatable: true,
optional: false,
modifier: '+',
},
],
path: '/:id+',
_type: TreeNodeType.param,
})
})
it('handles repeatable params zero or more', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('[[id]]+.vue')
expect(tree.children.get('[[id]]+')!.value).toMatchObject({
rawSegment: '[[id]]+',
params: [
{
paramName: 'id',
repeatable: true,
optional: true,
modifier: '*',
},
],
path: '/:id*',
_type: TreeNodeType.param,
})
})
it('handles optional params', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('[[id]].vue')
expect(tree.children.get('[[id]]')!.value).toMatchObject({
rawSegment: '[[id]]',
params: [
{
paramName: 'id',
repeatable: false,
optional: true,
modifier: '?',
},
],
path: '/:id?',
_type: TreeNodeType.param,
})
})
it('handles named views', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('index.vue')
tree.insert('index@a.vue')
tree.insert('index@b.vue')
tree.insert('nested/foo@a.vue')
tree.insert('nested/foo@b.vue')
tree.insert('nested/[id]@a.vue')
tree.insert('nested/[id]@b.vue')
tree.insert('not.nested.path@a.vue')
tree.insert('not.nested.path@b.vue')
tree.insert('deep/not.nested.path@a.vue')
tree.insert('deep/not.nested.path@b.vue')
expect([...tree.children.get('index')!.value.components.keys()]).toEqual([
'default',
'a',
'b',
])
expect([
...tree.children
.get('nested')!
.children.get('foo')!
.value.components.keys(),
]).toEqual(['a', 'b'])
expect([
...tree.children
.get('nested')!
.children.get('[id]')!
.value.components.keys(),
]).toEqual(['a', 'b'])
expect([
...tree.children.get('not.nested.path')!.value.components.keys(),
]).toEqual(['a', 'b'])
expect([
...tree.children
.get('deep')!
.children.get('not.nested.path')!
.value.components.keys(),
]).toEqual(['a', 'b'])
})
it('handles single named views that are not default', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('index@a.vue')
expect([...tree.children.get('index')!.value.components.keys()]).toEqual([
'a',
])
})
it('removes the node after all named views', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('index.vue')
tree.insert('index@a.vue')
expect(tree.children.get('index')).toBeDefined()
tree.remove('index@a.vue')
expect(tree.children.get('index')).toBeDefined()
tree.remove('index.vue')
expect(tree.children.get('index')).toBeUndefined()
})
it('can remove itself from the tree', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('index.vue').insert('nested.vue')
tree.insert('a.vue').insert('nested.vue')
tree.insert('b.vue')
expect(tree.children.size).toBe(3)
tree.children.get('a')!.delete()
expect(tree.children.size).toBe(2)
tree.children.get('index')!.delete()
expect(tree.children.size).toBe(1)
})
it('handles multiple params', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('[a]-[b].vue')
tree.insert('o[a]-[b]c.vue')
tree.insert('o[a][b]c.vue')
tree.insert('nested/o[a][b]c.vue')
expect(tree.children.size).toBe(4)
expect(tree.children.get('[a]-[b]')!.value).toMatchObject({
pathSegment: ':a-:b',
})
})
it('creates a tree of nested routes', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('index.vue')
tree.insert('a/index.vue')
tree.insert('a/b/index.vue')
expect(Array.from(tree.children.keys())).toEqual(['index', 'a'])
const index = tree.children.get('index')!
expect(index.value).toMatchObject({
rawSegment: 'index',
// the root should have a '/' instead of '' for the autocompletion
path: '/',
})
expect(index).toBeDefined()
const a = tree.children.get('a')!
expect(a).toBeDefined()
expect(a.value.components.get('default')).toBeUndefined()
expect(a.value).toMatchObject({
rawSegment: 'a',
path: '/a',
})
expect(Array.from(a.children.keys())).toEqual(['index', 'b'])
const aIndex = a.children.get('index')!
expect(aIndex).toBeDefined()
expect(Array.from(aIndex.children.keys())).toEqual([])
expect(aIndex.value).toMatchObject({
rawSegment: 'index',
path: '/a',
})
tree.insert('a.vue')
expect(a.value.components.get('default')).toBe('a.vue')
expect(a.value).toMatchObject({
rawSegment: 'a',
path: '/a',
})
})
it('handles a modifier for single params', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('[id]+.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get('[id]+')!
expect(child).toBeDefined()
expect(child.value).toMatchObject({
rawSegment: '[id]+',
params: [{ paramName: 'id', modifier: '+' }],
path: '/:id+',
pathSegment: ':id+',
_type: TreeNodeType.param,
})
expect(child.children.size).toBe(0)
})
it('removes nodes', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('foo.vue')
tree.insert('[id].vue')
tree.remove('foo.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get('[id]')!
expect(child).toBeDefined()
expect(child.value).toMatchObject({
rawSegment: '[id]',
params: [{ paramName: 'id' }],
path: '/:id',
pathSegment: ':id',
})
expect(child.children.size).toBe(0)
})
it('removes empty folders', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('a/b/c/d.vue')
expect(tree.children.size).toBe(1)
tree.remove('a/b/c/d.vue')
expect(tree.children.size).toBe(0)
})
it('insert returns the node', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const a = tree.insert('a.vue')
expect(tree.children.get('a')).toBe(a)
const bC = tree.insert('b/c.vue')
expect(tree.children.get('b')!.children.get('c')).toBe(bC)
const bCD = tree.insert('b/c/d.vue')
expect(tree.children.get('b')!.children.get('c')!.children.get('d')).toBe(
bCD
)
})
it('keeps parent with file but no children', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('a/b/c/d.vue')
tree.insert('a/b.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get('a')!.children.get('b')!
expect(child).toBeDefined()
expect(child.children.size).toBe(1)
tree.remove('a/b/c/d.vue')
expect(tree.children.size).toBe(1)
expect(tree.children.get('a')!.children.size).toBe(1)
expect(child.children.size).toBe(0)
})
it('allows a custom name', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
let node = tree.insert('[a]-[b].vue')
node.value.setOverride('', {
name: 'custom',
})
expect(node.name).toBe('custom')
node = tree.insert('auth/login.vue')
node.value.setOverride('', {
name: 'custom-child',
})
expect(node.name).toBe('custom-child')
})
it('allows a custom path', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
let node = tree.insert('[a]-[b].vue')
node.value.setOverride('', {
path: '/custom',
})
expect(node.path).toBe('/custom')
expect(node.fullPath).toBe('/custom')
node = tree.insert('auth/login.vue')
node.value.setOverride('', {
path: '/custom-child',
})
expect(node.path).toBe('/custom-child')
expect(node.fullPath).toBe('/custom-child')
})
it('removes trailing slash from path but not from name', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('a/index.vue')
tree.insert('a/a.vue')
let child = tree.children.get('a')!
expect(child).toBeDefined()
expect(child.fullPath).toBe('/a')
child = tree.children.get('a')!.children.get('index')!
expect(child).toBeDefined()
expect(child.name).toBe('/a/')
expect(child.fullPath).toBe('/a')
// it stays the same with a parent component in the parent route record
tree.insert('a.vue')
child = tree.children.get('a')!.children.get('index')!
expect(child).toBeDefined()
expect(child.name).toBe('/a/')
expect(child.fullPath).toBe('/a')
})
it('handles long extensions', () => {
const tree = createPrefixTree({
...DEFAULT_OPTIONS,
extensions: ['.page.vue'],
})
tree.insert('a.page.vue')
tree.insert('nested/b/c.page.vue')
expect(tree.children.size).toBe(2)
const a = tree.children.get('a')!
expect(a).toBeDefined()
expect(a.value.components.get('default')).toBe('a.page.vue')
expect(a.fullPath).toBe('/a')
const nested = tree.children.get('nested')!
expect(nested).toBeDefined()
expect(nested.children.size).toBe(1)
const b = nested.children.get('b')!
expect(b).toBeDefined()
expect(b.children.size).toBe(1)
const c = b.children.get('c')!
expect(c).toBeDefined()
expect(c.value.components.get('default')).toBe('nested/b/c.page.vue')
expect(c.fullPath).toBe('/nested/b/c')
tree.insert('a/nested.page.vue')
const aNested = a.children.get('nested')!
expect(aNested).toBeDefined()
expect(aNested.value.components.get('default')).toBe('a/nested.page.vue')
expect(aNested.fullPath).toBe('/a/nested')
})
describe('dot nesting', () => {
it('transforms dots into nested routes by default', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
tree.insert('users.new.vue')
expect(tree.children.size).toBe(1)
const users = tree.children.get('users.new')!
expect(users.value).toMatchObject({
rawSegment: 'users.new',
pathSegment: 'users/new',
path: '/users/new',
_type: TreeNodeType.static,
})
})
it.only('can ignore dot nesting', () => {
const tree = createPrefixTree({
...DEFAULT_OPTIONS,
pathParser: {
dotNesting: false,
},
})
tree.insert('1.2.3-lesson.vue')
expect(tree.children.size).toBe(1)
const lesson = tree.children.get('1.2.3-lesson')!
expect(lesson.value).toMatchObject({
rawSegment: '1.2.3-lesson',
pathSegment: '1.2.3-lesson',
path: '/1.2.3-lesson',
_type: TreeNodeType.static,
})
})
})
})