{
  "doc": {
    "id": "app/component-testing/react/examples",
    "title": "React Examples",
    "description": "Learn how to mount a React component, pass data to a React component, test event handlers, and customize `cy.mount()` for React Router and Redux.",
    "section": "app",
    "source_path": "/llm/markdown/app/component-testing/react/examples.md",
    "version": "ce02913654e2655ee63448bdc92bb92c7b46a619",
    "updated_at": "2026-04-22T19:37:51.587Z",
    "headings": [
      {
        "id": "app/component-testing/react/examples#react-examples",
        "text": "React Examples",
        "level": 1
      },
      {
        "id": "app/component-testing/react/examples#what-youll-learn",
        "text": " What you'll learn",
        "level": 5
      },
      {
        "id": "app/component-testing/react/examples#mounting-components",
        "text": "Mounting Components",
        "level": 2
      },
      {
        "id": "app/component-testing/react/examples#mounting-a-component",
        "text": "Mounting a Component",
        "level": 3
      },
      {
        "id": "app/component-testing/react/examples#passing-data-to-a-component",
        "text": "Passing Data to a Component",
        "level": 3
      },
      {
        "id": "app/component-testing/react/examples#testing-event-handlers",
        "text": "Testing Event Handlers",
        "level": 3
      },
      {
        "id": "app/component-testing/react/examples#custom-mount-commands",
        "text": "Custom Mount Commands",
        "level": 2
      },
      {
        "id": "app/component-testing/react/examples#customizing-cy-mount",
        "text": "Customizing cy.mount()",
        "level": 3
      },
      {
        "id": "app/component-testing/react/examples#react-router",
        "text": "React Router",
        "level": 3
      },
      {
        "id": "app/component-testing/react/examples#redux",
        "text": "Redux",
        "level": 3
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "React Examples"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 5,
        "children": [
          {
            "type": "text",
            "value": " What you'll learn"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "How to mount a React component"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "How to pass data to a React component"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "How to test event handlers"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "How to customize `cy.mount()` for React Router and Redux"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Mounting Components"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Mounting a Component"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The first step in testing a component is to mount it. This renders the component\ninto a testbed and enable's the use of the Cypress API to select elements,\ninteract with them, and run assertions."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To mount a React component, import the component into your spec and pass the\ncomponent to the `cy.mount` command:"
          }
        ]
      },
      {
        "type": "code",
        "lang": "js",
        "meta": null,
        "value": "import { Stepper } from './stepper'\n\nit('mounts', () => {\n  cy.mount(<Stepper />)\n  //Stepper should have initial count of 0 (default)\n  cy.get('[data-cy=counter]').should('have.text', '0')\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Passing Data to a Component"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "You can pass props to a component by setting them on the JSX passed into\n`cy.mount()`:"
          }
        ]
      },
      {
        "type": "code",
        "lang": "ts",
        "meta": null,
        "value": "it('mounts', () => {\n  cy.mount(<Stepper initial={100} />)\n  //Stepper should have initial count of 100\n  cy.get('[data-cy=counter]').should('have.text', '100')\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Testing Event Handlers"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pass a Cypress "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/guides/stubs-spies-and-clocks.md#Spies",
            "children": [
              {
                "type": "text",
                "value": "spy"
              }
            ]
          },
          {
            "type": "text",
            "value": " to an event\nprop and validate it was called:"
          }
        ]
      },
      {
        "type": "code",
        "lang": "ts",
        "meta": null,
        "value": "it('clicking + fires a change event with the incremented value', () => {\n  const onChangeSpy = cy.spy().as('onChangeSpy')\n  cy.mount(<Stepper onChange={onChangeSpy} />)\n  cy.get('[data-cy=increment]').click()\n  cy.get('@onChangeSpy').should('have.been.calledWith', 1)\n})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Custom Mount Commands"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Customizing cy.mount()"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "By default, `cy.mount()` is a simple passthrough to `mount()`, however, you can\ncustomize `cy.mount()` to fit your needs. For instance, if you are using\nproviders or other global app-level setups in your React app, you can configure\nthem here."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Below are a few examples that demonstrate using a custom mount command. These\nexamples can be adjusted for most other providers that you will need to support."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "React Router"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "If you have a component that consumes a hook or component from\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "https://reactrouter.com/",
            "children": [
              {
                "type": "text",
                "value": "React Router"
              }
            ]
          },
          {
            "type": "text",
            "value": ", make sure the component has access to\na React Router provider. Below is a sample mount command that uses\n`MemoryRouter` to wrap the component."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To set up certain scenarios, pass in props that will get passed to\n`MemoryRouter` in the options. Below is an example test that ensures an active\nlink has the correct class applied to it by initializing the router with\n`initialEntries` pointed to a particular route:"
          }
        ]
      },
      {
        "type": "code",
        "lang": "jsx",
        "meta": null,
        "value": "import { Navigation } from './Navigation'\n\nit('home link should be active when url is \"/\"', () => {\n  // No need to pass in custom initialEntries as default url is '/'\n  cy.mount(<Navigation />)\n\n  cy.get('a').contains('Home').should('have.class', 'active')\n})\n\nit('login link should be active when url is \"/login\"', () => {\n  cy.mount(<Navigation />, {\n    routerProps: {\n      initialEntries: ['/login'],\n    },\n  })\n\n  cy.get('a').contains('Login').should('have.class', 'active')\n})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Redux"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "To use a component that consumes state or actions from a\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "https://react-redux.js.org/",
            "children": [
              {
                "type": "text",
                "value": "Redux"
              }
            ]
          },
          {
            "type": "text",
            "value": " store, create a `mount` command that will\nwrap your component in a Redux Provider:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The options param can have a store that is already initialized with data:"
          }
        ]
      },
      {
        "type": "code",
        "lang": "jsx",
        "meta": null,
        "value": "import { getStore } from '../redux/store'\nimport { setUser } from '../redux/userSlice'\nimport { UserProfile } from './UserProfile'\n\nit('User profile should display user name', () => {\n  const user = { name: 'test person' }\n\n  // getStore is a factory method that creates a new store\n  const store = getStore()\n\n  // setUser is an action exported from the user slice\n  store.dispatch(setUser(user))\n\n  cy.mount(<UserProfile />, { reduxStore: store })\n\n  cy.get('div.name').should('have.text', user.name)\n})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The `getStore` method is a factory method that initializes a new Redux store. It\nis important that the store be initialized with each new test to ensure changes\nto the store don't affect other tests."
          }
        ]
      }
    ]
  },
  "token_estimate": 771
}