@instructure/ui-tree-browser 10.16.1 → 10.16.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/es/TreeBrowser/TreeButton/__new-tests__/TreeButton.test.js +165 -0
- package/es/TreeBrowser/TreeCollection/__new-tests__/TreeCollection.test.js +454 -0
- package/es/TreeBrowser/{TreeBrowserLocator.js → TreeNode/__new-tests__/TreeNode.test.js} +31 -14
- package/es/TreeBrowser/__new-tests__/TreeBrowser.test.js +525 -0
- package/lib/TreeBrowser/TreeButton/__new-tests__/TreeButton.test.js +166 -0
- package/lib/TreeBrowser/TreeCollection/__new-tests__/TreeCollection.test.js +457 -0
- package/lib/TreeBrowser/TreeNode/__new-tests__/TreeNode.test.js +56 -0
- package/lib/TreeBrowser/__new-tests__/TreeBrowser.test.js +527 -0
- package/package.json +17 -14
- package/src/TreeBrowser/TreeButton/__new-tests__/TreeButton.test.tsx +162 -0
- package/src/TreeBrowser/TreeCollection/__new-tests__/TreeCollection.test.tsx +423 -0
- package/src/TreeBrowser/{TreeBrowserLocator.ts → TreeNode/__new-tests__/TreeNode.test.tsx} +30 -13
- package/src/TreeBrowser/__new-tests__/TreeBrowser.test.tsx +575 -0
- package/tsconfig.build.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/TreeBrowser/TreeButton/__new-tests__/TreeButton.test.d.ts +2 -0
- package/types/TreeBrowser/TreeButton/__new-tests__/TreeButton.test.d.ts.map +1 -0
- package/types/TreeBrowser/TreeCollection/__new-tests__/TreeCollection.test.d.ts +2 -0
- package/types/TreeBrowser/TreeCollection/__new-tests__/TreeCollection.test.d.ts.map +1 -0
- package/types/TreeBrowser/TreeNode/__new-tests__/TreeNode.test.d.ts +2 -0
- package/types/TreeBrowser/TreeNode/__new-tests__/TreeNode.test.d.ts.map +1 -0
- package/types/TreeBrowser/__new-tests__/TreeBrowser.test.d.ts +2 -0
- package/types/TreeBrowser/__new-tests__/TreeBrowser.test.d.ts.map +1 -0
- package/es/TreeBrowser/locator.js +0 -26
- package/lib/TreeBrowser/TreeBrowserLocator.js +0 -44
- package/lib/TreeBrowser/locator.js +0 -37
- package/src/TreeBrowser/locator.ts +0 -27
- package/types/TreeBrowser/TreeBrowserLocator.d.ts +0 -1065
- package/types/TreeBrowser/TreeBrowserLocator.d.ts.map +0 -1
- package/types/TreeBrowser/locator.d.ts +0 -4
- package/types/TreeBrowser/locator.d.ts.map +0 -1
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { render, screen } from '@testing-library/react'
|
|
26
|
+
import { vi } from 'vitest'
|
|
27
|
+
|
|
28
|
+
import '@testing-library/jest-dom'
|
|
29
|
+
import { TreeButton } from '../index'
|
|
30
|
+
|
|
31
|
+
describe('<TreeButton />', () => {
|
|
32
|
+
it('should render', async () => {
|
|
33
|
+
const { container } = render(<TreeButton id="1" />)
|
|
34
|
+
const treeButton = container.querySelector('button[class$="-treeButton"]')
|
|
35
|
+
|
|
36
|
+
expect(treeButton).toBeInTheDocument()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
describe('containerRef', () => {
|
|
40
|
+
it('should call with parent element', async () => {
|
|
41
|
+
const containerRef = vi.fn()
|
|
42
|
+
|
|
43
|
+
render(
|
|
44
|
+
<div data-testid="1">
|
|
45
|
+
<TreeButton id="2" containerRef={containerRef} />
|
|
46
|
+
</div>
|
|
47
|
+
)
|
|
48
|
+
const div = screen.getByTestId('1')
|
|
49
|
+
|
|
50
|
+
expect(containerRef).toHaveBeenCalledWith(div)
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
describe('descriptor', () => {
|
|
55
|
+
it('should not render a descriptor element if no descriptor passed', async () => {
|
|
56
|
+
const { container } = render(<TreeButton id="1" />)
|
|
57
|
+
|
|
58
|
+
const descriptor = container.querySelector(
|
|
59
|
+
'[class$="-treeButton__textDescriptor"]'
|
|
60
|
+
)
|
|
61
|
+
expect(descriptor).not.toBeInTheDocument()
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('should render a descriptor element if descriptor passed', async () => {
|
|
65
|
+
const { container } = render(
|
|
66
|
+
<TreeButton id="1" descriptor="Some Descriptor" />
|
|
67
|
+
)
|
|
68
|
+
const descriptor = container.querySelector(
|
|
69
|
+
'[class$="-treeButton__textDescriptor"]'
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
expect(descriptor).toBeInTheDocument()
|
|
73
|
+
expect(descriptor).toHaveTextContent('Some Descriptor')
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
describe('icons', () => {
|
|
78
|
+
const Icon = (
|
|
79
|
+
<svg height="100" width="100" data-testid="custom-icon">
|
|
80
|
+
<title data-testid="custom-icon-title">Test icon</title>
|
|
81
|
+
<circle cx="50" cy="50" r="40" />
|
|
82
|
+
</svg>
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
it('should render a collection icon', async () => {
|
|
86
|
+
render(
|
|
87
|
+
<TreeButton id="1" type="collection" collectionIcon={() => Icon} />
|
|
88
|
+
)
|
|
89
|
+
const icon = screen.getByTestId('custom-icon')
|
|
90
|
+
const iconTitle = screen.getByTestId('custom-icon-title')
|
|
91
|
+
|
|
92
|
+
expect(icon).toBeInTheDocument()
|
|
93
|
+
expect(icon).toHaveTextContent('Test icon')
|
|
94
|
+
expect(iconTitle).toBeInTheDocument()
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('should render an item icon', async () => {
|
|
98
|
+
render(<TreeButton id="1" type="item" itemIcon={() => Icon} />)
|
|
99
|
+
const icon = screen.getByTestId('custom-icon')
|
|
100
|
+
|
|
101
|
+
expect(icon).toBeInTheDocument()
|
|
102
|
+
expect(icon).toHaveTextContent('Test icon')
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('should render no icon if no icon prop passed', async () => {
|
|
106
|
+
const { container } = render(<TreeButton id="1" />)
|
|
107
|
+
const icon = container.querySelector('svg')
|
|
108
|
+
|
|
109
|
+
expect(icon).not.toBeInTheDocument()
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('should render a thumbnail instead of an icon if a thumbnail URL is passed', async () => {
|
|
113
|
+
const { container } = render(
|
|
114
|
+
<TreeButton
|
|
115
|
+
id="1"
|
|
116
|
+
type="item"
|
|
117
|
+
thumbnail="data:image/gif;base64,R0lGODlhFAAUAJEAAP/9/fYQEPytrflWViH5BAAAAAAALAAAAAAUABQAQAJKhI+pGe09lnhBnEETfodatVHNh1BR+ZzH9LAOCYrVYpiAfWWJOxrC/5MASbyZT4d6AUIBlUYGoR1FsAXUuTN5YhxAEYbrpKRkQwEAOw=="
|
|
118
|
+
/>
|
|
119
|
+
)
|
|
120
|
+
const img = container.querySelector('img')
|
|
121
|
+
|
|
122
|
+
expect(img).toBeInTheDocument()
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('should not render a thumbnail if no thumbnail URL is passed', async () => {
|
|
126
|
+
const { container } = render(<TreeButton id="1" type="item" />)
|
|
127
|
+
const thumbnail = container.querySelector('img')
|
|
128
|
+
|
|
129
|
+
expect(thumbnail).not.toBeInTheDocument()
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('should render a thumbnail if a thumbnail and an icon are passed', async () => {
|
|
133
|
+
const { container } = render(
|
|
134
|
+
<TreeButton
|
|
135
|
+
id="1"
|
|
136
|
+
type="item"
|
|
137
|
+
itemIcon={() => Icon}
|
|
138
|
+
thumbnail="data:image/gif;base64,R0lGODlhFAAUAJEAAP/9/fYQEPytrflWViH5BAAAAAAALAAAAAAUABQAQAJKhI+pGe09lnhBnEETfodatVHNh1BR+ZzH9LAOCYrVYpiAfWWJOxrC/5MASbyZT4d6AUIBlUYGoR1FsAXUuTN5YhxAEYbrpKRkQwEAOw=="
|
|
139
|
+
/>
|
|
140
|
+
)
|
|
141
|
+
const thumbnail = container.querySelector('img')
|
|
142
|
+
const icon = container.querySelector('svg')
|
|
143
|
+
|
|
144
|
+
expect(thumbnail).toBeInTheDocument()
|
|
145
|
+
expect(icon).not.toBeInTheDocument()
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
describe('renderContent', () => {
|
|
150
|
+
it('should render the content passed to renderContent', async () => {
|
|
151
|
+
const { container } = render(
|
|
152
|
+
<TreeButton
|
|
153
|
+
id="1"
|
|
154
|
+
renderContent={() => <div className="test1">abcd</div>}
|
|
155
|
+
/>
|
|
156
|
+
)
|
|
157
|
+
const customElement = container.querySelector('div[class="test1"]')
|
|
158
|
+
|
|
159
|
+
expect(customElement).toBeInTheDocument()
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
})
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { render, screen, waitFor } from '@testing-library/react'
|
|
26
|
+
import userEvent from '@testing-library/user-event'
|
|
27
|
+
import { vi } from 'vitest'
|
|
28
|
+
import type { MockInstance } from 'vitest'
|
|
29
|
+
|
|
30
|
+
import '@testing-library/jest-dom'
|
|
31
|
+
import { TreeCollection } from '../index'
|
|
32
|
+
|
|
33
|
+
const IconFolder = (
|
|
34
|
+
<svg height="100" width="100">
|
|
35
|
+
<title>Folder icon</title>
|
|
36
|
+
<circle cx="50" cy="50" r="40" />
|
|
37
|
+
</svg>
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
const IconDocument = (
|
|
41
|
+
<svg height="100" width="100">
|
|
42
|
+
<title>Document icon</title>
|
|
43
|
+
<circle cx="50" cy="50" r="40" />
|
|
44
|
+
</svg>
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
const IconUser = (
|
|
48
|
+
<svg height="100" width="100">
|
|
49
|
+
<title>User icon</title>
|
|
50
|
+
<circle cx="50" cy="50" r="40" />
|
|
51
|
+
</svg>
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
describe('<TreeCollection />', () => {
|
|
55
|
+
let consoleWarningMock: ReturnType<typeof vi.spyOn>
|
|
56
|
+
let consoleErrorMock: ReturnType<typeof vi.spyOn>
|
|
57
|
+
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
// Mocking console to prevent test output pollution and expect for messages
|
|
60
|
+
consoleWarningMock = vi
|
|
61
|
+
.spyOn(console, 'warn')
|
|
62
|
+
.mockImplementation(() => {}) as MockInstance
|
|
63
|
+
|
|
64
|
+
consoleErrorMock = vi
|
|
65
|
+
.spyOn(console, 'error')
|
|
66
|
+
.mockImplementation(() => {}) as MockInstance
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
afterEach(() => {
|
|
70
|
+
consoleWarningMock.mockRestore()
|
|
71
|
+
consoleErrorMock.mockRestore()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('should render', async () => {
|
|
75
|
+
const { container } = render(
|
|
76
|
+
<TreeCollection
|
|
77
|
+
level={1}
|
|
78
|
+
id={1}
|
|
79
|
+
name="Coll 1"
|
|
80
|
+
collections={[
|
|
81
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
82
|
+
]}
|
|
83
|
+
items={[{ id: 1, name: 'Item 1' }]}
|
|
84
|
+
/>
|
|
85
|
+
)
|
|
86
|
+
const collection = container.querySelector('[class$="-treeCollection"]')
|
|
87
|
+
|
|
88
|
+
expect(collection).toBeInTheDocument()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
describe('collections', () => {
|
|
92
|
+
it('should render icons on children', async () => {
|
|
93
|
+
render(
|
|
94
|
+
<TreeCollection
|
|
95
|
+
level={1}
|
|
96
|
+
id={1}
|
|
97
|
+
name="Coll 1"
|
|
98
|
+
collections={[
|
|
99
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
100
|
+
]}
|
|
101
|
+
items={[{ id: 1, name: 'Item 1' }]}
|
|
102
|
+
collectionIcon={() => IconFolder}
|
|
103
|
+
collectionIconExpanded={() => IconFolder}
|
|
104
|
+
itemIcon={() => IconDocument}
|
|
105
|
+
expanded={true}
|
|
106
|
+
/>
|
|
107
|
+
)
|
|
108
|
+
const folderIcons = await screen.findAllByTitle('Folder icon')
|
|
109
|
+
expect(folderIcons.length).toBe(2)
|
|
110
|
+
|
|
111
|
+
const documentIcons = await screen.findAllByTitle('Document icon')
|
|
112
|
+
expect(documentIcons.length).toBe(1)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('should support the containerRef prop', async () => {
|
|
116
|
+
const containerRef = vi.fn()
|
|
117
|
+
|
|
118
|
+
const { container } = render(
|
|
119
|
+
<TreeCollection
|
|
120
|
+
level={1}
|
|
121
|
+
id={1}
|
|
122
|
+
containerRef={containerRef}
|
|
123
|
+
name="Coll 1"
|
|
124
|
+
collections={[
|
|
125
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
126
|
+
]}
|
|
127
|
+
items={[{ id: 1, name: 'Item 1' }]}
|
|
128
|
+
/>
|
|
129
|
+
)
|
|
130
|
+
const collection = container.querySelector('[class$="-treeCollection"]')
|
|
131
|
+
|
|
132
|
+
expect(containerRef).toHaveBeenCalledWith(collection)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('should pass an aria-expanded attribute to its list item', async () => {
|
|
136
|
+
render(
|
|
137
|
+
<TreeCollection
|
|
138
|
+
level={1}
|
|
139
|
+
id={1}
|
|
140
|
+
name="Coll 1"
|
|
141
|
+
collections={[
|
|
142
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
143
|
+
]}
|
|
144
|
+
items={[{ id: 1, name: 'Item 1' }]}
|
|
145
|
+
/>
|
|
146
|
+
)
|
|
147
|
+
const item = screen.getByRole('treeitem')
|
|
148
|
+
|
|
149
|
+
expect(item).toHaveAttribute('aria-expanded')
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
it('should pass an aria-selected attribute to its list item', async () => {
|
|
153
|
+
render(
|
|
154
|
+
<TreeCollection
|
|
155
|
+
level={1}
|
|
156
|
+
id={1}
|
|
157
|
+
name="Coll 1"
|
|
158
|
+
collections={[
|
|
159
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
160
|
+
]}
|
|
161
|
+
items={[{ id: 1, name: 'Item 1' }]}
|
|
162
|
+
selection="collection_1"
|
|
163
|
+
/>
|
|
164
|
+
)
|
|
165
|
+
const item = screen.getByRole('treeitem')
|
|
166
|
+
|
|
167
|
+
expect(item).toHaveAttribute('aria-selected')
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('should correctly evaluate `getCollectionProps` for each item', async () => {
|
|
171
|
+
const { container } = render(
|
|
172
|
+
<TreeCollection
|
|
173
|
+
level={1}
|
|
174
|
+
id={1}
|
|
175
|
+
name="Coll 1"
|
|
176
|
+
collections={[
|
|
177
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
178
|
+
]}
|
|
179
|
+
items={[
|
|
180
|
+
{ id: 1, name: 'Item 1', icon: () => IconFolder },
|
|
181
|
+
{ id: 2, name: 'Item 2', icon: () => IconUser },
|
|
182
|
+
{ id: 2, name: 'Item 3' }
|
|
183
|
+
]}
|
|
184
|
+
collectionIcon={IconFolder}
|
|
185
|
+
getCollectionProps={({ ...props }) => {
|
|
186
|
+
let icon = props.collectionIcon
|
|
187
|
+
if (props.name === 'Coll 2') {
|
|
188
|
+
icon = IconUser
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
...props,
|
|
192
|
+
collectionIcon: icon
|
|
193
|
+
}
|
|
194
|
+
}}
|
|
195
|
+
expanded={true}
|
|
196
|
+
/>
|
|
197
|
+
)
|
|
198
|
+
const svgIconUser = screen.getByTitle('User icon')
|
|
199
|
+
const item1 = container.querySelectorAll('button')[1]
|
|
200
|
+
|
|
201
|
+
expect(svgIconUser).toBeInTheDocument()
|
|
202
|
+
expect(item1).toContainElement(svgIconUser)
|
|
203
|
+
expect(item1).toHaveTextContent('Coll 2')
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
describe('onCollectionClick', () => {
|
|
207
|
+
it('should return the correct collection params on click', async () => {
|
|
208
|
+
const onCollectionClick = vi.fn()
|
|
209
|
+
|
|
210
|
+
render(
|
|
211
|
+
<TreeCollection
|
|
212
|
+
level={1}
|
|
213
|
+
id={1}
|
|
214
|
+
name="Coll 1"
|
|
215
|
+
collections={[
|
|
216
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
217
|
+
]}
|
|
218
|
+
items={[{ id: 1, name: 'Item 1' }]}
|
|
219
|
+
onCollectionClick={onCollectionClick}
|
|
220
|
+
/>
|
|
221
|
+
)
|
|
222
|
+
const item = screen.getByRole('treeitem')
|
|
223
|
+
|
|
224
|
+
await userEvent.click(item)
|
|
225
|
+
|
|
226
|
+
await waitFor(() => {
|
|
227
|
+
const args = onCollectionClick.mock.calls[0][1]
|
|
228
|
+
|
|
229
|
+
expect(onCollectionClick).toHaveBeenCalledTimes(1)
|
|
230
|
+
expect(args).toStrictEqual({
|
|
231
|
+
id: 1,
|
|
232
|
+
expanded: true,
|
|
233
|
+
type: 'collection'
|
|
234
|
+
})
|
|
235
|
+
})
|
|
236
|
+
})
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
describe('items', () => {
|
|
241
|
+
it('should not pass an aria-expanded attribute to its button', async () => {
|
|
242
|
+
render(
|
|
243
|
+
<TreeCollection
|
|
244
|
+
level={1}
|
|
245
|
+
id={1}
|
|
246
|
+
name="Coll 1"
|
|
247
|
+
collections={[
|
|
248
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
249
|
+
]}
|
|
250
|
+
items={[{ id: 1, name: 'Item 1' }]}
|
|
251
|
+
expanded={true}
|
|
252
|
+
/>
|
|
253
|
+
)
|
|
254
|
+
const item = screen.getAllByLabelText('Coll 1')[0]
|
|
255
|
+
const button = item.firstChild as HTMLElement
|
|
256
|
+
|
|
257
|
+
expect(item).toHaveAttribute('aria-expanded', 'true')
|
|
258
|
+
expect(button.tagName).toBe('BUTTON')
|
|
259
|
+
expect(button).not.toHaveAttribute('aria-expanded')
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
it('should call custom functions passed by onItemClick', async () => {
|
|
263
|
+
const onItemClick = vi.fn()
|
|
264
|
+
|
|
265
|
+
render(
|
|
266
|
+
<TreeCollection
|
|
267
|
+
level={1}
|
|
268
|
+
id={1}
|
|
269
|
+
name="Coll 1"
|
|
270
|
+
collections={[
|
|
271
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
272
|
+
]}
|
|
273
|
+
items={[{ id: 1, name: 'Item 1' }]}
|
|
274
|
+
onItemClick={onItemClick}
|
|
275
|
+
expanded={true}
|
|
276
|
+
/>
|
|
277
|
+
)
|
|
278
|
+
const item = screen.getByLabelText('Item 1')
|
|
279
|
+
|
|
280
|
+
await userEvent.click(item)
|
|
281
|
+
|
|
282
|
+
await waitFor(() => {
|
|
283
|
+
const args = onItemClick.mock.calls[0][1]
|
|
284
|
+
|
|
285
|
+
expect(onItemClick).toHaveBeenCalledTimes(1)
|
|
286
|
+
expect(args).toStrictEqual({
|
|
287
|
+
id: 1,
|
|
288
|
+
type: 'item'
|
|
289
|
+
})
|
|
290
|
+
})
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
it('should correctly evaluate `getItemProps` for each item', async () => {
|
|
294
|
+
render(
|
|
295
|
+
<TreeCollection
|
|
296
|
+
level={1}
|
|
297
|
+
id={1}
|
|
298
|
+
name="Coll 1"
|
|
299
|
+
collections={[
|
|
300
|
+
{ id: 2, name: 'Coll 2', descriptor: 'Another Descriptor' }
|
|
301
|
+
]}
|
|
302
|
+
items={[
|
|
303
|
+
{ id: 1, name: 'Item 1', icon: () => IconFolder },
|
|
304
|
+
{ id: 2, name: 'Item 2', icon: () => IconUser },
|
|
305
|
+
{ id: 2, name: 'Item 3' }
|
|
306
|
+
]}
|
|
307
|
+
getItemProps={({ name, ...props }) => {
|
|
308
|
+
let itemIcon = IconDocument
|
|
309
|
+
if (name === 'Item 1') {
|
|
310
|
+
itemIcon = IconFolder
|
|
311
|
+
}
|
|
312
|
+
if (name === 'Item 2') {
|
|
313
|
+
itemIcon = IconUser
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
...props,
|
|
317
|
+
itemIcon,
|
|
318
|
+
name
|
|
319
|
+
}
|
|
320
|
+
}}
|
|
321
|
+
expanded={true}
|
|
322
|
+
/>
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
expect(screen.getByTitle('Folder icon')).toBeInTheDocument()
|
|
326
|
+
expect(screen.getByTitle('User icon')).toBeInTheDocument()
|
|
327
|
+
expect(screen.getByTitle('Document icon')).toBeInTheDocument()
|
|
328
|
+
})
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
describe('sorting', () => {
|
|
332
|
+
it('should show the items and subcollections in alphabetical order', async () => {
|
|
333
|
+
const { container } = render(
|
|
334
|
+
<TreeCollection
|
|
335
|
+
level={1}
|
|
336
|
+
id={1}
|
|
337
|
+
name="Root"
|
|
338
|
+
collections={[
|
|
339
|
+
{ id: 2, name: 'A', descriptor: 'Collection A' },
|
|
340
|
+
{ id: 3, name: 'B', descriptor: 'Collection B' },
|
|
341
|
+
{ id: 4, name: 'C', descriptor: 'Collection A' }
|
|
342
|
+
]}
|
|
343
|
+
items={[
|
|
344
|
+
{ id: 1, name: 'A1' },
|
|
345
|
+
{ id: 2, name: 'B1' },
|
|
346
|
+
{ id: 2, name: 'C1' }
|
|
347
|
+
]}
|
|
348
|
+
getItemProps={({ name, ...props }) => {
|
|
349
|
+
const itemIcon = IconDocument
|
|
350
|
+
return {
|
|
351
|
+
...props,
|
|
352
|
+
itemIcon,
|
|
353
|
+
name
|
|
354
|
+
}
|
|
355
|
+
}}
|
|
356
|
+
expanded={true}
|
|
357
|
+
compareFunc={(a, b) => {
|
|
358
|
+
return a.name.localeCompare(b.name)
|
|
359
|
+
}}
|
|
360
|
+
/>
|
|
361
|
+
)
|
|
362
|
+
const childNames = container.querySelectorAll(
|
|
363
|
+
'span[class$="treeButton__textName"]'
|
|
364
|
+
)
|
|
365
|
+
const childNameArr = Array.from(childNames)
|
|
366
|
+
.map((element) => element.textContent)
|
|
367
|
+
.slice(1)
|
|
368
|
+
|
|
369
|
+
expect(childNameArr).toEqual(['A', 'A1', 'B', 'B1', 'C', 'C1'])
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
it('should show the items before the collections', async () => {
|
|
373
|
+
const { container } = render(
|
|
374
|
+
<TreeCollection
|
|
375
|
+
level={1}
|
|
376
|
+
id={1}
|
|
377
|
+
name="Root"
|
|
378
|
+
collections={[
|
|
379
|
+
{ id: 2, name: 'Coll A', descriptor: 'Collection A' },
|
|
380
|
+
{ id: 3, name: 'Coll B', descriptor: 'Collection B' },
|
|
381
|
+
{ id: 4, name: 'Coll C', descriptor: 'Collection A' }
|
|
382
|
+
]}
|
|
383
|
+
items={[
|
|
384
|
+
{ id: 1, name: 'Item A' },
|
|
385
|
+
{ id: 2, name: 'Item B' },
|
|
386
|
+
{ id: 2, name: 'Item C' }
|
|
387
|
+
]}
|
|
388
|
+
getItemProps={({ name, ...props }) => {
|
|
389
|
+
const itemIcon = IconDocument
|
|
390
|
+
return {
|
|
391
|
+
...props,
|
|
392
|
+
itemIcon,
|
|
393
|
+
name
|
|
394
|
+
}
|
|
395
|
+
}}
|
|
396
|
+
expanded={true}
|
|
397
|
+
compareFunc={(a, b) => {
|
|
398
|
+
if (a.type === 'item' && b.type === 'collection') return -1
|
|
399
|
+
|
|
400
|
+
if (a.type === 'collection' && b.type === 'item') return 1
|
|
401
|
+
|
|
402
|
+
return 0
|
|
403
|
+
}}
|
|
404
|
+
/>
|
|
405
|
+
)
|
|
406
|
+
const childNames = container.querySelectorAll(
|
|
407
|
+
'span[class$="treeButton__textName"]'
|
|
408
|
+
)
|
|
409
|
+
const childNameArr = Array.from(childNames)
|
|
410
|
+
.map((element) => element.textContent)
|
|
411
|
+
.slice(1)
|
|
412
|
+
|
|
413
|
+
expect(childNameArr).toEqual([
|
|
414
|
+
'Item A',
|
|
415
|
+
'Item B',
|
|
416
|
+
'Item C',
|
|
417
|
+
'Coll A',
|
|
418
|
+
'Coll B',
|
|
419
|
+
'Coll C'
|
|
420
|
+
])
|
|
421
|
+
})
|
|
422
|
+
})
|
|
423
|
+
})
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License (MIT)
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2021 - present Instructure, Inc.
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -21,19 +21,36 @@
|
|
|
21
21
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
|
-
import { locator } from '@instructure/ui-test-locator'
|
|
25
24
|
|
|
26
|
-
import {
|
|
25
|
+
import { render, screen } from '@testing-library/react'
|
|
26
|
+
import { vi } from 'vitest'
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
import '@testing-library/jest-dom'
|
|
29
|
+
import { TreeNode } from '../index'
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
describe('<TreeNode />', () => {
|
|
32
|
+
it('should render children', async () => {
|
|
33
|
+
render(
|
|
34
|
+
<TreeNode>
|
|
35
|
+
<button>Hello World</button>
|
|
36
|
+
</TreeNode>
|
|
37
|
+
)
|
|
38
|
+
const item = screen.getByText('Hello World')
|
|
39
|
+
|
|
40
|
+
expect(item).toBeInTheDocument()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('supports containerRef prop', async () => {
|
|
44
|
+
const containerRef = vi.fn()
|
|
45
|
+
render(
|
|
46
|
+
<div data-testid="1">
|
|
47
|
+
<TreeNode containerRef={containerRef}>
|
|
48
|
+
<button>Hello World</button>
|
|
49
|
+
</TreeNode>
|
|
50
|
+
</div>
|
|
51
|
+
)
|
|
52
|
+
const div = screen.getByTestId('1')
|
|
53
|
+
|
|
54
|
+
expect(containerRef).toHaveBeenCalledWith(div)
|
|
55
|
+
})
|
|
39
56
|
})
|