Re-render components under test #15717
Unanswered
NoriSte
asked this question in
Component Testing
Replies: 1 comment
-
Hey. Sorry for the slow reply. We now have a Here's a snippet: const Component = ({ input }) => {
useLayoutEffect(() => {
return layoutEffectCleanup
}, [input])
useEffect(() => {
return effectCleanup
}, [])
return <div>{input}</div>
}
mount(<Component input="0" />).then(({ rerender }) => {
expect(layoutEffectCleanup).to.have.been.callCount(0)
expect(effectCleanup).to.have.been.callCount(0)
rerender(<Component input="0" />).then(() => {
expect(layoutEffectCleanup).to.have.been.callCount(0)
expect(effectCleanup).to.have.been.callCount(0)
})
rerender(<Component input="1" />).then(() => {
expect(layoutEffectCleanup).to.have.been.callCount(1)
expect(effectCleanup).to.have.been.callCount(0)
})
})
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The discussion started on Twitter.
I was wondering about how to re-render components under tests. Currently, Cypress allows doing that through a mini-app, see this example coming directly from the react package.
In the above test, we're testing the
MyInput
but, in order to do so, we need to wrap it with theApp
component.My original question was: why are Cypress's component test APIs forcing the developer to write a stateful mini-app to do so? Would be possible to have a
rerender
-like API like the one exposed by React Testing Library?In the Twitter thread, @JessicaSachs pointed to the rationale behind this decision and I got it! My original question comes from the fact that we have a
mountHook
API that allows the developer to consume the hook "outside" a real component-like environment (Ideally I should create and mount a component in order to test a hook).I think we can live without a rerender-like API but I'm glad to discuss the topic 😊
Thanks everyone
p.s. let me know if you prefer to move the discussion to an issue
Beta Was this translation helpful? Give feedback.
All reactions