Skip to content

Commit 3b216e0

Browse files
authored
Merge pull request #309 from sparksuite/replace-fire-event-with-user-event
Replaced `fireEvent` with `userEvent.keyboard`
2 parents 7007cec + 1adf820 commit 3b216e0

File tree

1 file changed

+10
-93
lines changed

1 file changed

+10
-93
lines changed

src/use-dropdown-menu.test.tsx

Lines changed: 10 additions & 93 deletions
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

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

196196
expect(screen.getByText('1 Item')).toHaveFocus();
197197

198-
fireEvent(
199-
screen.getByText('1 Item'),
200-
new KeyboardEvent('keydown', {
201-
key: 'ArrowDown',
202-
bubbles: true,
203-
cancelable: true,
204-
})
205-
);
198+
await user.keyboard('{ArrowDown}');
206199

207200
expect(screen.getByText('2 Item')).toHaveFocus();
208201
});
@@ -218,25 +211,11 @@ it('Moves the focus to the previous element in the menu after pressing the up ar
218211

219212
expect(screen.getByText('1 Item')).toHaveFocus();
220213

221-
fireEvent(
222-
screen.getByText('1 Item'),
223-
new KeyboardEvent('keydown', {
224-
key: 'ArrowDown',
225-
bubbles: true,
226-
cancelable: true,
227-
})
228-
);
214+
await user.keyboard('{ArrowDown}');
229215

230216
expect(screen.getByText('2 Item')).toHaveFocus();
231217

232-
fireEvent(
233-
screen.getByText('2 Item'),
234-
new KeyboardEvent('keydown', {
235-
key: 'ArrowUp',
236-
bubbles: true,
237-
cancelable: true,
238-
})
239-
);
218+
await user.keyboard('{ArrowUp}');
240219

241220
expect(screen.getByText('1 Item')).toHaveFocus();
242221
});
@@ -252,14 +231,7 @@ it('Wraps the focus to the last element when pressing the up arrow at the beginn
252231

253232
expect(screen.getByText('1 Item')).toHaveFocus();
254233

255-
fireEvent(
256-
screen.getByText('1 Item'),
257-
new KeyboardEvent('keydown', {
258-
key: 'ArrowUp',
259-
bubbles: true,
260-
cancelable: true,
261-
})
262-
);
234+
await user.keyboard('{ArrowUp}');
263235

264236
expect(screen.getByText('4 Item')).toHaveFocus();
265237
});
@@ -275,25 +247,11 @@ it('Wraps the focus to the first element when pressing the down arrow at the end
275247

276248
expect(screen.getByText('1 Item')).toHaveFocus();
277249

278-
fireEvent(
279-
screen.getByText('1 Item'),
280-
new KeyboardEvent('keydown', {
281-
key: 'ArrowUp',
282-
bubbles: true,
283-
cancelable: true,
284-
})
285-
);
250+
await user.keyboard('{ArrowUp}');
286251

287252
expect(screen.getByText('4 Item')).toHaveFocus();
288253

289-
fireEvent(
290-
screen.getByText('4 Item'),
291-
new KeyboardEvent('keydown', {
292-
key: 'ArrowDown',
293-
bubbles: true,
294-
cancelable: true,
295-
})
296-
);
254+
await user.keyboard('{ArrowDown}');
297255

298256
expect(screen.getByText('1 Item')).toHaveFocus();
299257
});
@@ -378,50 +336,9 @@ it('Can navigate to a dynamically-added item', async () => {
378336

379337
await user.click(screen.getByText('Primary'));
380338

381-
fireEvent(
382-
screen.getByText('Primary'),
383-
new KeyboardEvent('keydown', {
384-
key: 'ArrowDown',
385-
bubbles: true,
386-
cancelable: true,
387-
})
388-
);
389-
390-
fireEvent(
391-
screen.getByText('1 Item'),
392-
new KeyboardEvent('keydown', {
393-
key: 'ArrowDown',
394-
bubbles: true,
395-
cancelable: true,
396-
})
397-
);
398-
399-
fireEvent(
400-
screen.getByText('2 Item'),
401-
new KeyboardEvent('keydown', {
402-
key: 'ArrowDown',
403-
bubbles: true,
404-
cancelable: true,
405-
})
406-
);
407-
408-
fireEvent(
409-
screen.getByText('3 Item'),
410-
new KeyboardEvent('keydown', {
411-
key: 'ArrowDown',
412-
bubbles: true,
413-
cancelable: true,
414-
})
415-
);
416-
417-
fireEvent(
418-
screen.getByText('4 Item'),
419-
new KeyboardEvent('keydown', {
420-
key: 'ArrowDown',
421-
bubbles: true,
422-
cancelable: true,
423-
})
424-
);
339+
for (let i = 0; i < 4; i += 1) {
340+
await user.keyboard('{ArrowDown}');
341+
}
425342

426343
expect(screen.getByText('5 Item')).toHaveFocus();
427344
});

0 commit comments

Comments
 (0)