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