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

"Cannot read property 'childElementCount' of null" error generated by react-test-renderer #233

Closed
rkoziel opened this issue Aug 19, 2019 · 7 comments

Comments

@rkoziel
Copy link

rkoziel commented Aug 19, 2019

The error "TypeError: Cannot read property 'childElementCount' of null" is being thrown by react-test-renderer for components that we have implemented the Accordion on, and parent components. This looks like it may be related to how react-test-renderer is rendering the AccordionItemHeading. Are there any recommendations on how to get around this error so we can still use react-test-renderer for our testing?

Thanks,
Ray Koziel

@ryami333
Copy link
Contributor

ryami333 commented Aug 20, 2019

Gidday @rkoziel, thanks for taking the time to file an issue with us. We're not doing anything particularly magical under the hood, so I can't imagine why you might be experiencing this issue. I'll try and take a poke around - but would you please be able to send me a minimal reproducible failing case? Also could you please let me know which versions of react, react-test-renderer and react-accessible-accordion you're using? Thanks!

@rkoziel
Copy link
Author

rkoziel commented Aug 20, 2019 via email

@ryami333
Copy link
Contributor

Cool thanks for that info @rkoziel. As for the reproducible case - just a copy+paste snippet of a failing test would be really useful, I need to see what kind of context we're dealing with here.

Cheers!

@tbogdziun
Copy link

I had the same problem. To fix it in library pls check ref value before check this childElementCount in AccordionItemHeading component.
For now @rkoziel i founded solution for fixing test use https://jestjs.io/docs/en/bypassing-module-mocks and bypass react-accessible-accordion in tests to make it pass it's only solution before this bug will be fixed.

in my case that was solution:

jest.mock('react-accessible-accordion')
import renderer from 'react-test-renderer'

import AccordionWrapper from '..'
const {
Accordion,
AccordionItem,
AccordionItemHeading,
AccordionItemPanel,
AccordionItemButton,
} = jest.requireActual('react-accessible-accordion')

and test pass.

@ryami333
Copy link
Contributor

Closing pending more specific details

@Vanuan
Copy link

Vanuan commented Mar 11, 2020

Struggling with this as well. It's a known issue when testing components with use refs (html nodes):
facebook/react#7740

The workaround is to provide a Node mock:

        act(() => {
          tree = create(element, {
            createNodeMock: (element) => {
              if (element.type === 'div') {
                // mock node interface
                return { 
                  querySelectorAll: () => [],
                  contains: () => {},
                  click: () => {},
                  getAttribute: (name) => {
                    if(name === 'data-accordion-component') {
                      return 'AccordionItemButton';
                    }
                  },
                  childElementCount: 1,
                  firstElementChild: { getAttribute: () => 'AccordionItemButton' }
                };
              }
              return null;
            }
          });
        });

@ivanji
Copy link

ivanji commented Apr 29, 2020

Getting exact same error when attempting to create a snapshot test of the wrapper component. The accordion implementation is similar to the ones taken from your examples - nothing fancy.

The test is the following:

  expect(tree).toMatchSnapshot()``` 

'Items' refers to a short array with same values to the ones used in prod. 

error: "Cannot read property 'childElementCount' of null"


More details from the console:

```The above error occurred in the <AccordionItemHeading> component:
          in AccordionItemHeading (created by Context.Consumer)
          in Consumer (created by AccordionItemHeading)
          in AccordionItemHeading (at CollapsibleHelp.js:16)
          in div (created by AccordionItem)
          in Consumer (created by Provider)
          in Provider (created by Context.Consumer)
          in Consumer (created by ProviderWrapper)
          in ProviderWrapper (created by AccordionItem)
          in AccordionItem (at CollapsibleHelp.js:15)
          in div (created by Context.Consumer)
          in Consumer (created by Accordion)
          in Provider (created by Accordion)
          in Accordion (at CollapsibleHelp.js:13)
          in CollapsibleHelp (at CollapsibleHelp.test.js:28)
      
      Consider adding an error boundary to your tree to customize error handling behavior.
      Visit https://fb.me/react-error-boundaries to learn more about error boundaries.````

Hope these extra details are useful. If you need further details do let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants