So I’ve read posts and watched tutorials on using Enzyme to test that a nested component is rendered with the correct props. I’ve also read the seemingly contradictory recommendation in the official react testing docs guidelines against testing component instances. This leaves me confused as to whether or not it is helpful to test whether nested components are rendered with the correct props.
As an example, here is a component with a nested component where the child component takes various prop values depending on the parent's props. Would it make sense here to test the props of the nested Decision components?
/* Parent Component */
import Decision from "./Decision";
interface BookProps {
age: number;
boring: boolean;
chapters: number[];
}
const Book = ({ age, boring, chapters }: BookProps) => {
if (age < 10) {
return (<Decision
bookDescription={chapters.length > 0 ? 'a chapter book' : 'prose'}
shouldAssignToStudents={boring && chapters.length > 10}
/>)
} else {
return (<Decision
bookDescription={'old'}
shouldAssignToStudents={false}
/>)
}
}
export default Book;
/* Child Component */
interface DecisionProps {
bookDescription: string;
shouldAssignToStudents: boolean
}
const Decision = ({ bookDescription, shouldAssignToStudents }: DecisionProps) => {
return (<div>
{`This book is ${bookDescription} and the
students ${shouldAssignToStudents && 'do not '} need to read it`}
</div>);
}
export default Decision;