button.test.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* eslint-disable no-undef */
  2. import React from 'react';
  3. import { render, cleanup, fireEvent } from '@testing-library/react';
  4. import '@testing-library/jest-dom';
  5. import Button from '../components/Button';
  6. import theme from '../helpers/theme';
  7. describe('Button component', () => {
  8. afterEach(cleanup);
  9. test('check button content', () => {
  10. const { getByText } = render(<Button>btn content</Button>);
  11. expect(getByText('btn content').textContent).toBe('btn content');
  12. });
  13. test('check button primary theme', () => {
  14. const { getByText } = render(
  15. <Button appearance="primary">btn content</Button>,
  16. );
  17. expect(getByText('btn content')).toHaveStyle(`
  18. background-color: ${theme.colors.primary};
  19. border-color: ${theme.colors.primary};
  20. color: white;
  21. `);
  22. });
  23. test('check click event', () => {
  24. let counter = 0;
  25. const handleClick = (): void => {
  26. counter += 1;
  27. };
  28. const { getByText } = render(
  29. <Button onClick={handleClick}>btn content</Button>,
  30. );
  31. fireEvent.mouseDown(getByText('btn content'));
  32. expect(counter).toBe(1);
  33. });
  34. test('check disable status', () => {
  35. const { getByText } = render(<Button isDisabled>btn content</Button>);
  36. expect(getByText('btn content')).toBeDisabled();
  37. });
  38. });