widgetFrame.spec.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.getByRole('button', {name: 'Widget description'}));
  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 a single 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. it('Shows multiple badges', () => {
  35. const {rerender} = render(<WidgetFrame title="count()" />);
  36. expect(screen.queryByText('Sampled')).not.toBeInTheDocument();
  37. rerender(
  38. <WidgetFrame
  39. title="count()"
  40. badgeProps={[
  41. {
  42. text: 'Sampled',
  43. },
  44. {
  45. text: 'Extracted',
  46. },
  47. ]}
  48. />
  49. );
  50. expect(screen.getByText('Sampled')).toBeInTheDocument();
  51. expect(screen.getByText('Extracted')).toBeInTheDocument();
  52. });
  53. });
  54. describe('Action Menu', () => {
  55. it('Renders a single action as a button', async () => {
  56. const onAction = jest.fn();
  57. render(
  58. <WidgetFrame
  59. title="EPS"
  60. description="Number of events per second"
  61. actions={[
  62. {
  63. key: 'hello',
  64. label: 'Make Go',
  65. onAction,
  66. },
  67. ]}
  68. />
  69. );
  70. const $button = screen.getByRole('button', {name: 'Make Go'});
  71. expect($button).toBeInTheDocument();
  72. await userEvent.click($button);
  73. expect(onAction).toHaveBeenCalledTimes(1);
  74. });
  75. it('Allows disabling a single action', async () => {
  76. const onAction = jest.fn();
  77. render(
  78. <WidgetFrame
  79. title="EPS"
  80. description="Number of events per second"
  81. actionsDisabled
  82. actionsMessage="Actions are not supported"
  83. actions={[
  84. {
  85. key: 'hello',
  86. label: 'Make Go',
  87. onAction,
  88. },
  89. ]}
  90. />
  91. );
  92. const $button = screen.getByRole('button', {name: 'Make Go'});
  93. expect($button).toBeInTheDocument();
  94. expect($button).toBeDisabled();
  95. await userEvent.click($button);
  96. expect(onAction).not.toHaveBeenCalled();
  97. await userEvent.hover($button);
  98. expect(await screen.findByText('Actions are not supported')).toBeInTheDocument();
  99. });
  100. it('Renders multiple actions in a dropdown menu', async () => {
  101. const onAction1 = jest.fn();
  102. const onAction2 = jest.fn();
  103. render(
  104. <WidgetFrame
  105. title="EPS"
  106. description="Number of events per second"
  107. actions={[
  108. {
  109. key: 'one',
  110. label: 'One',
  111. onAction: onAction1,
  112. },
  113. {
  114. key: 'two',
  115. label: 'Two',
  116. onAction: onAction2,
  117. },
  118. ]}
  119. />
  120. );
  121. await userEvent.click(screen.getByRole('button', {name: 'Widget actions'}));
  122. await userEvent.click(screen.getByRole('menuitemradio', {name: 'One'}));
  123. expect(onAction1).toHaveBeenCalledTimes(1);
  124. await userEvent.click(screen.getByRole('button', {name: 'Widget actions'}));
  125. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Two'}));
  126. expect(onAction2).toHaveBeenCalledTimes(1);
  127. });
  128. it('Allows disabling multiple actions', async () => {
  129. render(
  130. <WidgetFrame
  131. title="EPS"
  132. description="Number of events per second"
  133. actionsDisabled
  134. actionsMessage="Actions are not supported"
  135. actions={[
  136. {
  137. key: 'one',
  138. label: 'One',
  139. },
  140. {
  141. key: 'two',
  142. label: 'Two',
  143. },
  144. ]}
  145. />
  146. );
  147. const $trigger = screen.getByRole('button', {name: 'Widget actions'});
  148. await userEvent.click($trigger);
  149. expect(screen.queryByRole('menuitemradio', {name: 'One'})).not.toBeInTheDocument();
  150. expect(screen.queryByRole('menuitemradio', {name: 'Two'})).not.toBeInTheDocument();
  151. await userEvent.hover($trigger);
  152. expect(await screen.findByText('Actions are not supported')).toBeInTheDocument();
  153. });
  154. });
  155. describe('Full Screen View Button', () => {
  156. it('Renders a full screen view button', async () => {
  157. const onFullScreenViewClick = jest.fn();
  158. const {rerender} = render(<WidgetFrame title="count()" />);
  159. expect(
  160. screen.queryByRole('button', {name: 'Open Full-Screen View'})
  161. ).not.toBeInTheDocument();
  162. rerender(
  163. <WidgetFrame title="count()" onFullScreenViewClick={onFullScreenViewClick} />
  164. );
  165. const $button = screen.getByRole('button', {name: 'Open Full-Screen View'});
  166. expect($button).toBeInTheDocument();
  167. await userEvent.click($button);
  168. expect(onFullScreenViewClick).toHaveBeenCalledTimes(1);
  169. });
  170. });
  171. });