Skip to content

Commit 764884a

Browse files
authored
Merge branch 'master' into replace-user-type-with-user-keyboard
2 parents ca402da + 3b216e0 commit 764884a

File tree

1 file changed

+10
-93
lines changed

1 file changed

+10
-93
lines changed

src/use-dropdown-menu.test.tsx

+10-93
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Imports
22
import React, { useState } from 'react';
33
import useDropdownMenu, { DropdownMenuOptions } from './use-dropdown-menu';
4-
import { fireEvent, render, screen } from '@testing-library/react';
4+
import { render, screen } from '@testing-library/react';
55
import userEvent from '@testing-library/user-event';
66
import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup';
77

@@ -183,14 +183,7 @@ it('Moves the focus to the next element in the menu after pressing the down arro
183183

184184
expect(screen.getByText('1 Item')).toHaveFocus();
185185

186-
fireEvent(
187-
screen.getByText('1 Item'),
188-
new KeyboardEvent('keydown', {
189-
key: 'ArrowDown',
190-
bubbles: true,
191-
cancelable: true,
192-
})
193-
);
186+
await user.keyboard('{ArrowDown}');
194187

195188
expect(screen.getByText('2 Item')).toHaveFocus();
196189
});
@@ -204,25 +197,11 @@ it('Moves the focus to the previous element in the menu after pressing the up ar
204197

205198
expect(screen.getByText('1 Item')).toHaveFocus();
206199

207-
fireEvent(
208-
screen.getByText('1 Item'),
209-
new KeyboardEvent('keydown', {
210-
key: 'ArrowDown',
211-
bubbles: true,
212-
cancelable: true,
213-
})
214-
);
200+
await user.keyboard('{ArrowDown}');
215201

216202
expect(screen.getByText('2 Item')).toHaveFocus();
217203

218-
fireEvent(
219-
screen.getByText('2 Item'),
220-
new KeyboardEvent('keydown', {
221-
key: 'ArrowUp',
222-
bubbles: true,
223-
cancelable: true,
224-
})
225-
);
204+
await user.keyboard('{ArrowUp}');
226205

227206
expect(screen.getByText('1 Item')).toHaveFocus();
228207
});
@@ -236,14 +215,7 @@ it('Wraps the focus to the last element when pressing the up arrow at the beginn
236215

237216
expect(screen.getByText('1 Item')).toHaveFocus();
238217

239-
fireEvent(
240-
screen.getByText('1 Item'),
241-
new KeyboardEvent('keydown', {
242-
key: 'ArrowUp',
243-
bubbles: true,
244-
cancelable: true,
245-
})
246-
);
218+
await user.keyboard('{ArrowUp}');
247219

248220
expect(screen.getByText('4 Item')).toHaveFocus();
249221
});
@@ -257,25 +229,11 @@ it('Wraps the focus to the first element when pressing the down arrow at the end
257229

258230
expect(screen.getByText('1 Item')).toHaveFocus();
259231

260-
fireEvent(
261-
screen.getByText('1 Item'),
262-
new KeyboardEvent('keydown', {
263-
key: 'ArrowUp',
264-
bubbles: true,
265-
cancelable: true,
266-
})
267-
);
232+
await user.keyboard('{ArrowUp}');
268233

269234
expect(screen.getByText('4 Item')).toHaveFocus();
270235

271-
fireEvent(
272-
screen.getByText('4 Item'),
273-
new KeyboardEvent('keydown', {
274-
key: 'ArrowDown',
275-
bubbles: true,
276-
cancelable: true,
277-
})
278-
);
236+
await user.keyboard('{ArrowDown}');
279237

280238
expect(screen.getByText('1 Item')).toHaveFocus();
281239
});
@@ -350,50 +308,9 @@ it('Can navigate to a dynamically-added item', async () => {
350308

351309
await user.click(screen.getByText('Primary'));
352310

353-
fireEvent(
354-
screen.getByText('Primary'),
355-
new KeyboardEvent('keydown', {
356-
key: 'ArrowDown',
357-
bubbles: true,
358-
cancelable: true,
359-
})
360-
);
361-
362-
fireEvent(
363-
screen.getByText('1 Item'),
364-
new KeyboardEvent('keydown', {
365-
key: 'ArrowDown',
366-
bubbles: true,
367-
cancelable: true,
368-
})
369-
);
370-
371-
fireEvent(
372-
screen.getByText('2 Item'),
373-
new KeyboardEvent('keydown', {
374-
key: 'ArrowDown',
375-
bubbles: true,
376-
cancelable: true,
377-
})
378-
);
379-
380-
fireEvent(
381-
screen.getByText('3 Item'),
382-
new KeyboardEvent('keydown', {
383-
key: 'ArrowDown',
384-
bubbles: true,
385-
cancelable: true,
386-
})
387-
);
388-
389-
fireEvent(
390-
screen.getByText('4 Item'),
391-
new KeyboardEvent('keydown', {
392-
key: 'ArrowDown',
393-
bubbles: true,
394-
cancelable: true,
395-
})
396-
);
311+
for (let i = 0; i < 4; i += 1) {
312+
await user.keyboard('{ArrowDown}');
313+
}
397314

398315
expect(screen.getByText('5 Item')).toHaveFocus();
399316
});

0 commit comments

Comments
 (0)