CollapseToggle.test.tsx 1.2 KB

123456789101112131415161718192021222324252627282930
  1. import { render, screen } from '@testing-library/react';
  2. import { noop } from 'lodash';
  3. import React from 'react';
  4. import { CollapseToggle } from './CollapseToggle';
  5. describe('TestToggle', () => {
  6. it('should render text', () => {
  7. render(<CollapseToggle isCollapsed={true} text="Hello, world" onToggle={noop} />);
  8. expect(screen.getByRole('button')).toHaveTextContent('Hello, world');
  9. });
  10. it('should respect isCollapsed', () => {
  11. const { rerender } = render(<CollapseToggle isCollapsed={false} text="Hello, world" onToggle={noop} />);
  12. expect(screen.getByRole('button', { expanded: true })).toBeInTheDocument();
  13. rerender(<CollapseToggle isCollapsed={true} text="Hello, world" onToggle={noop} />);
  14. expect(screen.getByRole('button', { expanded: false })).toBeInTheDocument();
  15. });
  16. it('should call onToggle', () => {
  17. const onToggle = jest.fn();
  18. render(<CollapseToggle isCollapsed={true} text="Hello, world" onToggle={onToggle} />);
  19. screen.getByRole('button').click();
  20. expect(onToggle).toHaveBeenCalledWith(false);
  21. // it should also not have any impact on the actual expanded state since the component does not track its own state
  22. expect(screen.getByRole('button', { expanded: false })).toBeInTheDocument();
  23. });
  24. });