react Project in typescript need testing. Add and configure a test runner: Jest or Vitest. Test runner should show the test coverage. You should aim to reach at least 80% of the test coverage.
Add a testing library: React Testing Library. You should add tests for the several scenarios keeping in mind that mocked data should be used instead of real API calls.
Tests for the Card List component:
Verify that the component renders the specified number of cards;
Check that an appropriate message is displayed if no cards are present.
Tests for the Card component:
Ensure that the card component renders the relevant card data;
Validate that clicking on a card opens a detailed card component;
Check that clicking triggers an additional API call to fetch detailed information.
Tests for the Detailed Card component:
Check that a loading indicator is displayed while fetching data;
Make sure the detailed card component correctly displays the detailed card data;
Ensure that clicking the close button hides the component.
Tests for the Pagination component:
Make sure the component updates URL query parameter when page changes.
Tests for the Search component:
Verify that clicking the Search button saves the entered value to the local storage;
Check that the component retrieves the value from the local storage upon mounting.
Lastly, update Husky to run tests on the pre-push hook, ensuring that tests are automatically executed before any code is pushed.
Assistant
Sure, here's a step-by-step guide to add and configure Jest along with React Testing Library for your React project in TypeScript and achieve at least 80% test coverage:
// DetailedCard.test.tsx
import React from 'react';
import { render, screen } from '@testing-library/react';
import { DetailedCard } from '../components/DetailedCard';
const mockCardDetail = { id: 1, title: 'Card 1', description: 'Detailed Description 1' };
test('displays a loading indicator while fetching data', () => {
render(<DetailedCard isLoading={true} />);
expect(screen.getByText(/loading.../i)).toBeInTheDocument();
});
test('correctly displays the detailed card data', () => {
render(<DetailedCard isLoading={false} cardDetail={mockCardDetail} />);
expect(screen.getByText(mockCardDetail.title)).toBeInTheDocument();
expect(screen.getByText(mockCardDetail.description)).toBeInTheDocument();
});
test('hides the component when clicking the close button', () => {
const mockCloseDetail = jest.fn();
render(<DetailedCard isLoading={false} cardDetail={mockCardDetail} onCloseDetail={mockCloseDetail} />);
fireEvent.click(screen.getByText(/close/i));
expect(mockCloseDetail).toHaveBeenCalled();
});
Pagination Component Tests
typescript
// Pagination.test.tsx
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { Pagination } from '../components/Pagination';
import { createMemoryHistory } from 'history';
import { Router } from 'react-router-dom';
const history = createMemoryHistory();
test('updates URL query parameter when page changes', () => {
render(
<Router history={history}>
<Pagination totalPages={5} currentPage={1} />
</Router>
);
fireEvent.click(screen.getByText('2'));
expect(history.location.search).toContain('page=2');
});
Search Component Tests
typescript
// Search.test.tsx
import React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import { Search } from '../components/Search';
test('saves entered value to the local storage when clicking the Search button', () => {
localStorage.setItem = jest.fn();
render(<Search />);
const input = screen.getByPlaceholderText('Search...');
const button = screen.getByText(/search/i);
fireEvent.change(input, { target: { value: 'test query' } });
fireEvent.click(button);
expect(localStorage.setItem).toHaveBeenCalledWith('searchQuery', 'test query');
});
test('retrieves value from local storage upon mounting', () => {
localStorage.getItem = jest.fn(() => 'stored query');
render(<Search />);
const input = screen.getByPlaceholderText('Search...');
expect(input.value).toBe('stored query');
});
Step 5: Configure Husky
Initialize Husky:
bash
npx husky-init && npm install
This will create a .husky directory in your project.
Create a pre-push hook:
bash
npx husky add .husky/pre-push "npm test"
After these configurations, your tests will run each time you push your code, ensuring that you maintain at least 80% coverage.
Final Thoughts
This guide helps you setup Jest, @testing-library/react and Husky for a React project in TypeScript, and gives you a basic structure for testing different components. Adjust the tests according to your actual component implementations and functionality to achieve the desired coverage goal.