|
1 | 1 | import * as React from 'react' |
2 | | -import {render} from '@testing-library/react' |
| 2 | +import {render, buildQueries, queryAllByRole} from '@testing-library/react' |
| 3 | +import userEvent from '@testing-library/user-event' |
3 | 4 | import Downshift from '../' |
4 | 5 | import DropdownSelect from '../../test/useSelect.test' |
5 | | -import DropdownCombobox from '../../test/useCombobox.test' |
| 6 | +import DropdownCombobox, {colors} from '../../test/useCombobox.test' |
6 | 7 | import DropdownMultipleSelect from '../../test/useMultipleSelect.test' |
7 | 8 | import {ReactShadowRoot} from '../../test/react-shadow' |
8 | 9 |
|
| 10 | + |
| 11 | +function _queryAllByRoleDeep(container, ...rest) { |
| 12 | + // eslint-disable-next-line testing-library/prefer-screen-queries |
| 13 | + const result = queryAllByRole(container, ...rest) // replace here with different queryAll* variants. |
| 14 | + for (const element of container.querySelectorAll('*')) { |
| 15 | + if (element.shadowRoot) { |
| 16 | + result.push(..._queryAllByRoleDeep(element.shadowRoot, ...rest)) |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + return result |
| 21 | + } |
| 22 | + |
| 23 | + // eslint-disable-next-line no-unused-vars |
| 24 | + const [_queryByRoleDeep, _getAllByRoleDeep, _getByRoleDeep, _findAllByRoleDeep, _findByRoleDeep] = buildQueries( |
| 25 | + _queryAllByRoleDeep, |
| 26 | + (_, role) => `Found multiple elements with the role ${role}`, |
| 27 | + (_, role) => `Unable to find an element with the role ${role}` |
| 28 | + ) |
| 29 | + |
| 30 | + const getAllByRoleDeep = _getAllByRoleDeep.bind(null, document.body) |
| 31 | + const getByRoleDeep = _getByRoleDeep.bind(null, document.body) |
| 32 | + |
| 33 | + |
9 | 34 | const Wrapper = ({children}) => { |
10 | 35 | return <ReactShadowRoot>{children}</ReactShadowRoot> |
11 | 36 | } |
@@ -33,3 +58,113 @@ test('DropdownMultipleSelect renders with a shadow root', () => { |
33 | 58 |
|
34 | 59 | expect(container.shadowRoot).toBeDefined() |
35 | 60 | }) |
| 61 | + |
| 62 | +test('DropdownSelect works correctly in shadow DOM', async () => { |
| 63 | + const user = userEvent.setup() |
| 64 | + const {container} = render(<DropdownSelect />, {wrapper: Wrapper}) |
| 65 | + |
| 66 | + // Verify shadow root exists |
| 67 | + expect(container.shadowRoot).toBeDefined() |
| 68 | + |
| 69 | + // Get elements within the shadow root |
| 70 | + const toggleButton = getByRoleDeep('combobox') |
| 71 | + const menu = getByRoleDeep('listbox') |
| 72 | + expect(toggleButton).toBeInTheDocument() |
| 73 | + expect(menu).toBeInTheDocument() |
| 74 | + |
| 75 | + // Initially menu should be closed |
| 76 | + expect(toggleButton).toHaveAttribute('aria-expanded', 'false') |
| 77 | + |
| 78 | + // Open the dropdown |
| 79 | + await user.click(toggleButton) |
| 80 | + |
| 81 | + // Menu should now be open |
| 82 | + expect(toggleButton).toHaveAttribute('aria-expanded', 'true') |
| 83 | + |
| 84 | + // Select an item |
| 85 | + const blackOption = getByRoleDeep('option', {name: 'Black'}) |
| 86 | + await user.click(blackOption) |
| 87 | + |
| 88 | + // Menu should close and selected item should appear in button |
| 89 | + expect(toggleButton).toHaveAttribute('aria-expanded', 'false') |
| 90 | + expect(toggleButton).toHaveTextContent('Black') |
| 91 | + |
| 92 | + // Open it again |
| 93 | + await user.click(toggleButton) |
| 94 | + expect(toggleButton).toHaveAttribute('aria-expanded', 'true') |
| 95 | + |
| 96 | + // Click outside (this tests our targetWithinDownshift with composedPath) |
| 97 | + await user.click(document.body) |
| 98 | + |
| 99 | + // Menu should close |
| 100 | + expect(toggleButton).toHaveAttribute('aria-expanded', 'false') |
| 101 | +}) |
| 102 | + |
| 103 | +test('DropdownCombobox works correctly in shadow DOM', async () => { |
| 104 | + const user = userEvent.setup() |
| 105 | + const {container} = render(<DropdownCombobox />, {wrapper: Wrapper}) |
| 106 | + |
| 107 | + // Verify shadow root exists |
| 108 | + expect(container.shadowRoot).toBeDefined() |
| 109 | + |
| 110 | + // Get elements within the shadow root |
| 111 | + const input = getByRoleDeep('combobox') |
| 112 | + const toggleButton = getByRoleDeep('button', {name: 'toggle menu'}) |
| 113 | + const clearButton = getByRoleDeep('button', {name: 'clear'}) |
| 114 | + const menu = getByRoleDeep('listbox') |
| 115 | + |
| 116 | + expect(input).toBeInTheDocument() |
| 117 | + expect(toggleButton).toBeInTheDocument() |
| 118 | + expect(clearButton).toBeInTheDocument() |
| 119 | + expect(menu).toBeInTheDocument() |
| 120 | + |
| 121 | + // Initially menu should be closed |
| 122 | + expect(input).toHaveAttribute('aria-expanded', 'false') |
| 123 | + expect(toggleButton).toHaveAttribute('aria-expanded', 'false') |
| 124 | + |
| 125 | + // Open the dropdown |
| 126 | + await user.click(toggleButton) |
| 127 | + |
| 128 | + // Menu should now be open |
| 129 | + expect(input).toHaveAttribute('aria-expanded', 'true') |
| 130 | + |
| 131 | + // All colors should initially be visible |
| 132 | + const items = getAllByRoleDeep('option') |
| 133 | + expect(items).toHaveLength(colors.length) |
| 134 | + |
| 135 | +// // Type in the input to filter items |
| 136 | +// await user.click(input) |
| 137 | +// input.focus() |
| 138 | +// expect(input).toHaveFocus() |
| 139 | +// await user.type(input, 'bl') |
| 140 | + |
| 141 | +// // Only Black and Blue should be visible |
| 142 | +// items = getAllByRoleDeep('option') |
| 143 | +// await waitFor(() => expect(items).toHaveLength(2)) |
| 144 | +// expect(items[0]).toHaveTextContent('Black') |
| 145 | +// expect(items[1]).toHaveTextContent('Blue') |
| 146 | + |
| 147 | + // Select an item |
| 148 | + await user.click(items[0]) |
| 149 | + |
| 150 | + // Menu should close and input should have selected value |
| 151 | + expect(input).toHaveAttribute('aria-expanded', 'false') |
| 152 | + expect(toggleButton).toHaveAttribute('aria-expanded', 'false') |
| 153 | + expect(input).toHaveValue('Black') |
| 154 | + |
| 155 | + // Clear the selection |
| 156 | + await user.click(clearButton) |
| 157 | + |
| 158 | + // Input should be empty |
| 159 | + expect(input).toHaveValue('') |
| 160 | + |
| 161 | + // Open it again |
| 162 | + await user.click(toggleButton) |
| 163 | + expect(input).toHaveAttribute('aria-expanded', 'true') |
| 164 | + |
| 165 | + // Click outside to close |
| 166 | + await user.click(document.body) |
| 167 | + |
| 168 | + // Menu should close |
| 169 | + expect(input).toHaveAttribute('aria-expanded', 'false') |
| 170 | +}) |
0 commit comments