|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | +import { act, render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { Dropdown } from './Dropdown'; |
| 5 | +import { options } from './Dropdown.stories'; |
| 6 | + |
| 7 | +/** |
| 8 | + * TODO |
| 9 | + * |
| 10 | + * We get lots of warnings about needing to wrap in act(...) because actions |
| 11 | + * update React state but, when wrapped, some of the actions don't update |
| 12 | + * the rendered element. I've wrapped what I could to eliminate as many |
| 13 | + * warnings as I could. |
| 14 | + * |
| 15 | + * I have tried some of the workarounds outlined in the article below |
| 16 | + * but have not had any success eliminating the warnings. |
| 17 | + * |
| 18 | + * https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning |
| 19 | + */ |
| 20 | + |
| 21 | +const onSelect = (): null => null; |
| 22 | + |
| 23 | +const label = '-default dropdown-'; |
| 24 | +const id = 'anID'; |
| 25 | +const placeholder = 'HOLD MY PLACE'; |
| 26 | + |
| 27 | +/** |
| 28 | + * Single select |
| 29 | + */ |
| 30 | +describe('Default Dropdown', () => { |
| 31 | + const defaultProps = { id, label, options, onSelect, placeholder }; |
| 32 | + |
| 33 | + it('Renders default labels correctly', () => { |
| 34 | + render(<Dropdown {...{ id, options, onSelect }} />); |
| 35 | + expect(screen.queryByText(label)).not.toBeInTheDocument(); |
| 36 | + expect(screen.getByText('Dropdown w/ Multi-select')).toBeInTheDocument(); |
| 37 | + expect(screen.getByText('Select...')).toBeInTheDocument(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('Renders provided labels correctly', () => { |
| 41 | + render(<Dropdown {...defaultProps} />); |
| 42 | + expect(screen.getByText(label)).toBeInTheDocument(); |
| 43 | + expect(screen.getByText(placeholder)).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('(Mouse) Selects an option', async () => { |
| 47 | + const optionLabel = 'Option A'; |
| 48 | + const user = userEvent.setup(); |
| 49 | + |
| 50 | + render(<Dropdown {...defaultProps} />); |
| 51 | + await act(async () => { |
| 52 | + await user.click(screen.getByText(label)); |
| 53 | + }); |
| 54 | + |
| 55 | + expect(screen.getByText(optionLabel)).toBeInTheDocument(); |
| 56 | + await act(async () => { |
| 57 | + await user.click(screen.getByText(optionLabel)); |
| 58 | + }); |
| 59 | + |
| 60 | + const selectedOption = screen.getByText(optionLabel); |
| 61 | + expect(selectedOption).toBeInTheDocument(); |
| 62 | + expect(selectedOption.getAttribute('class')).toMatch(/singlevalue/gi); |
| 63 | + }); |
| 64 | + |
| 65 | + it('(Keyboard) Selects an option', async () => { |
| 66 | + const optionLabel = 'Option C'; |
| 67 | + const user = userEvent.setup(); |
| 68 | + |
| 69 | + render(<Dropdown {...defaultProps} />); |
| 70 | + |
| 71 | + expect(screen.queryByText(optionLabel)).not.toBeInTheDocument(); |
| 72 | + await user.click(screen.getByText(label)); |
| 73 | + await user.keyboard('{Tab}{Tab}{Enter}'); |
| 74 | + |
| 75 | + const selectedOption = screen.getByText(optionLabel); |
| 76 | + expect(selectedOption).toBeInTheDocument(); |
| 77 | + expect(selectedOption.getAttribute('class')).toMatch(/singlevalue/gi); |
| 78 | + }); |
| 79 | + |
| 80 | + it('Correctly displays a defaultValue', async () => { |
| 81 | + render( |
| 82 | + <Dropdown |
| 83 | + {...{ |
| 84 | + id, |
| 85 | + label, |
| 86 | + options, |
| 87 | + onSelect, |
| 88 | + placeholder, |
| 89 | + defaultValue: options.at(-1) |
| 90 | + }} |
| 91 | + /> |
| 92 | + ); |
| 93 | + |
| 94 | + const selectedOption = screen.getByText('Option C'); |
| 95 | + expect(selectedOption).toBeInTheDocument(); |
| 96 | + expect(selectedOption.getAttribute('class')).toMatch(/singlevalue/gi); |
| 97 | + |
| 98 | + expect(screen.queryByText('Option A')).not.toBeInTheDocument(); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +/** |
| 103 | + * Multi-select |
| 104 | + */ |
| 105 | +describe('Multi-select Dropdown', () => { |
| 106 | + const multiProperties = { |
| 107 | + id, |
| 108 | + label, |
| 109 | + options, |
| 110 | + onSelect, |
| 111 | + placeholder, |
| 112 | + isMulti: true |
| 113 | + }; |
| 114 | + |
| 115 | + it('(Mouse) Selects an option and displays pill', async () => { |
| 116 | + const optionLabel = 'Option A'; |
| 117 | + const user = userEvent.setup(); |
| 118 | + |
| 119 | + render(<Dropdown {...multiProperties} />); |
| 120 | + await act(async () => { |
| 121 | + await user.click(screen.getByText(label)); |
| 122 | + }); |
| 123 | + |
| 124 | + expect(screen.getByText(optionLabel)).toBeInTheDocument(); |
| 125 | + expect(screen.getByText(optionLabel).getAttribute('class')).toMatch( |
| 126 | + /option/gi |
| 127 | + ); |
| 128 | + await act(async () => { |
| 129 | + await user.click(screen.getByText(optionLabel)); |
| 130 | + }); |
| 131 | + |
| 132 | + const pills = screen.queryAllByRole('listitem'); |
| 133 | + expect(pills.length).toBe(1); |
| 134 | + |
| 135 | + const selectedOption = pills[0]; |
| 136 | + expect(selectedOption).toHaveClass('pill'); |
| 137 | + expect(selectedOption).toHaveTextContent(optionLabel); |
| 138 | + }); |
| 139 | + |
| 140 | + it('(Keyboard) Navigation, selection, de-selection', async () => { |
| 141 | + const optionLabel = 'Option C'; |
| 142 | + const user = userEvent.setup(); |
| 143 | + |
| 144 | + render(<Dropdown {...multiProperties} />); |
| 145 | + |
| 146 | + const beforeSelection = screen.queryAllByRole('listitem'); |
| 147 | + |
| 148 | + expect(beforeSelection.length).toBe(0); |
| 149 | + expect(screen.queryByText(optionLabel)).not.toBeInTheDocument(); |
| 150 | + |
| 151 | + // Choose 'Option C' and close menu |
| 152 | + await user.click(screen.getByText(label)); |
| 153 | + await user.keyboard( |
| 154 | + '{Tab}{Tab}{Shift>}{Tab}{/Shift}{Tab}{Enter}{Escape}{Tab}' |
| 155 | + ); |
| 156 | + |
| 157 | + // Verify other options are hidden |
| 158 | + expect(screen.queryByText('Option A')).not.toBeInTheDocument(); |
| 159 | + expect(screen.queryByText('Option B')).not.toBeInTheDocument(); |
| 160 | + |
| 161 | + // Verify pill displayed |
| 162 | + expect(screen.getByText(optionLabel)).toBeInTheDocument(); |
| 163 | + const afterSelection = screen.queryAllByRole('listitem'); |
| 164 | + expect(afterSelection.length).toBe(1); |
| 165 | + expect(afterSelection[0]).toHaveClass('pill'); |
| 166 | + expect(afterSelection[0]).toHaveTextContent(optionLabel); |
| 167 | + |
| 168 | + // Delete selection |
| 169 | + await act(async () => { |
| 170 | + await user.click(screen.getByText(label)); |
| 171 | + await user.keyboard('{Delete}'); |
| 172 | + }); |
| 173 | + |
| 174 | + // No pills |
| 175 | + expect(screen.queryAllByRole('listitem').length).toBe(0); |
| 176 | + }); |
| 177 | + |
| 178 | + it('(Keyboard) Pills interaction', async () => { |
| 179 | + const optionLabel = 'Option C'; |
| 180 | + const user = userEvent.setup(); |
| 181 | + |
| 182 | + // All options selected by default |
| 183 | + render(<Dropdown {...multiProperties} defaultValue={options} />); |
| 184 | + |
| 185 | + // Verify pills displayed |
| 186 | + const afterSelection = screen.queryAllByRole('listitem'); |
| 187 | + expect(afterSelection.length).toBe(3); |
| 188 | + expect(afterSelection[2]).toHaveClass('pill'); |
| 189 | + expect(afterSelection[2]).toHaveTextContent(optionLabel); |
| 190 | + |
| 191 | + // Focus on pill and delete selection |
| 192 | + await act(async () => { |
| 193 | + await user.click(screen.getByText(label)); |
| 194 | + await user.keyboard('{Shift}{Tab}{Tab}{/Shift}{Enter}'); |
| 195 | + }); |
| 196 | + |
| 197 | + // Verify correct option's pill was removed, while others remain |
| 198 | + const afterDelete = screen.queryAllByRole('listitem'); |
| 199 | + expect(afterDelete.length).toBe(2); |
| 200 | + expect(afterDelete[0]).toHaveTextContent('Option B'); |
| 201 | + expect(afterDelete[1]).toHaveTextContent('Option C'); |
| 202 | + }); |
| 203 | + |
| 204 | + it('Correctly displays a default option', async () => { |
| 205 | + render( |
| 206 | + <Dropdown |
| 207 | + {...{ |
| 208 | + ...multiProperties, |
| 209 | + defaultValue: [options[1], options[2]] |
| 210 | + }} |
| 211 | + /> |
| 212 | + ); |
| 213 | + |
| 214 | + expect(screen.queryByText('Option A')).not.toBeInTheDocument(); |
| 215 | + expect(screen.getByText('Option B')).toBeInTheDocument(); |
| 216 | + expect(screen.getByText('Option C')).toBeInTheDocument(); |
| 217 | + }); |
| 218 | +}); |
0 commit comments