LinksSettings.test.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 { LinksSettings } from './LinksSettings';
  7. describe('LinksSettings', () => {
  8. let dashboard = {};
  9. const links = [
  10. {
  11. asDropdown: false,
  12. icon: 'external link',
  13. includeVars: false,
  14. keepTime: false,
  15. tags: [],
  16. targetBlank: false,
  17. title: 'link 1',
  18. tooltip: '',
  19. type: 'link',
  20. url: 'https://www.google.com',
  21. },
  22. {
  23. asDropdown: false,
  24. icon: 'external link',
  25. includeVars: false,
  26. keepTime: false,
  27. tags: ['gdev'],
  28. targetBlank: false,
  29. title: 'link 2',
  30. tooltip: '',
  31. type: 'dashboards',
  32. url: '',
  33. },
  34. {
  35. asDropdown: false,
  36. icon: 'external link',
  37. includeVars: false,
  38. keepTime: false,
  39. tags: [],
  40. targetBlank: false,
  41. title: '',
  42. tooltip: '',
  43. type: 'link',
  44. url: 'https://www.bing.com',
  45. },
  46. ];
  47. const getTableBody = () => screen.getAllByRole('rowgroup')[1];
  48. const getTableBodyRows = () => within(getTableBody()).getAllByRole('row');
  49. const assertRowHasText = (index: number, text: string) => {
  50. expect(within(getTableBodyRows()[index]).queryByText(text)).toBeInTheDocument();
  51. };
  52. beforeEach(() => {
  53. dashboard = {
  54. id: 74,
  55. version: 7,
  56. links: [...links],
  57. };
  58. });
  59. test('it renders a header and cta if no links', () => {
  60. const linklessDashboard = { ...dashboard, links: [] };
  61. // @ts-ignore
  62. render(<LinksSettings dashboard={linklessDashboard} />);
  63. expect(screen.getByRole('heading', { name: 'Dashboard links' })).toBeInTheDocument();
  64. expect(
  65. screen.getByTestId(selectors.components.CallToActionCard.buttonV2('Add dashboard link'))
  66. ).toBeInTheDocument();
  67. expect(screen.queryByRole('table')).not.toBeInTheDocument();
  68. });
  69. test('it renders a table of links', () => {
  70. // @ts-ignore
  71. render(<LinksSettings dashboard={dashboard} />);
  72. expect(getTableBodyRows().length).toBe(links.length);
  73. expect(
  74. screen.queryByTestId(selectors.components.CallToActionCard.buttonV2('Add dashboard link'))
  75. ).not.toBeInTheDocument();
  76. });
  77. test('it rearranges the order of dashboard links', async () => {
  78. // @ts-ignore
  79. render(<LinksSettings dashboard={dashboard} />);
  80. // Check that we have sorting buttons
  81. expect(within(getTableBodyRows()[0]).queryByRole('button', { name: 'arrow-up' })).not.toBeInTheDocument();
  82. expect(within(getTableBodyRows()[0]).queryByRole('button', { name: 'arrow-down' })).toBeInTheDocument();
  83. expect(within(getTableBodyRows()[1]).queryByRole('button', { name: 'arrow-up' })).toBeInTheDocument();
  84. expect(within(getTableBodyRows()[1]).queryByRole('button', { name: 'arrow-down' })).toBeInTheDocument();
  85. expect(within(getTableBodyRows()[2]).queryByRole('button', { name: 'arrow-up' })).toBeInTheDocument();
  86. expect(within(getTableBodyRows()[2]).queryByRole('button', { name: 'arrow-down' })).not.toBeInTheDocument();
  87. // Checking the original order
  88. assertRowHasText(0, links[0].title);
  89. assertRowHasText(1, links[1].title);
  90. assertRowHasText(2, links[2].url);
  91. await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'arrow-down' })[0]);
  92. await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'arrow-down' })[1]);
  93. await userEvent.click(within(getTableBody()).getAllByRole('button', { name: 'arrow-up' })[0]);
  94. // Checking if it has changed the sorting accordingly
  95. assertRowHasText(0, links[2].url);
  96. assertRowHasText(1, links[1].title);
  97. assertRowHasText(2, links[0].title);
  98. });
  99. test('it duplicates dashboard links', async () => {
  100. // @ts-ignore
  101. render(<LinksSettings dashboard={dashboard} />);
  102. expect(getTableBodyRows().length).toBe(links.length);
  103. await userEvent.click(within(getTableBody()).getAllByRole('button', { name: /copy/i })[0]);
  104. expect(getTableBodyRows().length).toBe(links.length + 1);
  105. expect(within(getTableBody()).getAllByText(links[0].title).length).toBe(2);
  106. });
  107. test('it deletes dashboard links', async () => {
  108. // @ts-ignore
  109. render(<LinksSettings dashboard={dashboard} />);
  110. expect(getTableBodyRows().length).toBe(links.length);
  111. await userEvent.click(within(getTableBody()).getAllByLabelText(/Delete link with title/)[0]);
  112. await userEvent.click(within(getTableBody()).getByRole('button', { name: 'Delete' }));
  113. expect(getTableBodyRows().length).toBe(links.length - 1);
  114. expect(within(getTableBody()).queryByText(links[0].title)).not.toBeInTheDocument();
  115. });
  116. test('it renders a form which modifies dashboard links', async () => {
  117. // @ts-ignore
  118. render(<LinksSettings dashboard={dashboard} />);
  119. await userEvent.click(screen.getByRole('button', { name: /new/i }));
  120. expect(screen.queryByText('Type')).toBeInTheDocument();
  121. expect(screen.queryByText('Title')).toBeInTheDocument();
  122. expect(screen.queryByText('With tags')).toBeInTheDocument();
  123. expect(screen.queryByText('Apply')).toBeInTheDocument();
  124. expect(screen.queryByText('Url')).not.toBeInTheDocument();
  125. expect(screen.queryByText('Tooltip')).not.toBeInTheDocument();
  126. expect(screen.queryByText('Icon')).not.toBeInTheDocument();
  127. await userEvent.click(screen.getByText('Dashboards'));
  128. expect(screen.queryAllByText('Dashboards')).toHaveLength(2);
  129. expect(screen.queryByText('Link')).toBeVisible();
  130. await userEvent.click(screen.getByText('Link'));
  131. expect(screen.queryByText('URL')).toBeInTheDocument();
  132. expect(screen.queryByText('Tooltip')).toBeInTheDocument();
  133. expect(screen.queryByText('Icon')).toBeInTheDocument();
  134. await userEvent.clear(screen.getByRole('textbox', { name: /title/i }));
  135. await userEvent.type(screen.getByRole('textbox', { name: /title/i }), 'New Dashboard Link');
  136. await userEvent.click(
  137. within(screen.getByRole('heading', { name: /dashboard links edit/i })).getByText(/dashboard links/i)
  138. );
  139. expect(getTableBodyRows().length).toBe(links.length + 1);
  140. expect(within(getTableBody()).queryByText('New Dashboard Link')).toBeInTheDocument();
  141. await userEvent.click(screen.getAllByText(links[0].type)[0]);
  142. await userEvent.clear(screen.getByRole('textbox', { name: /title/i }));
  143. await userEvent.type(screen.getByRole('textbox', { name: /title/i }), 'The first dashboard link');
  144. await userEvent.click(
  145. within(screen.getByRole('heading', { name: /dashboard links edit/i })).getByText(/dashboard links/i)
  146. );
  147. expect(within(getTableBody()).queryByText(links[0].title)).not.toBeInTheDocument();
  148. expect(within(getTableBody()).queryByText('The first dashboard link')).toBeInTheDocument();
  149. });
  150. });