Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to render LineChart on React Testing Library #2232

Open
joaopedromatias opened this issue Feb 1, 2023 · 5 comments
Open

Unable to render LineChart on React Testing Library #2232

joaopedromatias opened this issue Feb 1, 2023 · 5 comments
Labels

Comments

@joaopedromatias
Copy link
Contributor

Description
Unable to render LineChart inside React Testing Library

Reproduce

// test code: 

it( 'should render the line chart', () => {

  render(<div style={{width: '1000px', height: '1000px'}}><ResponsiveLine
              data={[
                 { 
                    id: 'A',
                    data: [
                        {x: '12-20-2022', y: '201.05'},
                    ]
                },
                {   id: 'B',
                    data: [
                        {x: '12-20-2022', y: '201.05'}
                    ]
                }]
              }   
              margin={{ top: 50, right: 110, bottom: 90, left: 60 }}
              xScale={{ type: 'point' }}
              curve='linear'
              yScale={{
                  type: 'linear',
                  min: 'auto', 
                  max: 'auto', 
                  stacked: false,
                  reverse: false,
                  tickValues: 10
              }}
              yFormat="> $.2f" 
              axisBottom={{
                  orient: 'bottom',
                  tickSize: 5,
                  tickPadding: 2,
                  tickRotation: 60,
                  legend: 'Data de uso',
                  legendOffset: 80,
                  legendPosition: 'end'       
              }}
              axisLeft={{
                  tickValues: 10,
                  orient: 'left',
                  tickSize: 5,
                  tickPadding: 2,
                  tickRotation: 0,
                  legend: 'Custo (USD)',
                  legendOffset: -50,
                  legendPosition: 'end'
              }}
              pointSize={4}
              colors={{ scheme: 'set1' }}
              pointColor={{ theme: 'background' }}
              pointBorderWidth={2}
              pointBorderColor={{ from: 'serieColor' }}
              pointLabelYOffset={-12}
              isInteractive={true}
              enableSlices='x'
              crosshairType='x'
              enableGridX={false}
              legends={[
                  {
                      anchor: 'top-left',
                      direction: 'row',
                      justify: false,
                      translateX: 10,
                      onClick: (e) => { console.log(e)},
                      translateY: -35,
                      itemsSpacing: 0,
                      itemDirection: 'left-to-right',
                      itemWidth: 80,
                      itemHeight: 20,
                      itemOpacity: 0.75,
                      symbolSize: 15,
                      symbolShape: 'circle',
                      symbolBorderColor: 'rgba(0, 0, 0, .5)',
                  }
              ]}
          /></div>)

          screen.debug() // shows the component on terminal

     }

The DOM rendered does not contain the chart:

Captura de Tela 2023-02-01 às 16 10 28

{
   "dependencies": {
      "jest-environment-jsdom": "^29.3.1",
      "@testing-library/jest-dom": "^5.16.5",
      "@testing-library/react": "^12.1.5",
     "ts-jest": "^29.0.5",
     "@nivo/bar": "^0.80.0",
     "@nivo/core": "^0.80.0",
     "@nivo/line": "^0.80.0",
   }
}

Expected behavior
It should be rendered by the React Testing Library on the jsdom environment

Desktop (please complete the following information):

  • OS: MacOS
@djbowers
Copy link

Having the same issue here. I am rendering the line chart as a story in storybook and everything is working great there, but when I try to render the same story with jest, all I get is an empty div like above.

@KadeerAmin
Copy link

Bump. We have our graphs working for our data visualizations but are unable to regression test using react-testing-library.

@plouc plouc mentioned this issue May 3, 2023
@beohoang98
Copy link

beohoang98 commented Jul 24, 2023

I think the reason is here https://github.com/plouc/nivo/blob/master/packages/core/src/components/ResponsiveWrapper.js

The responsive measure not work with Jest environment so width & height always are 0 then nothing are rendered

So I found a solution that is mocking ResponsiveWrapper component:

jest.mock("@nivo/core", () => ({
  ...jest.requireActual("@nivo/core"),
  ResponsiveWrapper: ({ children }: any) =>
    children({ width: 400, height: 400 }),
}));

Then it worked

@joaopedromatias
Copy link
Contributor Author

Great @beohoang98! thank you

Copy link

stale bot commented Dec 15, 2023

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the stale label Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants