mui / mui-x

MUI X: Build complex and data-rich applications using a growing list of advanced React components, like the Data Grid, Date and Time Pickers, Charts, and more!
https://mui.com/x/
4.08k stars 1.26k forks source link

[data grid] Cannot use data grid with react testing library #13369

Closed benparks1976 closed 3 months ago

benparks1976 commented 3 months ago

Steps to reproduce

As this problem occurs in the react test I'm not able to provide a live link. Instead I have attached react project including a test reproduces the issue. I hope this sufficient. mui-repro.zip

Steps.

  1. Create a component that uses the MUI data grid 7.6.1
  2. Create a react test using the react testing framework
  3. Run the test

Current behavior

When the component is rendered by the react testing framework the following error is shown:

Error: Uncaught [TypeError: apiRef.current.state.rows.dataRowIds.at is not a function]

Expected behavior

The error should not occur when rendering the MUI data grid component.

When the same test is run with Mui data grid version 6.9.2 the problem does not occur.

Context

Write unit tests for our components that use Mui data grid 7.6.1.

Your environment

npx @mui/envinfo System: OS: Windows 11 10.0.22631 Binaries: Node: 14.20.1 - C:\Program Files\nodejs\node.EXE npm: 6.14.17 - C:\Program Files\nodejs\npm.CMD pnpm: Not Found Browsers: Chrome: Not Found Edge: Chromium (125.0.2535.79)

Search keywords: react testing library with mui data grid

michelengelen commented 3 months ago

Hey @benparks1976 ... my colleague @JCQuintas noticed that you were using the old syntax. If you change the code in your example to this it should work:

import React from "react";
import Demo from "../demo";
import { render } from "@testing-library/react";

describe("when rendering the data grid", () => {
    it("should render the table columns", async () => {
        const {findAllByRole} = render(<Demo/>)

        const headers = await findAllByRole("columnheader")!;

        expect(headers[1].textContent).toBe("Serial number");
        expect(headers[2].textContent).toBe("Model name");
    });
})

Could you try that please?

benparks1976 commented 3 months ago

Hi, thanks for the follow up. I tried your suggestion and I still got the same issue:

image

michelengelen commented 3 months ago

It does work on my machine:

Screenshot 2024-06-05 at 11 33 09

Did you try a fresh install?

JCQuintas commented 3 months ago

Hi @benparks1976, the tests are green on my side with the example code you provided. I've noticed you are using node 14, which doesn't support the array.at() function. I would suggest upgrading your node to a more recent version, at is available from 16.6.

Otherwise you could also add a polyfill. eg: https://www.npmjs.com/package/array.prototype.at

benparks1976 commented 3 months ago

Thanks @JCQuintas, this is the problem. Thanks all for your help.

benparks1976 commented 3 months ago

Closing...

github-actions[bot] commented 3 months ago

:warning: This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

@benparks1976: How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

TrinhNoBamBum commented 1 month ago

I am using node 18 and still having the above problem, please help me