Testing React Applications
Write solid unit and integration tests using Jest and React Testing Library. Mock API calls and user interactions.
Downloaded from PrepIQ (https://prepiq.online)
Q1. What are testing libraries for React?
Jest, React Testing Library, Enzyme, Cypress, Playwright.
Q2. How do you write unit tests for React components?
import { render, screen } from '@testing-library/react';
test('renders welcome message', () => {
render(<Welcome />);
const element = screen.getByText(/welcome/i);
expect(element).toBeInTheDocument();
});
Q3. How do you test async operations?
test('fetches data', async () => {
render(<DataComponent />);
const element = await screen.findByText(/data/i);
expect(element).toBeInTheDocument();
});
Q4. How do you test Redux?
Test reducers, actions, selectors separately. Use mock store for integration tests.
Q5. What is mocking and stubbing?
Mocking - replace real functions with fake ones. Stubbing - provide fake implementations.
Q6. How do you test hooks?
import { renderHook, act } from '@testing-library/react';
import { useCounter } from './useCounter';
test('counter increments', () => {
const { result } = renderHook(() => useCounter());
act(() => { result.current.increment(); });
expect(result.current.count).toBe(1);
});
Q7. How do you write integration tests?
Test multiple components together. Test user flows and interactions.
Testing React Applications
Write solid unit and integration tests using Jest and React Testing Library. Mock API calls and user interactions.
What are testing libraries for React?
Jest, React Testing Library, Enzyme, Cypress, Playwright.
How do you write unit tests for React components?
import { render, screen } from '@testing-library/react'; test('renders welcome message', () => { render(<We...
How do you test async operations?
test('fetches data', async () => { render(<DataComponent />); const element = await screen.findByText(/dat...
How do you test Redux?
Test reducers, actions, selectors separately. Use mock store for integration tests.
What is mocking and stubbing?
Mocking - replace real functions with fake ones. Stubbing - provide fake implementations.
How do you test hooks?
import { renderHook, act } from '@testing-library/react'; import { useCounter } from './useCounter'; test('co...
How do you write integration tests?
Test multiple components together. Test user flows and interactions.