0
0
Cypresstesting~20 mins

Mounting React components in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Mounting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Mounting a Simple React Component
What will be the output in the Cypress test runner when this React component is mounted and checked for text content?
Cypress
import React from 'react';
import { mount } from 'cypress/react';

function Greeting() {
  return <h1>Hello, Cypress!</h1>;
}

describe('Greeting component', () => {
  it('renders correct text', () => {
    mount(<Greeting />);
    cy.contains('Hello, Cypress!').should('exist');
  });
});
ATest passes because the text 'Hello, Cypress!' is found in the mounted component.
BTest fails because the component does not render any text.
CTest fails with a syntax error due to missing import of React.
DTest fails because cy.contains cannot find the text 'Hello, World!'.
Attempts:
2 left
💡 Hint
Check what text the component renders and what text the test looks for.
locator
intermediate
2:00remaining
Best Locator for a Button in Mounted React Component
You have mounted a React component with a button: . Which Cypress selector is the best practice to locate this button for testing?
Acy.contains('Submit')
Bcy.get('button').eq(0)
Ccy.get('button').contains('Submit')
Dcy.get('button[aria-label="submit form"]')
Attempts:
2 left
💡 Hint
Use selectors that improve accessibility and are less likely to break.
assertion
advanced
2:00remaining
Correct Assertion for React Component State Change
After mounting a React component with a button that toggles text from 'Off' to 'On', which assertion correctly verifies the text changes after clicking?
Cypress
import React, { useState } from 'react';
import { mount } from 'cypress/react';

function Toggle() {
  const [on, setOn] = useState(false);
  return <button onClick={() => setOn(!on)}>{on ? 'On' : 'Off'}</button>;
}

describe('Toggle component', () => {
  it('toggles text on click', () => {
    mount(<Toggle />);
    cy.get('button').click();
    // Which assertion is correct here?
  });
});
Acy.get('button').should('have.text', 'On')
Bcy.get('button').should('contain', 'On')
Ccy.get('button').should('have.attr', 'text', 'On')
Dcy.get('button').should('have.value', 'On')
Attempts:
2 left
💡 Hint
Check the correct Cypress assertion for button text content.
🔧 Debug
advanced
2:00remaining
Debugging Mount Failure in Cypress React Test
You try to mount a React component in Cypress but get the error: "TypeError: mount is not a function". What is the most likely cause?
Cypress
import React from 'react';
import { mount } from 'cypress/react';

function Sample() {
  return <div>Sample</div>;
}

describe('Sample test', () => {
  it('mounts component', () => {
    mount(<Sample />);
  });
});
AThe import statement should be from 'react-dom' instead of 'cypress/react'.
BThe cypress/react package is not installed or not configured correctly.
CThe mount function is deprecated and should be replaced with render.
DThe React component is missing a return statement.
Attempts:
2 left
💡 Hint
Check if the testing environment has the right packages installed.
framework
expert
3:00remaining
Handling Asynchronous Effects in Mounted React Components
You mount a React component that fetches data asynchronously on mount and displays it. Which Cypress approach correctly waits for the data to appear before asserting?
Cypress
import React, { useEffect, useState } from 'react';
import { mount } from 'cypress/react';

function DataFetcher() {
  const [data, setData] = useState(null);
  useEffect(() => {
    setTimeout(() => setData('Loaded'), 1000);
  }, []);
  return <div>{data ?? 'Loading...'}</div>;
}

describe('DataFetcher component', () => {
  it('shows loaded data', () => {
    mount(<DataFetcher />);
    // Which option waits correctly for data?
  });
});
Acy.get('div').should('have.text', 'Loaded')
Bcy.wait(1000); cy.get('div').should('have.text', 'Loaded')
Ccy.contains('Loaded').should('exist')
Dcy.get('div').should('not.have.text', 'Loading...')
Attempts:
2 left
💡 Hint
Use Cypress commands that automatically retry until the condition is met.