PlaylistForm.test.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 { PlaylistForm } from './PlaylistForm';
  6. import { Playlist } from './types';
  7. jest.mock('../../core/components/TagFilter/TagFilter', () => ({
  8. TagFilter: () => {
  9. return <>mocked-tag-filter</>;
  10. },
  11. }));
  12. function getTestContext({ name, interval, items, uid }: Partial<Playlist> = {}) {
  13. const onSubmitMock = jest.fn();
  14. const playlist = { name, items, interval, uid } as unknown as Playlist;
  15. const { rerender } = render(<PlaylistForm onSubmit={onSubmitMock} playlist={playlist} />);
  16. return { onSubmitMock, playlist, rerender };
  17. }
  18. const playlist: Playlist = {
  19. name: 'A test playlist',
  20. interval: '10m',
  21. items: [
  22. { title: 'First item', type: 'dashboard_by_id', order: 1, value: '1' },
  23. { title: 'Middle item', type: 'dashboard_by_id', order: 2, value: '2' },
  24. { title: 'Last item', type: 'dashboard_by_tag', order: 2, value: 'Last item' },
  25. ],
  26. uid: 'foo',
  27. };
  28. function rows() {
  29. return screen.getAllByRole('row', { name: /playlist item row/i });
  30. }
  31. describe('PlaylistForm', () => {
  32. describe('when mounted without playlist', () => {
  33. it('then it should contain name and interval fields', () => {
  34. getTestContext();
  35. expect(screen.getByRole('textbox', { name: /playlist name/i })).toBeInTheDocument();
  36. expect(screen.getByRole('textbox', { name: /playlist interval/i })).toBeInTheDocument();
  37. expect(screen.queryByRole('row', { name: /playlist item row/i })).not.toBeInTheDocument();
  38. });
  39. it('then name field should have empty string as default value', () => {
  40. getTestContext();
  41. expect(screen.getByRole('textbox', { name: /playlist name/i })).toHaveValue('');
  42. });
  43. it('then interval field should have 5m as default value', () => {
  44. getTestContext();
  45. expect(screen.getByRole('textbox', { name: /playlist interval/i })).toHaveValue('5m');
  46. });
  47. });
  48. describe('when mounted with a playlist', () => {
  49. it('then name field should have correct value', () => {
  50. getTestContext(playlist);
  51. expect(screen.getByRole('textbox', { name: /playlist name/i })).toHaveValue('A test playlist');
  52. });
  53. it('then interval field should have correct value', () => {
  54. getTestContext(playlist);
  55. expect(screen.getByRole('textbox', { name: /playlist interval/i })).toHaveValue('10m');
  56. });
  57. it('then items row count should be correct', () => {
  58. getTestContext(playlist);
  59. expect(screen.getAllByRole('row', { name: /playlist item row/i })).toHaveLength(3);
  60. });
  61. it('then the first item row should be correct', () => {
  62. getTestContext(playlist);
  63. expectCorrectRow({ index: 0, type: 'id', title: 'first item', first: true });
  64. });
  65. it('then the middle item row should be correct', () => {
  66. getTestContext(playlist);
  67. expectCorrectRow({ index: 1, type: 'id', title: 'middle item' });
  68. });
  69. it('then the last item row should be correct', () => {
  70. getTestContext(playlist);
  71. expectCorrectRow({ index: 2, type: 'tag', title: 'last item', last: true });
  72. });
  73. });
  74. describe('when deleting a playlist item', () => {
  75. it('then the item should be removed and other items should be correct', async () => {
  76. getTestContext(playlist);
  77. expect(rows()).toHaveLength(3);
  78. await userEvent.click(within(rows()[2]).getByRole('button', { name: /delete playlist item/i }));
  79. expect(rows()).toHaveLength(2);
  80. expectCorrectRow({ index: 0, type: 'id', title: 'first item', first: true });
  81. expectCorrectRow({ index: 1, type: 'id', title: 'middle item', last: true });
  82. });
  83. });
  84. describe('when moving a playlist item up', () => {
  85. it('then the item should be removed and other items should be correct', async () => {
  86. getTestContext(playlist);
  87. await userEvent.click(within(rows()[2]).getByRole('button', { name: /move playlist item order up/i }));
  88. expectCorrectRow({ index: 0, type: 'id', title: 'first item', first: true });
  89. expectCorrectRow({ index: 1, type: 'tag', title: 'last item' });
  90. expectCorrectRow({ index: 2, type: 'id', title: 'middle item', last: true });
  91. });
  92. });
  93. describe('when moving a playlist item down', () => {
  94. it('then the item should be removed and other items should be correct', async () => {
  95. getTestContext(playlist);
  96. await userEvent.click(within(rows()[0]).getByRole('button', { name: /move playlist item order down/i }));
  97. expectCorrectRow({ index: 0, type: 'id', title: 'middle item', first: true });
  98. expectCorrectRow({ index: 1, type: 'id', title: 'first item' });
  99. expectCorrectRow({ index: 2, type: 'tag', title: 'last item', last: true });
  100. });
  101. });
  102. describe('when submitting the form', () => {
  103. it('then the correct item should be submitted', async () => {
  104. const { onSubmitMock } = getTestContext(playlist);
  105. await userEvent.click(screen.getByRole('button', { name: /save/i }));
  106. expect(onSubmitMock).toHaveBeenCalledTimes(1);
  107. expect(onSubmitMock).toHaveBeenCalledWith({
  108. name: 'A test playlist',
  109. interval: '10m',
  110. items: [
  111. { title: 'First item', type: 'dashboard_by_id', order: 1, value: '1' },
  112. { title: 'Middle item', type: 'dashboard_by_id', order: 2, value: '2' },
  113. { title: 'Last item', type: 'dashboard_by_tag', order: 2, value: 'Last item' },
  114. ],
  115. });
  116. });
  117. describe('and name is missing', () => {
  118. it('then an alert should appear and nothing should be submitted', async () => {
  119. const { onSubmitMock } = getTestContext({ ...playlist, name: undefined });
  120. await userEvent.click(screen.getByRole('button', { name: /save/i }));
  121. expect(screen.getAllByRole('alert')).toHaveLength(1);
  122. expect(onSubmitMock).not.toHaveBeenCalled();
  123. });
  124. });
  125. describe('and interval is missing', () => {
  126. it('then an alert should appear and nothing should be submitted', async () => {
  127. const { onSubmitMock } = getTestContext(playlist);
  128. await userEvent.clear(screen.getByRole('textbox', { name: /playlist interval/i }));
  129. await userEvent.click(screen.getByRole('button', { name: /save/i }));
  130. expect(screen.getAllByRole('alert')).toHaveLength(1);
  131. expect(onSubmitMock).not.toHaveBeenCalled();
  132. });
  133. });
  134. });
  135. describe('when items are missing', () => {
  136. it('then save button is disabled', async () => {
  137. getTestContext({ ...playlist, items: [] });
  138. expect(screen.getByRole('button', { name: /save/i })).toBeDisabled();
  139. });
  140. });
  141. });
  142. interface ExpectCorrectRowArgs {
  143. index: number;
  144. type: 'id' | 'tag';
  145. title: string;
  146. first?: boolean;
  147. last?: boolean;
  148. }
  149. function expectCorrectRow({ index, type, title, first = false, last = false }: ExpectCorrectRowArgs) {
  150. const row = within(rows()[index]);
  151. const cell = `playlist item dashboard by ${type} type ${title}`;
  152. const regex = new RegExp(cell, 'i');
  153. expect(row.getByRole('cell', { name: regex })).toBeInTheDocument();
  154. if (first) {
  155. expect(row.queryByRole('button', { name: /move playlist item order up/i })).not.toBeInTheDocument();
  156. } else {
  157. expect(row.getByRole('button', { name: /move playlist item order up/i })).toBeInTheDocument();
  158. }
  159. if (last) {
  160. expect(row.queryByRole('button', { name: /move playlist item order down/i })).not.toBeInTheDocument();
  161. } else {
  162. expect(row.getByRole('button', { name: /move playlist item order down/i })).toBeInTheDocument();
  163. }
  164. expect(row.getByRole('button', { name: /delete playlist item/i })).toBeInTheDocument();
  165. }