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.

Is CreateNodeMock supported?

See original GitHub issue

Is CreateNodeMock supported?

I am not sure if I am using this wrong, but I am trying to mock a ref method. According to the API for the render function, it states:

function render(
  component: React.Element<any>,
  options?: {
    /* A React Component that renders `component` as children */
    wrapper?: React.ComponentType<any>,
    /* You won't often use this, but it's helpful when testing refs */
    createNodeMock: (element: React.Element<any>) => any,
  }
): RenderResult {}

I am passing in the options like so (which I got from react-test-renderer)

const options = {
   createNodeMock: (element) => {
      console.log("element", element);
      return {
         takePictureAsync: takePictureMock
       };
    }
};

const {getByType, queryByTestId} = render(<Camera/>, options);

I never see the console.log print anything and I cannot find a test in render.test.js that covers the use of CreateNodeMock.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
almeynmancommented, Mar 24, 2020

For those who will encounter this problem in the future.

Worked around it by

  1. Introduce a useRef optional prop and by default use react’s one
import React, { useRef as defaultUseRef } from 'react'
const component = ({ useRef = defaultUseRef }) => {
  const ref = useRef(null)
  return <RefComponent ref={ref} />
}
  1. in test mock useRef
const mockUseRef = (obj: any) => () => Object.defineProperty({}, 'current', {
  get: () => obj,
  set: () => {}
})

// in your test
...
    const useRef = mockUseRef({ refFunction: jest.fn() })
    render(
      <ScanBarcodeView onScan={handleScan} useRef={useRef} />,
    )
...
1reaction
thymikeecommented, Oct 21, 2019

We pass it to react-test-renderer, but it doesn’t seem to work with react-native, at least the last time I tried to use it. Didn’t dig it too much though. We’d be glad if you helped us get it working 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing React Components with React Test Renderer
createNodeMock function is called when Ref functions for DOMComponent is called. You can mock the Ref function through this option like this.
Read more >
API | React Native Testing Library - Open Source
This options allows you to pass createNodeMock option to ... is done automatically if your test runner supports afterEach hook (like Jest, mocha,...
Read more >
Test Renderer - React
createNodeMock accepts the current element and should return a mock ref object. This is useful when you test a component that relies on...
Read more >
How to use the react-test-renderer.act function in react ... - Snyk
To help you get started, we've selected a few react-test-renderer.act examples, based on popular ways it is used in public projects.
Read more >
Why is my renderer failing when using Material-UI using Jest ...
test('checked', () => { // Mock node - see https://reactjs.org/docs/test-renderer.html#ideas const createNodeMock = element => { return ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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