SecondaryActions.test.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { render, screen } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import { noop } from 'lodash';
  4. import React from 'react';
  5. import { SecondaryActions } from './SecondaryActions';
  6. describe('SecondaryActions', () => {
  7. it('should render component with three buttons', () => {
  8. render(
  9. <SecondaryActions
  10. onClickAddQueryRowButton={noop}
  11. onClickRichHistoryButton={noop}
  12. onClickQueryInspectorButton={noop}
  13. />
  14. );
  15. expect(screen.getByRole('button', { name: /Add row button/i })).toBeInTheDocument();
  16. expect(screen.getByRole('button', { name: /Rich history button/i })).toBeInTheDocument();
  17. expect(screen.getByRole('button', { name: /Query inspector button/i })).toBeInTheDocument();
  18. });
  19. it('should not render hidden elements', () => {
  20. render(
  21. <SecondaryActions
  22. addQueryRowButtonHidden={true}
  23. richHistoryRowButtonHidden={true}
  24. onClickAddQueryRowButton={noop}
  25. onClickRichHistoryButton={noop}
  26. onClickQueryInspectorButton={noop}
  27. />
  28. );
  29. expect(screen.queryByRole('button', { name: /Add row button/i })).not.toBeInTheDocument();
  30. expect(screen.queryByRole('button', { name: /Rich history button/i })).not.toBeInTheDocument();
  31. expect(screen.getByRole('button', { name: /Query inspector button/i })).toBeInTheDocument();
  32. });
  33. it('should disable add row button if addQueryRowButtonDisabled=true', () => {
  34. render(
  35. <SecondaryActions
  36. addQueryRowButtonDisabled={true}
  37. onClickAddQueryRowButton={noop}
  38. onClickRichHistoryButton={noop}
  39. onClickQueryInspectorButton={noop}
  40. />
  41. );
  42. expect(screen.getByRole('button', { name: /Add row button/i })).toBeDisabled();
  43. expect(screen.getByRole('button', { name: /Rich history button/i })).toBeInTheDocument();
  44. expect(screen.getByRole('button', { name: /Query inspector button/i })).toBeInTheDocument();
  45. });
  46. it('should map click handlers correctly', async () => {
  47. const user = userEvent.setup();
  48. const onClickAddRow = jest.fn();
  49. const onClickHistory = jest.fn();
  50. const onClickQueryInspector = jest.fn();
  51. render(
  52. <SecondaryActions
  53. onClickAddQueryRowButton={onClickAddRow}
  54. onClickRichHistoryButton={onClickHistory}
  55. onClickQueryInspectorButton={onClickQueryInspector}
  56. />
  57. );
  58. await user.click(screen.getByRole('button', { name: /Add row button/i }));
  59. expect(onClickAddRow).toBeCalledTimes(1);
  60. await user.click(screen.getByRole('button', { name: /Rich history button/i }));
  61. expect(onClickHistory).toBeCalledTimes(1);
  62. await user.click(screen.getByRole('button', { name: /Query inspector button/i }));
  63. expect(onClickQueryInspector).toBeCalledTimes(1);
  64. });
  65. });