-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
tree.spec.ts
196 lines (183 loc) · 5.87 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
import { describe, expect, it } from 'vitest'
import { DEFAULT_OPTIONS } from '../options'
import { createPrefixTree } from './tree'
import { TreeLeafType } from './treeLeafValue'
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: TreeLeafType.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: TreeLeafType.param,
})
expect(child.children.size).toBe(0)
})
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.filePaths.keys()]).toEqual([
'default',
'a',
'b',
])
expect([
...tree.children
.get('nested')!
.children.get('foo')!
.value.filePaths.keys(),
]).toEqual(['a', 'b'])
expect([
...tree.children
.get('nested')!
.children.get('[id]')!
.value.filePaths.keys(),
]).toEqual(['a', 'b'])
expect([
...tree.children.get('not.nested.path')!.value.filePaths.keys(),
]).toEqual(['a', 'b'])
expect([
...tree.children
.get('deep')!
.children.get('not.nested.path')!
.value.filePaths.keys(),
]).toEqual(['a', 'b'])
})
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('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: '',
path: '/',
})
expect(index).toBeDefined()
const a = tree.children.get('a')!
expect(a).toBeDefined()
expect(a.value.filePaths.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: '',
path: '/a/',
})
tree.insert('a.vue')
expect(a.value.filePaths.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: TreeLeafType.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('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)
})
})