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

Missing documentation for using the library with enzyme #87

Closed
dagoltz opened this issue Feb 2, 2020 · 2 comments
Closed

Missing documentation for using the library with enzyme #87

dagoltz opened this issue Feb 2, 2020 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@dagoltz
Copy link

dagoltz commented Feb 2, 2020

This article says:

Enforcing
If you are concerned about missing these console logs, you can also use this library in your tests. The library perfectly works with enzyme and react-test-renderer, and as mentioned above you can report to an object in your tests to ensure nothing was re-rendered.

But I can't find any documentation on how to set why-did-you-render during all of my tests.
Can you direct me to the relevant docs or add some?

@vzaidman
Copy link
Collaborator

vzaidman commented Feb 2, 2020

I forgot altogether I planned to write about this indeed.
I'm not sure when I'll get to it though.

It would look something like in the tests of the library itself:
https://github.com/welldone-software/why-did-you-render/blob/master/tests/hooks/useState.test.js

Instead of printing to the console, you can output redundant re-renders to an object and then make assertions about it:

    updateInfos = []
    whyDidYouRender(React, {
      notifier: updateInfo => updateInfos.push(updateInfo)
    })
test('setState of different values', () => {
    const ComponentWithHooks = () => {
      const [currentState, setCurrentState] = React.useState({b: 'b'})

      React.useLayoutEffect(() => {
        setCurrentState({b: 'c'})
      }, [])

      return (
        <div>hi! {currentState.b}</div>
      )
    }

    ComponentWithHooks.whyDidYouRender = true

    rtl.render(
      <ComponentWithHooks/>
    )

    expect(updateInfos).toHaveLength(1)
    expect(updateInfos[0].reason).toEqual({
      propsDifferences: false,
      stateDifferences: false,
      hookDifferences: [
        {
          pathString: '.b',
          diffType: diffTypes.different,
          prevValue: 'b',
          nextValue: 'c'
        },
        {
          pathString: '',
          diffType: diffTypes.different,
          prevValue: {b: 'b'},
          nextValue: {b: 'c'}
        }
      ]
    })
  })

@vzaidman vzaidman added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Feb 2, 2020
@vzaidman vzaidman reopened this Feb 22, 2020
@vzaidman
Copy link
Collaborator

vzaidman commented Feb 5, 2021

I think my example here is enough.
If anybody finds the need to write tests about re-renders, you are welcome to leave a comment here.

@vzaidman vzaidman closed this as completed Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants