Complete the code to render the Remix component for testing.
import { render } from '@testing-library/react'; import MyComponent from '~/components/MyComponent'; test('renders MyComponent', () => { const { getByText } = render([1]); expect(getByText('Hello')).toBeInTheDocument(); });
To render a React component in Testing Library, you use JSX syntax like <MyComponent />.
Complete the code to simulate a user clicking a button in the test.
import { render, fireEvent } from '@testing-library/react'; import Button from '~/components/Button'; test('button click updates text', () => { const { getByRole } = render(<Button />); const button = getByRole('button'); fireEvent.[1](button); expect(button.textContent).toBe('Clicked'); });
The fireEvent.click method simulates a user clicking on an element.
Fix the error in the test by completing the code to wait for an async element.
import { render, screen, waitFor } from '@testing-library/react'; import AsyncComponent from '~/components/AsyncComponent'; test('loads and displays data', async () => { render(<AsyncComponent />); await waitFor(() => [1]('Data loaded')); expect(screen.getByText('Data loaded')).toBeInTheDocument(); });
queryByText which returns null and does not throw.findByText inside waitFor which is redundant.waitFor expects a function that throws an error until the condition is met. Using getByText inside waitFor works because it throws if the text is not found yet.
Fill both blanks to create a test that checks if an input changes value on user typing.
import { render, screen, fireEvent } from '@testing-library/react'; import InputComponent from '~/components/InputComponent'; test('input value changes on typing', () => { render(<InputComponent />); const input = screen.[1]('textbox'); fireEvent.[2](input, { target: { value: 'Hello' } }); expect(input.value).toBe('Hello'); });
Use getByRole('textbox') to find the input element by its role. Then use fireEvent.change to simulate typing into the input.
Fill all three blanks to write a test that submits a form and checks for success message.
import { render, screen, fireEvent } from '@testing-library/react'; import FormComponent from '~/components/FormComponent'; test('form submission shows success', () => { render(<FormComponent />); const input = screen.[1]('username'); fireEvent.[2](input, { target: { value: 'user123' } }); const button = screen.[3]('button'); fireEvent.click(button); expect(screen.getByText('Success')).toBeInTheDocument(); });
Use getByLabelText('username') to find the input by its label. Use fireEvent.change to type the username. Use getByRole('button') to find the submit button by its role.