button.test.tsx 1.3 KB

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