FlexGrid unit test with Jest and RTL can't simulate row selection event

Posted by: scott.chinn on 11 August 2020, 9:09 pm EST

  • Posted 11 August 2020, 9:09 pm EST

    Hi, I’m trying to get a selectionChanged event to fire in my Jest / React Testing Library test. I’m able to find grid cells and then click them, but the selectionChanged event doesn’t seem to fire. Below is my code. Do you know if it’s possible to use the click event? Or maybe there is some other way? Thanks!

    /**  * @jest-environment jsdom-sixteen  */
    
    import '@grapecity/wijmo.styles/wijmo.css';
    import React from 'react';
    import {
        FlexGrid,
        FlexGridColumn,
    } from '@grapecity/wijmo.react.grid';
    
    import {render, screen, waitFor, waitForDomChange} from '@testing-library/react';
    import userEvent from '@testing-library/user-event';
    
    const mockRowData = [{"pk":1,"shortName":"Burrito Short","longName":"Burrito Long"},
        {"pk":2,"shortName":"Taco Short","longName":"Taco Long"}
    ];
    
    it('should render row data on the screen and fire selection changed event', async () => {
    
        const onSelectionChanged = jest.fn();
    
        render(
            <FlexGrid virtualizationThreshold={ [100, 100] }
                      itemsSource={mockRowData}
                      selectionChanged={onSelectionChanged}
                      selectionMode={'Row'}
            >
                <FlexGridColumn header="Pri Key" binding="pk" width={100} />
                <FlexGridColumn header="Short Name" binding="shortName" width={100} />
                <FlexGridColumn header="Long Name" binding="longName" width={100} />
            </FlexGrid>
        );
    
        let node = await screen.findByText(/Taco Short/i);
        userEvent.click(node);
        node = await screen.findByText(/Burrito Long/i);
        userEvent.click(node);
        expect(onSelectionChanged).toHaveBeenCalledTimes(1);
    
    });
    
  • Posted 12 August 2020, 12:32 pm EST

    I should have add that this issue is related to this one that was solved already:

    https://www.grapecity.com/forums/wijmo/flexgrid-unit-test-with-je

  • Posted 13 August 2020, 2:26 am EST

    Hi Scott,

    Sorry for the delayed response. We are investigating this and will update you as soon as possible.

  • Posted 13 August 2020, 6:24 am EST

    Hi Scott,

    It seems that you need to wrap the userEvent.click method call inside the act method of react-testing-library. If there is any state change in the react component (and I think that the click method changes the state), then it should be wrapped inside the act callback.

    Reference: https://stackoverflow.com/a/62291978

    So, you test code will be:

    let node = await screen.findByText(/Taco Short/i);
    act(() => userEvent.click(node));
    node = await screen.findByText(/Burrito Long/i);
    act(() => userEvent.click(node));
    waitFor(() => expect(onSelectionChanged).toHaveBeenCalledTimes(2));
    

    Also, the selectionChanged event will be fired 2 times according to your test code. So, please make sure to update the toHaveBeenCalledTimes method accordingly.

    Pleaser refer to the sample attached.

    Regards,

    Ashwin

    selection test.zip

  • Posted 13 August 2020, 11:54 am EST

    Thanks Ashwin, but I’m not sure if it’s really working. In your example, if I change the last line to expect 7 calls, it still passes. Seems like a “false positive”:

    waitFor(() => expect(onSelectionChanged).toHaveBeenCalledTimes(7));

    Actually, what really want to do is attach the selectionChange listener and have it return the selected row so I can then pass it to another component. Maybe this is too ambitious, but this kind of integration testing is supposed to be possible with the react testing library. But if we can get this simple case with the mock function working properly that’s good too.

    Thanks,

    Scott

  • Posted 14 August 2020, 7:37 am EST

    Hi Scott,

    Sorry, I must have forgot to check for false positive. During investigation, I found that the issue was due to userEvent.click method. The element provided in this method is correct but it it not clicking on this exact element. This method just clicks on the current focused element regardless of you provide as the function parameter.

    I will investigate this more and will update you on Monday.

    ~regards

  • Posted 17 August 2020, 1:07 am EST

    Hi Scott,

    It seems that the issue is in the userEvent.click method but I have forwarded this to the developers for further investigation with internal tracking id 457622. I will update you once I will hear from them.

    ~regards

  • Posted 26 August 2020, 8:33 am EST

    Hi Scott,

    The dev team has said that clicking on cell or analogues action is not testable by react testing library due to the library limitation.

    FlexGrid requires real DOM with the correct position and size of elements whereas the testing library is based on jest-dom which does not correctly handle the size and position of elements(https://github.com/jsdom/jsdom/issues/653, https://github.com/testing-library/react-testing-library/issues/353).

    We would suggest you use another testing framework (for example selenium, cypress) to test FlexGrid mouse events.

    We are sorry for the inconvenience.

    ~regards

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels