Manipulating State of Stateful Component in Tests
Explore how to manage and manipulate the state of a stateful React component during testing with Enzyme. Understand how to pass slide data as props, update component state, handle edge cases with click handlers, and declare prop types for clean, testable components. This lesson guides you through writing thorough tests to ensure your Carousel component behaves as expected.
We'll cover the following...
We'll cover the following...
Manipulating state in tests
So far, the carousel is just a pair of buttons. What it really needs is some slides. A simple approach to this is to give the Carousel instance an array of data as a prop called slides, then pass the data from slides[slideIndex] as props to a CarouselSlide.
- Replace the existing
beforeEachblock to set theslidesprop on theCarouselinstance, then add a (failing) test for the slide-passing behavior:
...
- The new test uses a method called
props()instead ofprop(). This method returns an object containing all of the