Closed gCardinal closed 7 years ago
If you want to test the whole component tree, you need mount
.
I realize that, sorry if my question made it sound otherwise.
However, as my example test shows, I can inspect the shallow rendered component and check that the prop I want is set to the value I'm expecting. Since the information is there, within the ShallowWrapper
, is there no way to just test "Hey, is there a CreditBalance component in there somewhere, anywhere?"
It's fine if there is no way to do it, but I prefer to make sure before I do the switch since I much prefer shallow rendering to mounting components to a DOM.
No, there's no way to do it - the point of shallow
is that the implementation details of the subtree do not belong in your tests.
I recommend doing it with shallow
, and having your MobileHeader
tests simply assert that it renders an AppBar
, with a iconElementRight
prop of a CreditBalance
. Then, your shallow unit tests for AppBar
can assert that the iconElementRight
gets rendered - and your CreditBalance
tests can assert that it is rendered properly.
Alright. Guess I still need to do some figuring out when it comes to testing React components!
Thank you!
Details about my setup: I'm using Material-UI, so normally whenever a component uses one of this library's component, I should wrap it in a
ThemeProvider
. I'm then using shallow rendering in my tests to skip that and simply test that my components display whatever I'm expecting them to display.That being said, I'm hitting a wall right now and I'm unsure if it's me not getting how shallow rendering works or just something that's not possible to do due to either how React or Enzyme work.
I have this component from Material-UI that I want to "bake in" some values and let my app customize other.
Let's say I want to test it displays credits properly, in my tests, the only thing I really care about is 1- Is there a
CreditBalance
component and 2- does it display 100?Normally, when components are simply a series of children composed together, this is simple with
find()
andprops()
, but since theCreditBalance
component is withinAppBar
's props, I can't a test working easily.find()
won't find itI did manage to test it, but it's almost reverse implementation and very brittle:
I'd love if I could simple do something like
expect(mobileHeader.contains('CreditBalance')).to.be.true
or something similar. Anything more precise than this and I feel like I'm throwing BDD out the window.Is what I'm trying to do simply not possible?