AnnotationsSettings.test.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { within } from '@testing-library/dom';
  2. import { render, screen } from '@testing-library/react';
  3. import userEvent from '@testing-library/user-event';
  4. import React from 'react';
  5. import { selectors } from '@grafana/e2e-selectors';
  6. import { setAngularLoader, setDataSourceSrv } from '@grafana/runtime';
  7. import { mockDataSource, MockDataSourceSrv } from 'app/features/alerting/unified/mocks';
  8. import { AnnotationsSettings } from './AnnotationsSettings';
  9. describe('AnnotationsSettings', () => {
  10. let dashboard: any;
  11. const dataSources = {
  12. grafana: mockDataSource(
  13. {
  14. name: 'Grafana',
  15. uid: 'uid1',
  16. type: 'grafana',
  17. isDefault: true,
  18. },
  19. { annotations: true }
  20. ),
  21. Testdata: mockDataSource(
  22. {
  23. name: 'Testdata',
  24. uid: 'uid2',
  25. type: 'testdata',
  26. isDefault: true,
  27. },
  28. { annotations: true }
  29. ),
  30. Prometheus: mockDataSource(
  31. {
  32. name: 'Prometheus',
  33. uid: 'uid3',
  34. type: 'prometheus',
  35. },
  36. { annotations: true }
  37. ),
  38. };
  39. setDataSourceSrv(new MockDataSourceSrv(dataSources));
  40. const getTableBody = () => screen.getAllByRole('rowgroup')[1];
  41. const getTableBodyRows = () => within(getTableBody()).getAllByRole('row');
  42. beforeAll(() => {
  43. setAngularLoader({
  44. load: () => ({
  45. destroy: jest.fn(),
  46. digest: jest.fn(),
  47. getScope: () => ({ $watch: () => {} }),
  48. }),
  49. });
  50. });
  51. beforeEach(() => {
  52. dashboard = {
  53. id: 74,
  54. version: 7,
  55. annotations: {
  56. list: [
  57. {
  58. builtIn: 1,
  59. datasource: { uid: 'uid1', type: 'grafana' },
  60. enable: true,
  61. hide: true,
  62. iconColor: 'rgba(0, 211, 255, 1)',
  63. name: 'Annotations & Alerts',
  64. type: 'dashboard',
  65. },
  66. ],
  67. },
  68. links: [],
  69. };
  70. });
  71. test('it renders a header and cta if no annotations or only builtIn annotation', async () => {
  72. render(<AnnotationsSettings dashboard={dashboard} />);
  73. expect(screen.getByRole('heading', { name: /annotations/i })).toBeInTheDocument();
  74. expect(screen.queryByRole('table')).toBeInTheDocument();
  75. expect(screen.getByRole('row', { name: /annotations & alerts \(built\-in\) grafana/i })).toBeInTheDocument();
  76. expect(
  77. screen.getByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query'))
  78. ).toBeInTheDocument();
  79. expect(screen.queryByRole('link', { name: /annotations documentation/i })).toBeInTheDocument();
  80. await userEvent.click(screen.getByRole('cell', { name: /annotations & alerts \(built\-in\)/i }));
  81. const heading = screen.getByRole('heading', {
  82. name: /annotations edit/i,
  83. });
  84. const nameInput = screen.getByRole('textbox', { name: /name/i });
  85. expect(heading).toBeInTheDocument();
  86. await userEvent.clear(nameInput);
  87. await userEvent.type(nameInput, 'My Annotation');
  88. expect(screen.queryByText(/grafana/i)).toBeInTheDocument();
  89. expect(screen.getByRole('checkbox', { name: /hidden/i })).toBeChecked();
  90. await userEvent.click(within(heading).getByText(/annotations/i));
  91. expect(screen.getByRole('table')).toBeInTheDocument();
  92. expect(screen.getByRole('row', { name: /my annotation \(built\-in\) grafana/i })).toBeInTheDocument();
  93. expect(
  94. screen.getByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query'))
  95. ).toBeInTheDocument();
  96. expect(screen.queryByRole('button', { name: /new query/i })).not.toBeInTheDocument();
  97. await userEvent.click(screen.getAllByLabelText(/Delete query with title/)[0]);
  98. await userEvent.click(screen.getByRole('button', { name: 'Delete' }));
  99. expect(screen.queryAllByRole('row').length).toBe(0);
  100. expect(
  101. screen.getByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query'))
  102. ).toBeInTheDocument();
  103. });
  104. test('it renders the anotation names or uid if annotation doesnt exist', async () => {
  105. const annotationsList = [
  106. ...dashboard.annotations.list,
  107. {
  108. builtIn: 0,
  109. datasource: { uid: 'uid3', type: 'prometheus' },
  110. enable: true,
  111. hide: true,
  112. iconColor: 'rgba(0, 211, 255, 1)',
  113. name: 'Annotation 2',
  114. type: 'dashboard',
  115. },
  116. {
  117. builtIn: 0,
  118. datasource: { uid: 'deletedAnnotationId', type: 'prometheus' },
  119. enable: true,
  120. hide: true,
  121. iconColor: 'rgba(0, 211, 255, 1)',
  122. name: 'Annotation 2',
  123. type: 'dashboard',
  124. },
  125. ];
  126. const dashboardWithAnnotations = {
  127. ...dashboard,
  128. annotations: {
  129. list: [...annotationsList],
  130. },
  131. };
  132. render(<AnnotationsSettings dashboard={dashboardWithAnnotations} />);
  133. // Check that we have the correct annotations
  134. expect(screen.queryByText(/prometheus/i)).toBeInTheDocument();
  135. expect(screen.queryByText(/deletedAnnotationId/i)).toBeInTheDocument();
  136. });
  137. test('it renders a sortable table of annotations', async () => {
  138. const annotationsList = [
  139. ...dashboard.annotations.list,
  140. {
  141. builtIn: 0,
  142. datasource: { uid: 'uid3', type: 'prometheus' },
  143. enable: true,
  144. hide: true,
  145. iconColor: 'rgba(0, 211, 255, 1)',
  146. name: 'Annotation 2',
  147. type: 'dashboard',
  148. },
  149. {
  150. builtIn: 0,
  151. datasource: { uid: 'uid3', type: 'prometheus' },
  152. enable: true,
  153. hide: true,
  154. iconColor: 'rgba(0, 211, 255, 1)',
  155. name: 'Annotation 3',
  156. type: 'dashboard',
  157. },
  158. ];
  159. const dashboardWithAnnotations = {
  160. ...dashboard,
  161. annotations: {
  162. list: [...annotationsList],
  163. },
  164. };
  165. render(<AnnotationsSettings dashboard={dashboardWithAnnotations} />);
  166. // Check that we have sorting buttons
  167. expect(within(getTableBodyRows()[0]).queryByRole('button', { name: 'arrow-up' })).not.toBeInTheDocument();
  168. expect(within(getTableBodyRows()[0]).queryByRole('button', { name: 'arrow-down' })).toBeInTheDocument();
  169. expect(within(getTableBodyRows()[1]).queryByRole('button', { name: 'arrow-up' })).toBeInTheDocument();
  170. expect(within(getTableBodyRows()[1]).queryByRole('button', { name: 'arrow-down' })).toBeInTheDocument();
  171. expect(within(getTableBodyRows()[2]).queryByRole('button', { name: 'arrow-up' })).toBeInTheDocument();
  172. expect(within(getTableBodyRows()[2]).queryByRole('button', { name: 'arrow-down' })).not.toBeInTheDocument();
  173. // Check the original order
  174. expect(within(getTableBodyRows()[0]).queryByText(/annotations & alerts/i)).toBeInTheDocument();
  175. expect(within(getTableBodyRows()[1]).queryByText(/annotation 2/i)).toBeInTheDocument();
  176. expect(within(getTableBodyRows()[2]).queryByText(/annotation 3/i)).toBeInTheDocument();
  177. await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'arrow-down' })[0]);
  178. await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'arrow-down' })[1]);
  179. await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'arrow-up' })[0]);
  180. // Checking if it has changed the sorting accordingly
  181. expect(within(getTableBodyRows()[0]).queryByText(/annotation 3/i)).toBeInTheDocument();
  182. expect(within(getTableBodyRows()[1]).queryByText(/annotation 2/i)).toBeInTheDocument();
  183. expect(within(getTableBodyRows()[2]).queryByText(/annotations & alerts/i)).toBeInTheDocument();
  184. });
  185. test('it renders a form for adding/editing annotations', async () => {
  186. render(<AnnotationsSettings dashboard={dashboard} />);
  187. await userEvent.click(screen.getByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query')));
  188. const heading = screen.getByRole('heading', {
  189. name: /annotations edit/i,
  190. });
  191. const nameInput = screen.getByRole('textbox', { name: /name/i });
  192. expect(heading).toBeInTheDocument();
  193. await userEvent.clear(nameInput);
  194. await userEvent.type(nameInput, 'My Prometheus Annotation');
  195. await userEvent.click(screen.getByText(/testdata/i));
  196. expect(await screen.findByText(/Prometheus/i)).toBeVisible();
  197. expect(screen.queryAllByText(/testdata/i)).toHaveLength(2);
  198. await userEvent.click(screen.getByText(/prometheus/i));
  199. expect(screen.getByRole('checkbox', { name: /hidden/i })).not.toBeChecked();
  200. await userEvent.click(within(heading).getByText(/annotations/i));
  201. expect(within(screen.getAllByRole('rowgroup')[1]).getAllByRole('row').length).toBe(2);
  202. expect(screen.queryByRole('row', { name: /my prometheus annotation prometheus/i })).toBeInTheDocument();
  203. expect(screen.queryByRole('button', { name: /new query/i })).toBeInTheDocument();
  204. expect(
  205. screen.queryByTestId(selectors.components.CallToActionCard.buttonV2('Add annotation query'))
  206. ).not.toBeInTheDocument();
  207. await userEvent.click(screen.getByRole('button', { name: /new query/i }));
  208. await userEvent.click(within(screen.getByRole('heading', { name: /annotations edit/i })).getByText(/annotations/i));
  209. expect(within(screen.getAllByRole('rowgroup')[1]).getAllByRole('row').length).toBe(3);
  210. await userEvent.click(screen.getAllByLabelText(/Delete query with title/)[0]);
  211. await userEvent.click(screen.getByRole('button', { name: 'Delete' }));
  212. expect(within(screen.getAllByRole('rowgroup')[1]).getAllByRole('row').length).toBe(2);
  213. });
  214. });