← All tasksReact · Junior

Counter

Write a Counter component. It shows a number in an element with data-testid="count" and has two buttons: - decreases the number by one and + increases it.

The initial value comes in the initial prop (0 by default). Hooks (useState and others) are available without imports.

Tests (4)

  1. render(<Counter />);
    expect(screen.getByTestId('count').textContent).toBe('0');
  2. render(<Counter />);
    fireEvent.click(screen.getByRole('button', { name: '+' }));
    fireEvent.click(screen.getByRole('button', { name: '+' }));
    expect(screen.getByTestId('count').textContent).toBe('2');
  3. render(<Counter />);
    fireEvent.click(screen.getByRole('button', { name: '-' }));
    expect(screen.getByTestId('count').textContent).toBe('-1');
  4. render(<Counter initial={10} />);
    fireEvent.click(screen.getByRole('button', { name: '+' }));
    expect(screen.getByTestId('count').textContent).toBe('11');