Just different ways of doing it. So that was a walkthrough of a TDD example with component tests. Now we'll have a second example and some more points of component testing, especially about props, wrappers of components, and parent-child relationships.
So this example is from chapter five of the book, and here's the final code. You can use hard coding initially, or you can use console log to make your test pass. Just hard coding, everything's fine there. Then you manipulate your component either with props or wrappers.
So here we have a prop. Instead of a hard-coded test, we're adding that prop right here, the placeholder. And then we add it to the component. You can pass data with props. Like here, we have a prop for heroes, we're passing an empty array. And then here, we're passing a heroes array. On the right side, on the top, we're not passing anything, just the component by itself. But if you wanted to, you could have a wrapper there, for instance, Context API, and thereby provide a value there, the villains array. And that's how we manipulate our components. So either you use a property or wrapper. That's how you customize your component testing in Cypress and React and anywhere else. When you're adding props, you add the property, right here we're adding name property, and then you add the prop to the component types. You add it to the arguments of the component, then you use it in the component. Same flow, we'll be going through it. This time we're adding the value property, a test failing with that, right? And then add the prop, the argument, and use it in the test itself, which is in the component. And we have a nice render at some point, which we'll be using for a form. This form, we want it to have the ability to be a read only also. So we write a failing test for that, go ahead and add the prop. And if we have events, we can just start them. Here we have an event that's two keystrokes, so we want the change event to be called twice. First, the failing tests, and then we add the prop, the arguments, and again, we use it in the component. When I see failures through tests, and when you have green tests, you wanna prefer adding more tests, or refactoring versus adding additional source code. This is very important.
Comments