question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty list when testing with react-testing-library

See original GitHub issue

Hi, first of all, congrats for your awesome library! I’ve been trying to test some components where I use it, but the list never gets rendered 😞

import * as React from 'react';
import { Virtuoso } from 'react-virtuoso';
import {  render } from '@testing-library/react';

function Component () {
     return (
         <Virtuoso
                totalCount={100}
                item={index => <div>{index}</div>}
          />
     );
}

describe('Test', () => {
   it('should render list of items', () => {
        const { debug } = render(<Component />);
        debug(); // Prints an empty list
   });
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:22 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
tanato-citcommented, May 18, 2022

The two provided solutions didn’t seemed great to me,

Setting the initialItemCount fixed in the code when not in SSR is quite dangerous, as it’ll basically render all elements thus removing the purpose of Virtualization and also can lead to multiple renders.

The second solution to override the initialItemCount on the component code that uses Virtuoso according to the environment that’s running (test vs production) doesn’t seem great either as the component code will be messy.

A better solution in my opinion is to override the Virtuoso implementation by creating a mocked version with a HOC that set this attribute, like this: (in this example I’m setting the initialItemCount to the same value as totalCount so it render all elements)

import { ElementType, Component as mockComponent } from 'react'

// ...

jest.mock('react-virtuoso', () => {
  const { Virtuoso } = jest.requireActual('react-virtuoso')

  const mockVirtuoso = (WrappedVirtuoso: ElementType) =>
    class extends mockComponent<{ totalCount: number }, unknown> {
      render() {
        return <WrappedVirtuoso initialItemCount={this.props?.totalCount} {...this.props} />
      }
    }
  return { Virtuoso: mockVirtuoso(Virtuoso) }
})
7reactions
petyosicommented, Aug 1, 2019

Hi,

I am not familiar with the testing-library internals, but my guess is that it is executed in a node environment, bypassing the hooks, effects, etc. If this is so, you can probably make it work by setting the initialItemCount prop, available in v0.10 (released yesterday). Check the demo: https://stackblitz.com/edit/react-virtuoso-server-side-rendering?file=example.js It is also mentioned in the API reference: https://virtuoso.dev/virtuoso-api-reference/

Please let me know if this works for you since it is a new feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to test a component if the component returns empty array ...
i am trying to learn react-testing-library and i got stuck at this point. I have one array that returns an empty array in...
Read more >
API | Testing Library
React Testing Library re-exports everything from DOM Testing Library as well. ... See Queries for a complete list. Example.
Read more >
React JS Testing Lists (TDD) | React Testing Library - YouTube
In this video I show you how to test lists in React.js! We'll take a test driven development (TDD) approach to first focus...
Read more >
Testing Recipes - React
Common testing patterns for React components. Note: This page assumes you're using Jest as a test runner. If you use a different test...
Read more >
React Integration Testing with React-testing-library - Toptal
This tutorial explains how to properly employ react-testing-library to conduct integration tests on your React app.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found