button.spec.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import {fireEvent, mountWithTheme} from 'sentry-test/reactTestingLibrary';
  2. import Button from 'app/components/button';
  3. describe('Button', function () {
  4. // @ts-expect-error
  5. const routerContext = TestStubs.routerContext();
  6. it('renders', function () {
  7. const {container} = mountWithTheme(<Button priority="primary">Button</Button>);
  8. expect(container).toSnapshot();
  9. });
  10. it('renders react-router link', function () {
  11. const {container} = mountWithTheme(<Button to="/some/route">Router Link</Button>, {
  12. context: routerContext,
  13. });
  14. expect(container).toSnapshot();
  15. });
  16. it('renders normal link', function () {
  17. const {container} = mountWithTheme(
  18. <Button href="/some/relative/url">Normal Link</Button>,
  19. {context: routerContext}
  20. );
  21. expect(container).toSnapshot();
  22. });
  23. it('renders disabled normal link', function () {
  24. const {container} = mountWithTheme(
  25. <Button href="/some/relative/url">Normal Link</Button>,
  26. {context: routerContext}
  27. );
  28. expect(container).toSnapshot();
  29. });
  30. it('calls `onClick` callback', function () {
  31. const spy = jest.fn();
  32. const {getByText} = mountWithTheme(<Button onClick={spy}>Click me</Button>, {
  33. context: routerContext,
  34. });
  35. fireEvent.click(getByText('Click me'));
  36. expect(spy).toHaveBeenCalled();
  37. });
  38. it('does not call `onClick` on disabled buttons', function () {
  39. const spy = jest.fn();
  40. const {getByText} = mountWithTheme(
  41. <Button onClick={spy} disabled>
  42. Click me
  43. </Button>,
  44. {context: routerContext}
  45. );
  46. fireEvent.click(getByText('Click me'));
  47. expect(spy).not.toHaveBeenCalled();
  48. });
  49. });