0
0
Cypresstesting~10 mins

Why component testing isolates UI units in Cypress - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test checks that a UI component renders correctly by itself without depending on other parts of the app. It verifies that the component shows the expected text and reacts to a button click.

Test Code - Cypress
Cypress
import { mount } from 'cypress/react';
import React, { useState } from 'react';

function Greeting() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <h1>Hello, User!</h1>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)} aria-label="increment-button">Click me</button>
    </div>
  );
}

describe('Greeting component test', () => {
  it('renders and updates count on button click', () => {
    mount(<Greeting />);
    cy.get('h1').should('contain.text', 'Hello, User!');
    cy.get('p').should('contain.text', 'You clicked 0 times');
    cy.get('button[aria-label="increment-button"]').click();
    cy.get('p').should('contain.text', 'You clicked 1 times');
  });
});
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsCypress test runner is ready-PASS
2Mount the Greeting component in isolationGreeting component rendered with heading, paragraph, and button-PASS
3Find the <h1> element and check its textHeading element visible with text 'Hello, User!'Verify heading text is 'Hello, User!'PASS
4Find the <p> element and check initial count textParagraph visible with text 'You clicked 0 times'Verify paragraph text shows count 0PASS
5Find the button by aria-label and click itButton visible and clickable-PASS
6Check that paragraph text updates to count 1Paragraph text updated to 'You clicked 1 times'Verify paragraph text shows count 1 after clickPASS
Failure Scenario
Failing Condition: Component fails to render or button click does not update count
Execution Trace Quiz - 3 Questions
Test your understanding
Why does this test mount the Greeting component alone?
ATo check backend API responses
BTo test the entire app's UI at once
CTo isolate and test the component without other app parts
DTo test database connections
Key Result
Component testing isolates UI units so you can check their behavior alone, making bugs easier to find and fixing faster.