enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.96k stars 2.01k forks source link

Component is not re-rendered with updated states #2557

Closed juliGer closed 2 years ago

juliGer commented 2 years ago

Hello! I wanted to see if someone can give me a hand regarding a question I have that I do not know if it is possible to do, and if it is I would like to know what I'm doing wrong so that it does not work.

I have a component that reads a state and that shows it on the screen

I see the updated states if I print .instance().state but I would need to render it again with the updated state, I tried .update() as shown in the image but I would not be getting the result I want

Is it possible to do this with enzyme ? Thanks and greetings!

Component

Screenshot_20220407_121516

Test

Screenshot_20220407_121727

Result Test (Bad)

Screenshot_20220407_121655

API

Version

library version
enzyme 3.11.0
react 16.8.6
react-dom 16.8.6
react-test-renderer
adapter (below)

Adapter

ljharb commented 2 years ago

Could you make a codesandbox, with relevant component and test code, that reproduces the error?

juliGer commented 2 years ago

Could you make a codesandbox, with relevant component and test code, that reproduces the error?

There I tried a smaller code in codesandbox and it worked so it must be something of mine, I am going to review the issue is that I cannot put the code because it is private but I consider the issue closed Thank you very much for answering, greetings!

juliGer commented 2 years ago

Hello! I have been able to replicate the error, the problem is that when I want to use mount() with a library called notistack I get this error

Test bad

https://codesandbox.io/s/shallow-test-app-forked-s7bj8o?file=/src/App.test.js

image

If you run the tests you can see how it only prints once cant with value 0 and the expected behavior is that it prints

cant: 0 cant: 1

That is, if I don't use notistack, the code performs the expected behavior. Hopefully there is a solution. Thanks and greetings again!

image

ljharb commented 2 years ago

The provider should be passed in with the wrappingComponent option, not directly wrapping the jsx of the thing you want to test.

juliGer commented 2 years ago

The provider should be passed in with the wrappingComponent option, not directly wrapping the jsx of the thing you want to test.

Oh, this worked, thank you so much. You're a genius!