Skip to content

Commit 6239c3a

Browse files
authored
fix: fire mouse events with the correct properties (testing-library#296)
1 parent bfbe92c commit 6239c3a

File tree

4 files changed

+555
-57
lines changed

4 files changed

+555
-57
lines changed

src/__tests__/click.js

+178
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,181 @@ test.each(['input', 'textarea'])(
431431
expect(screen.getByTestId('element')).not.toHaveFocus()
432432
},
433433
)
434+
435+
it('should fire mouse events with the correct properties', () => {
436+
const events = []
437+
const eventsHandler = jest.fn(evt => events.push({
438+
type: evt.type,
439+
button: evt.button,
440+
buttons: evt.buttons,
441+
detail: evt.detail
442+
}))
443+
render(
444+
<div
445+
data-testid="div"
446+
onMouseOver={eventsHandler}
447+
onMouseMove={eventsHandler}
448+
onMouseDown={eventsHandler}
449+
onFocus={eventsHandler}
450+
onMouseUp={eventsHandler}
451+
onClick={eventsHandler}
452+
/>,
453+
)
454+
455+
userEvent.click(screen.getByTestId('div'))
456+
expect(events).toEqual([
457+
{
458+
type: 'mouseover',
459+
button: 0,
460+
buttons: 0,
461+
detail: 0
462+
},
463+
{
464+
type: 'mousemove',
465+
button: 0,
466+
buttons: 0,
467+
detail: 0
468+
},
469+
{
470+
type: 'mousedown',
471+
button: 0,
472+
buttons: 1,
473+
detail: 1
474+
},
475+
{
476+
type: 'mouseup',
477+
button: 0,
478+
buttons: 1,
479+
detail: 1
480+
},
481+
{
482+
type: 'click',
483+
button: 0,
484+
buttons: 1,
485+
detail: 1
486+
},
487+
])
488+
})
489+
490+
it('should fire mouse events with custom button property', () => {
491+
const events = []
492+
const eventsHandler = jest.fn(evt => events.push({
493+
type: evt.type,
494+
button: evt.button,
495+
buttons: evt.buttons,
496+
detail: evt.detail,
497+
altKey: evt.altKey
498+
}))
499+
render(
500+
<div
501+
data-testid="div"
502+
onMouseOver={eventsHandler}
503+
onMouseMove={eventsHandler}
504+
onMouseDown={eventsHandler}
505+
onFocus={eventsHandler}
506+
onMouseUp={eventsHandler}
507+
onClick={eventsHandler}
508+
/>,
509+
)
510+
511+
userEvent.click(screen.getByTestId('div'), {
512+
button: 1,
513+
altKey: true
514+
})
515+
516+
expect(events).toEqual([
517+
{
518+
type: 'mouseover',
519+
button: 0,
520+
buttons: 0,
521+
detail: 0,
522+
altKey: true
523+
},
524+
{
525+
type: 'mousemove',
526+
button: 0,
527+
buttons: 0,
528+
detail: 0,
529+
altKey: true
530+
},
531+
{
532+
type: 'mousedown',
533+
button: 1,
534+
buttons: 4,
535+
detail: 1,
536+
altKey: true
537+
},
538+
{
539+
type: 'mouseup',
540+
button: 1,
541+
buttons: 4,
542+
detail: 1,
543+
altKey: true
544+
},
545+
{
546+
type: 'click',
547+
button: 1,
548+
buttons: 4,
549+
detail: 1,
550+
altKey: true
551+
},
552+
])
553+
})
554+
555+
it('should fire mouse events with custom buttons property', () => {
556+
const events = []
557+
const eventsHandler = jest.fn(evt => events.push({
558+
type: evt.type,
559+
button: evt.button,
560+
buttons: evt.buttons,
561+
detail: evt.detail
562+
}))
563+
render(
564+
<div
565+
data-testid="div"
566+
onMouseOver={eventsHandler}
567+
onMouseMove={eventsHandler}
568+
onMouseDown={eventsHandler}
569+
onFocus={eventsHandler}
570+
onMouseUp={eventsHandler}
571+
onClick={eventsHandler}
572+
/>,
573+
)
574+
575+
userEvent.click(screen.getByTestId('div'), {
576+
buttons: 4
577+
})
578+
579+
expect(events).toEqual([
580+
{
581+
type: 'mouseover',
582+
button: 0,
583+
buttons: 0,
584+
detail: 0
585+
},
586+
{
587+
type: 'mousemove',
588+
button: 0,
589+
buttons: 0,
590+
detail: 0
591+
},
592+
{
593+
type: 'mousedown',
594+
button: 1,
595+
buttons: 4,
596+
detail: 1
597+
},
598+
{
599+
type: 'mouseup',
600+
button: 1,
601+
buttons: 4,
602+
detail: 1
603+
},
604+
{
605+
type: 'click',
606+
button: 1,
607+
buttons: 4,
608+
detail: 1
609+
},
610+
])
611+
})

0 commit comments

Comments
 (0)