|
1 | 1 | import { expect } from 'vitest'; |
2 | 2 | import * as React from 'react'; |
3 | 3 | import { Combobox } from '@base-ui/react/combobox'; |
4 | | -import { createRenderer, describeConformance } from '#test-utils'; |
| 4 | +import { createRenderer, describeConformance, isJSDOM } from '#test-utils'; |
5 | 5 | import { fireEvent, screen, waitFor } from '@mui/internal-test-utils'; |
6 | 6 |
|
7 | 7 | describe('<Combobox.Popup />', () => { |
@@ -150,4 +150,102 @@ describe('<Combobox.Popup />', () => { |
150 | 150 | expect(screen.getByRole('button', { name: 'final focus' })).toHaveFocus(); |
151 | 151 | }); |
152 | 152 | }); |
| 153 | + |
| 154 | + // `inert` is only implemented in a real browser; jsdom keeps focus inside the closing popup. |
| 155 | + describe.skipIf(isJSDOM)('exit animation', () => { |
| 156 | + const style = ` |
| 157 | + @keyframes combobox-close-test { |
| 158 | + to { |
| 159 | + opacity: 0; |
| 160 | + } |
| 161 | + } |
| 162 | +
|
| 163 | + .animation-test-popup[data-ending-style] { |
| 164 | + animation: combobox-close-test 500ms linear; |
| 165 | + } |
| 166 | + `; |
| 167 | + |
| 168 | + // The common layout: the input sits outside the popup, so the focus manager runs with |
| 169 | + // `returnFocus: false` on the assumption that focus never left the input. A control inside the |
| 170 | + // popup breaks that assumption. |
| 171 | + function AnimatedCombobox() { |
| 172 | + return ( |
| 173 | + <React.Fragment> |
| 174 | + {/* eslint-disable-next-line react/no-danger */} |
| 175 | + <style dangerouslySetInnerHTML={{ __html: style }} /> |
| 176 | + <Combobox.Root items={['a', 'b']}> |
| 177 | + <Combobox.Input data-testid="input" /> |
| 178 | + <Combobox.Portal> |
| 179 | + <Combobox.Positioner> |
| 180 | + <Combobox.Popup data-testid="popup" className="animation-test-popup"> |
| 181 | + <Combobox.List> |
| 182 | + {(item: string) => ( |
| 183 | + <Combobox.Item key={item} value={item}> |
| 184 | + {item} |
| 185 | + </Combobox.Item> |
| 186 | + )} |
| 187 | + </Combobox.List> |
| 188 | + <button type="button" data-testid="inside"> |
| 189 | + Create new |
| 190 | + </button> |
| 191 | + </Combobox.Popup> |
| 192 | + </Combobox.Positioner> |
| 193 | + </Combobox.Portal> |
| 194 | + </Combobox.Root> |
| 195 | + <button type="button" data-testid="after"> |
| 196 | + After |
| 197 | + </button> |
| 198 | + </React.Fragment> |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + it('returns focus to the input when the closing popup held it', async ({ onTestFinished }) => { |
| 203 | + globalThis.BASE_UI_ANIMATIONS_DISABLED = false; |
| 204 | + onTestFinished(() => { |
| 205 | + globalThis.BASE_UI_ANIMATIONS_DISABLED = true; |
| 206 | + }); |
| 207 | + |
| 208 | + const { user } = await render(<AnimatedCombobox />); |
| 209 | + await user.click(screen.getByTestId('input')); |
| 210 | + await user.click(await screen.findByTestId('inside')); |
| 211 | + expect(screen.getByTestId('inside')).toHaveFocus(); |
| 212 | + |
| 213 | + await user.keyboard('{Escape}'); |
| 214 | + expect(screen.getByTestId('popup')).toHaveAttribute('data-ending-style'); |
| 215 | + |
| 216 | + // Making the closing popup inert blurs whatever it held, so without a fallback focus would |
| 217 | + // sit on `<body>` for the whole exit animation, restarting the tab order at the top of the |
| 218 | + // document. |
| 219 | + await waitFor(() => expect(screen.getByTestId('input')).toHaveFocus()); |
| 220 | + |
| 221 | + await user.keyboard('{Tab}'); |
| 222 | + expect(screen.getByTestId('after')).toHaveFocus(); |
| 223 | + }); |
| 224 | + |
| 225 | + it('leaves focus alone when an outside press moved it out of the popup', async ({ |
| 226 | + onTestFinished, |
| 227 | + }) => { |
| 228 | + globalThis.BASE_UI_ANIMATIONS_DISABLED = false; |
| 229 | + onTestFinished(() => { |
| 230 | + globalThis.BASE_UI_ANIMATIONS_DISABLED = true; |
| 231 | + }); |
| 232 | + |
| 233 | + const { user } = await render( |
| 234 | + <React.Fragment> |
| 235 | + <p data-testid="plain">Not focusable</p> |
| 236 | + <AnimatedCombobox /> |
| 237 | + </React.Fragment>, |
| 238 | + ); |
| 239 | + await user.click(screen.getByTestId('input')); |
| 240 | + await user.click(await screen.findByTestId('inside')); |
| 241 | + expect(screen.getByTestId('inside')).toHaveFocus(); |
| 242 | + |
| 243 | + // Pressing non-focusable content blurs to `<body>`. That is the user's own gesture rather |
| 244 | + // than `inert` stranding focus, so the popup must not claw it back to the input. |
| 245 | + await user.click(screen.getByTestId('plain')); |
| 246 | + expect(screen.getByTestId('popup')).toHaveAttribute('data-ending-style'); |
| 247 | + |
| 248 | + await waitFor(() => expect(screen.getByTestId('input')).not.toHaveFocus()); |
| 249 | + }); |
| 250 | + }); |
153 | 251 | }); |
0 commit comments