-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathgetCustomCollectionViewByRoute.spec.ts
More file actions
153 lines (128 loc) · 4.7 KB
/
getCustomCollectionViewByRoute.spec.ts
File metadata and controls
153 lines (128 loc) · 4.7 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
import type { AdminViewConfig, SanitizedCollectionConfig } from 'payload'
import { describe, expect, it } from 'vitest'
import { getCustomCollectionViewByRoute } from './getCustomCollectionViewByRoute.js'
type Views = SanitizedCollectionConfig['admin']['components']['views']
const gridView: Views = {
grid: {
Component: '/components/views/GridView/index.js#GridView',
exact: true,
path: '/grid',
},
}
describe('getCustomCollectionViewByRoute', () => {
describe('route matching with default /admin prefix', () => {
it('should match a custom view at the correct path', () => {
const result = getCustomCollectionViewByRoute({
adminRoute: '/admin',
baseRoute: '/collections/my-collection',
currentRoute: '/admin/collections/my-collection/grid',
views: gridView,
})
expect(result.viewKey).toBe('grid')
expect(result.view.payloadComponent).toBeDefined()
})
it('should not match when the path segment does not correspond to any custom view', () => {
const result = getCustomCollectionViewByRoute({
adminRoute: '/admin',
baseRoute: '/collections/my-collection',
currentRoute: '/admin/collections/my-collection/abc123',
views: gridView,
})
expect(result.viewKey).toBeNull()
expect(result.view.payloadComponent).toBeUndefined()
})
})
describe('route matching with custom adminRoute prefix', () => {
it('should match when adminRoute is a non-default prefix', () => {
const result = getCustomCollectionViewByRoute({
adminRoute: '/cms',
baseRoute: '/collections/my-collection',
currentRoute: '/cms/collections/my-collection/grid',
views: gridView,
})
expect(result.viewKey).toBe('grid')
expect(result.view.payloadComponent).toBeDefined()
})
it('should match when adminRoute is /', () => {
const result = getCustomCollectionViewByRoute({
adminRoute: '/',
baseRoute: '/collections/my-collection',
currentRoute: '/collections/my-collection/grid',
views: gridView,
})
expect(result.viewKey).toBe('grid')
expect(result.view.payloadComponent).toBeDefined()
})
})
describe('edge cases', () => {
it('should return no match when views is undefined', () => {
const result = getCustomCollectionViewByRoute({
adminRoute: '/admin',
baseRoute: '/collections/my-collection',
currentRoute: '/admin/collections/my-collection/grid',
views: undefined,
})
expect(result.viewKey).toBeNull()
expect(result.view.payloadComponent).toBeUndefined()
})
it('should not match built-in "edit" or "list" keys', () => {
const viewsWithBuiltins: Views = {
edit: {
default: { Component: '/components/views/Edit/index.js#EditView' },
},
list: {
Component: '/components/views/List/index.js#ListView',
},
}
const result = getCustomCollectionViewByRoute({
adminRoute: '/admin',
baseRoute: '/collections/my-collection',
currentRoute: '/admin/collections/my-collection/edit',
views: viewsWithBuiltins,
})
expect(result.viewKey).toBeNull()
})
it('should not match a custom view that has no path defined', () => {
const viewsWithNoPath: Views = {
grid: {
Component: '/components/views/GridView/index.js#GridView',
} as unknown as AdminViewConfig,
}
const result = getCustomCollectionViewByRoute({
adminRoute: '/admin',
baseRoute: '/collections/my-collection',
currentRoute: '/admin/collections/my-collection/grid',
views: viewsWithNoPath,
})
expect(result.viewKey).toBeNull()
})
it('should match the correct view when multiple custom views are defined', () => {
const multipleViews: Views = {
grid: {
Component: '/components/views/GridView/index.js#GridView',
exact: true,
path: '/grid',
},
map: {
Component: '/components/views/MapView/index.js#MapView',
exact: true,
path: '/map',
},
}
const gridResult = getCustomCollectionViewByRoute({
adminRoute: '/admin',
baseRoute: '/collections/my-collection',
currentRoute: '/admin/collections/my-collection/grid',
views: multipleViews,
})
expect(gridResult.viewKey).toBe('grid')
const mapResult = getCustomCollectionViewByRoute({
adminRoute: '/admin',
baseRoute: '/collections/my-collection',
currentRoute: '/admin/collections/my-collection/map',
views: multipleViews,
})
expect(mapResult.viewKey).toBe('map')
})
})
})