widgetFrame.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {WidgetFrame} from 'sentry/views/dashboards/widgets/common/widgetFrame';
  3. describe('WidgetFrame', () => {
  4. describe('Layout', () => {
  5. it('Renders the title and description', async () => {
  6. render(<WidgetFrame title="EPS" description="Number of events per second" />);
  7. expect(screen.getByText('EPS')).toBeInTheDocument();
  8. await userEvent.hover(screen.getByTestId('more-information'));
  9. expect(await screen.findByText('Number of events per second')).toBeInTheDocument();
  10. });
  11. });
  12. describe('Warnings', () => {
  13. it('Shows the warnings in a tooltip', async () => {
  14. render(<WidgetFrame title="count()" warnings={['This widget has stale data']} />);
  15. expect(screen.queryByText('This widget has stale data')).not.toBeInTheDocument();
  16. await userEvent.hover(screen.getByRole('button', {name: 'Widget warnings'}));
  17. expect(await screen.findByText('This widget has stale data')).toBeInTheDocument();
  18. });
  19. });
  20. describe('Badge', () => {
  21. it('Shows the badge', () => {
  22. const {rerender} = render(<WidgetFrame title="count()" />);
  23. expect(screen.queryByText('Sampled')).not.toBeInTheDocument();
  24. rerender(
  25. <WidgetFrame
  26. title="count()"
  27. badgeProps={{
  28. text: 'Sampled',
  29. }}
  30. />
  31. );
  32. expect(screen.getByText('Sampled')).toBeInTheDocument();
  33. });
  34. });
  35. describe('Action Menu', () => {
  36. it('Renders a single action as a button', async () => {
  37. const onAction = jest.fn();
  38. render(
  39. <WidgetFrame
  40. title="EPS"
  41. description="Number of events per second"
  42. actions={[
  43. {
  44. key: 'hello',
  45. label: 'Make Go',
  46. onAction,
  47. },
  48. ]}
  49. />
  50. );
  51. const $button = screen.getByRole('button', {name: 'Make Go'});
  52. expect($button).toBeInTheDocument();
  53. await userEvent.click($button);
  54. expect(onAction).toHaveBeenCalledTimes(1);
  55. });
  56. it('Renders multiple actions in a dropdown menu', async () => {
  57. const onAction1 = jest.fn();
  58. const onAction2 = jest.fn();
  59. render(
  60. <WidgetFrame
  61. title="EPS"
  62. description="Number of events per second"
  63. actions={[
  64. {
  65. key: 'one',
  66. label: 'One',
  67. onAction: onAction1,
  68. },
  69. {
  70. key: 'two',
  71. label: 'Two',
  72. onAction: onAction2,
  73. },
  74. ]}
  75. />
  76. );
  77. await userEvent.click(screen.getByRole('button', {name: 'Actions'}));
  78. await userEvent.click(screen.getByRole('menuitemradio', {name: 'One'}));
  79. expect(onAction1).toHaveBeenCalledTimes(1);
  80. await userEvent.click(screen.getByRole('button', {name: 'Actions'}));
  81. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Two'}));
  82. expect(onAction2).toHaveBeenCalledTimes(1);
  83. });
  84. });
  85. describe('Full Screen View Button', () => {
  86. it('Renders a full screen view button', async () => {
  87. const onFullScreenViewClick = jest.fn();
  88. const {rerender} = render(<WidgetFrame title="count()" />);
  89. expect(
  90. screen.queryByRole('button', {name: 'Open Full-Screen View'})
  91. ).not.toBeInTheDocument();
  92. rerender(
  93. <WidgetFrame title="count()" onFullScreenViewClick={onFullScreenViewClick} />
  94. );
  95. const $button = screen.getByRole('button', {name: 'Open Full-Screen View'});
  96. expect($button).toBeInTheDocument();
  97. await userEvent.click($button);
  98. expect(onFullScreenViewClick).toHaveBeenCalledTimes(1);
  99. });
  100. });
  101. });