Skip to content

Emphasize most common uses of fireEvent  #469

Closed
@nareshbhatia

Description

@nareshbhatia
  • @testing-library/dom version: 7.5.7
  • Testing Framework and version: Jest 26.0.1
  • DOM Environment: jsdom 16.2.2

Relevant code or config:

import React from 'react';
import { fireEvent, render } from '@testing-library/react';

describe('fireEvent', () => {
    test("fireEvent.click() works!", () => {
        const handleClick = (event: any) => {
            console.log('--->', event.button);
        };

        const { getByText } = render(
            <div onClick={handleClick}>
                Submit
            </div>
        );
        const element = getByText("Submit");

        // ------------------------------------------------
        // This fires only twice, for button=0 and button=1
        // ------------------------------------------------
        fireEvent.click(element, { button: 0 });
        fireEvent.click(element, { button: 1 });
        fireEvent.click(element, { button: 2 });
    });
});

What you did:

I was trying to right-click on an element in my test using fireEvent.click(element, { button: 2 }).

What happened:

No event was fired.

Reproduction:

Please see code above. The click handler is called only twice, with button=0 and button=1. It is not called with button=2.

Problem description:

fireEvent.click() does not work for right-click.

Also the docs are a bit confusing. They imply that for left-click use button=0 and for right-click use button=2. However according to MDN docs, 0 is no buttons, 1 is left button, 2 is right button - there is a mismatch. Please see below:

  • 0 : No button or un-initialized
  • 1 : Primary button (usually the left button)
  • 2 : Secondary button (usually the right button)
  • 4 : Auxilary button (usually the mouse wheel button or middle button)
  • 8 : 4th button (typically the "Browser Back" button)
  • 16 : 5th button (typically the "Browser Forward" button)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions