You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.2 KiB

  1. import { render, fireEvent, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import renderer from 'react-test-renderer';
  4. import Button from '../button';
  5. describe('<Button />', () => {
  6. it('renders a button element', () => {
  7. const component = renderer.create(<Button />);
  8. const tree = component.toJSON();
  9. expect(tree).toMatchSnapshot();
  10. });
  11. it('renders the given text', () => {
  12. const text = 'foo';
  13. const component = renderer.create(<Button text={text} />);
  14. const tree = component.toJSON();
  15. expect(tree).toMatchSnapshot();
  16. });
  17. it('handles click events using the given handler', () => {
  18. const handler = jest.fn();
  19. render(<Button onClick={handler}>button</Button>);
  20. fireEvent.click(screen.getByText('button'));
  21. expect(handler.mock.calls.length).toEqual(1);
  22. });
  23. it('does not handle click events if props.disabled given', () => {
  24. const handler = jest.fn();
  25. render(<Button onClick={handler} disabled>button</Button>);
  26. fireEvent.click(screen.getByText('button'));
  27. expect(handler.mock.calls.length).toEqual(0);
  28. });
  29. it('renders a disabled attribute if props.disabled given', () => {
  30. const component = renderer.create(<Button disabled />);
  31. const tree = component.toJSON();
  32. expect(tree).toMatchSnapshot();
  33. });
  34. it('renders the children', () => {
  35. const children = <p>children</p>;
  36. const component = renderer.create(<Button>{children}</Button>);
  37. const tree = component.toJSON();
  38. expect(tree).toMatchSnapshot();
  39. });
  40. it('renders the props.text instead of children', () => {
  41. const text = 'foo';
  42. const children = <p>children</p>;
  43. const component = renderer.create(<Button text={text}>{children}</Button>);
  44. const tree = component.toJSON();
  45. expect(tree).toMatchSnapshot();
  46. });
  47. it('renders class="button--block" if props.block given', () => {
  48. const component = renderer.create(<Button block />);
  49. const tree = component.toJSON();
  50. expect(tree).toMatchSnapshot();
  51. });
  52. it('adds class "button-secondary" if props.secondary given', () => {
  53. const component = renderer.create(<Button secondary />);
  54. const tree = component.toJSON();
  55. expect(tree).toMatchSnapshot();
  56. });
  57. });