Why do we isolate UI components during component testing?
Think about testing one piece at a time without outside influence.
Isolating UI units lets us check their behavior clearly without interference from other parts. This helps find bugs faster and makes tests simpler.
What will be the test result when running this Cypress component test?
import { mount } from 'cypress/react'; function Button({label}) { return <button>{label}</button>; } describe('Button component', () => { it('shows correct label', () => { mount(<Button label="Click me" />); cy.get('button').should('have.text', 'Click me'); }); });
Check if the component renders the label prop correctly.
The test mounts the Button component with label 'Click me' and asserts the button text matches. This passes because the component renders correctly.
Which assertion correctly verifies that a component renders a disabled button?
mount(<button disabled>Submit</button>);
Check the button's disabled state, not its text.
The correct way to check if a button is disabled is using should('be.disabled'). Checking text or existence is incorrect here.
This Cypress component test fails. What is the cause?
import { mount } from 'cypress/react'; function Input({placeholder}) { return <input placeholder={placeholder} />; } describe('Input component', () => { it('renders placeholder text', () => { mount(<Input placeholder="Enter name" />); cy.get('input').should('have.attr', 'placeholder', 'Enter your name'); }); });
Compare the placeholder prop value and the assertion value carefully.
The test fails because the placeholder prop is 'Enter name' but the assertion expects 'Enter your name'. This mismatch causes the failure.
Which practice best ensures UI units are isolated during Cypress component testing?
Think about controlling what the component depends on during tests.
Mocking external dependencies and avoiding rendering parent components keeps tests focused on the unit. This isolation helps find bugs in the component itself.