AddRemove.test.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { AddRemove } from './AddRemove';
  4. const noop = () => {};
  5. const TestComponent = ({ items }: { items: any[] }) => (
  6. <>
  7. {items.map((_, index) => (
  8. <AddRemove key={index} elements={items} index={index} onAdd={noop} onRemove={noop} />
  9. ))}
  10. </>
  11. );
  12. describe('AddRemove Button', () => {
  13. describe("When There's only one element in the list", () => {
  14. it('Should only show the add button', () => {
  15. render(<TestComponent items={['something']} />);
  16. expect(screen.getByText('add')).toBeInTheDocument();
  17. expect(screen.queryByText('remove')).not.toBeInTheDocument();
  18. });
  19. });
  20. describe("When There's more than one element in the list", () => {
  21. it('Should show the remove button on every element', () => {
  22. const items = ['something', 'something else'];
  23. render(<TestComponent items={items} />);
  24. expect(screen.getAllByText('remove')).toHaveLength(items.length);
  25. });
  26. it('Should show the add button only once', () => {
  27. const items = ['something', 'something else'];
  28. render(<TestComponent items={items} />);
  29. expect(screen.getAllByText('add')).toHaveLength(1);
  30. });
  31. });
  32. });