editAccessSelector.spec.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import {DashboardFixture} from 'sentry-fixture/dashboard';
  2. import {TeamFixture} from 'sentry-fixture/team';
  3. import {UserFixture} from 'sentry-fixture/user';
  4. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  5. import TeamStore from 'sentry/stores/teamStore';
  6. import EditAccessSelector from 'sentry/views/dashboards/editAccessSelector';
  7. function renderTestComponent(
  8. mockDashboard = DashboardFixture([], {
  9. id: '1',
  10. title: 'test dashboard 2',
  11. createdBy: UserFixture({id: '35478'}),
  12. })
  13. ) {
  14. render(<EditAccessSelector dashboard={mockDashboard} onChangeEditAccess={jest.fn()} />);
  15. }
  16. describe('When EditAccessSelector is rendered with no Teams', () => {
  17. beforeEach(() => {
  18. MockApiClient.addMockResponse({
  19. url: '/organizations/org-slug/dashboards/',
  20. body: [
  21. {
  22. ...DashboardFixture([], {
  23. id: 'default-overview',
  24. title: 'Default',
  25. }),
  26. },
  27. {
  28. ...DashboardFixture([], {
  29. id: '1',
  30. title: 'test dashboard 2',
  31. createdBy: UserFixture({id: '35478'}),
  32. }),
  33. },
  34. ],
  35. });
  36. MockApiClient.addMockResponse({
  37. url: '/organizations/org-slug/dashboards/1/',
  38. body: DashboardFixture([], {
  39. id: '1',
  40. title: 'Custom Errors',
  41. filters: {},
  42. }),
  43. });
  44. });
  45. afterEach(() => {
  46. MockApiClient.clearMockResponses();
  47. jest.clearAllMocks();
  48. });
  49. it('renders with creator and everyone options', async function () {
  50. renderTestComponent();
  51. await userEvent.click(await screen.findByText('Edit Access:'));
  52. expect(screen.getByText('Creator')).toBeInTheDocument();
  53. expect(screen.getByText('All users')).toBeInTheDocument();
  54. });
  55. it('renders All badge when dashboards has no perms defined', async function () {
  56. renderTestComponent();
  57. await userEvent.click(await screen.findByText('Edit Access:'));
  58. expect(screen.getByText('All')).toBeInTheDocument();
  59. });
  60. it('renders All badge when perms is set to everyone', async function () {
  61. const mockDashboard = DashboardFixture([], {
  62. id: '1',
  63. createdBy: UserFixture({id: '1'}),
  64. title: 'Custom Errors',
  65. permissions: {isEditableByEveryone: true}, // set to true
  66. });
  67. renderTestComponent(mockDashboard);
  68. await screen.findByText('Edit Access:');
  69. expect(screen.getByText('All')).toBeInTheDocument();
  70. });
  71. it('renders All badge when All users is selected', async function () {
  72. const mockDashboard = DashboardFixture([], {
  73. id: '1',
  74. createdBy: UserFixture({id: '1'}),
  75. title: 'Custom Errors',
  76. permissions: {isEditableByEveryone: false}, // set to false
  77. });
  78. renderTestComponent(mockDashboard);
  79. await userEvent.click(await screen.findByText('Edit Access:'));
  80. expect(screen.queryByText('All')).not.toBeInTheDocument();
  81. // Select everyone
  82. expect(await screen.findByRole('option', {name: 'All users'})).toHaveAttribute(
  83. 'aria-selected',
  84. 'false'
  85. );
  86. await userEvent.click(screen.getByRole('option', {name: 'All users'}));
  87. expect(await screen.findByRole('option', {name: 'All users'})).toHaveAttribute(
  88. 'aria-selected',
  89. 'true'
  90. );
  91. expect(screen.getByText('All')).toBeInTheDocument();
  92. });
  93. it('renders User badge when creator-only is selected', async function () {
  94. const mockDashboard = DashboardFixture([], {
  95. id: '1',
  96. createdBy: UserFixture({id: '1', name: 'Lorem Ipsum'}),
  97. title: 'Custom Errors',
  98. permissions: {isEditableByEveryone: false}, // set to false
  99. });
  100. renderTestComponent(mockDashboard);
  101. await screen.findByText('Edit Access:');
  102. expect(screen.getByText('LI')).toBeInTheDocument(); // dashboard owner's initials
  103. expect(screen.queryByText('All')).not.toBeInTheDocument();
  104. });
  105. });
  106. const teamData = [
  107. {
  108. id: '1',
  109. slug: 'team1',
  110. name: 'Team 1',
  111. },
  112. {
  113. id: '2',
  114. slug: 'team2',
  115. name: 'Team 2',
  116. },
  117. {
  118. id: '3',
  119. slug: 'team3',
  120. name: 'Team 3',
  121. },
  122. ];
  123. describe('When EditAccessSelector is rendered with Teams', function () {
  124. const teams = teamData.map(data => TeamFixture(data));
  125. beforeEach(function () {
  126. TeamStore.loadInitialData(teams);
  127. MockApiClient.addMockResponse({
  128. url: '/organizations/org-slug/dashboards/',
  129. body: [
  130. {
  131. ...DashboardFixture([], {
  132. id: 'default-overview',
  133. title: 'Default',
  134. }),
  135. },
  136. {
  137. ...DashboardFixture([], {
  138. id: '1',
  139. title: 'test dashboard 2',
  140. createdBy: UserFixture({id: '35478'}),
  141. }),
  142. },
  143. ],
  144. });
  145. MockApiClient.addMockResponse({
  146. url: '/organizations/org-slug/dashboards/1/',
  147. body: DashboardFixture([], {
  148. id: '1',
  149. title: 'Custom Errors',
  150. filters: {},
  151. }),
  152. });
  153. });
  154. afterEach(() => {
  155. MockApiClient.clearMockResponses();
  156. jest.clearAllMocks();
  157. });
  158. it('renders all teams', async function () {
  159. renderTestComponent();
  160. await userEvent.click(await screen.findByText('Edit Access:'));
  161. expect(screen.getByText('Teams')).toBeInTheDocument();
  162. expect(screen.getByText('#team1')).toBeInTheDocument();
  163. expect(screen.getByText('#team2')).toBeInTheDocument();
  164. expect(screen.getByText('#team3')).toBeInTheDocument();
  165. });
  166. it('selects all teams when all users is selected', async function () {
  167. const mockDashboard = DashboardFixture([], {
  168. id: '1',
  169. createdBy: UserFixture({id: '1'}),
  170. title: 'Custom Errors',
  171. permissions: {isEditableByEveryone: false}, // set to false
  172. });
  173. renderTestComponent(mockDashboard);
  174. await userEvent.click(await screen.findByText('Edit Access:'));
  175. expect(await screen.findByRole('option', {name: '#team1'})).toHaveAttribute(
  176. 'aria-selected',
  177. 'false'
  178. );
  179. expect(await screen.findByRole('option', {name: '#team2'})).toHaveAttribute(
  180. 'aria-selected',
  181. 'false'
  182. );
  183. // Select everyone
  184. expect(await screen.findByRole('option', {name: 'All users'})).toHaveAttribute(
  185. 'aria-selected',
  186. 'false'
  187. );
  188. await userEvent.click(screen.getByRole('option', {name: 'All users'}));
  189. expect(await screen.findByRole('option', {name: 'All users'})).toHaveAttribute(
  190. 'aria-selected',
  191. 'true'
  192. );
  193. expect(await screen.findByRole('option', {name: '#team1'})).toHaveAttribute(
  194. 'aria-selected',
  195. 'true'
  196. );
  197. expect(await screen.findByRole('option', {name: '#team2'})).toHaveAttribute(
  198. 'aria-selected',
  199. 'true'
  200. );
  201. });
  202. it('searches teams', async function () {
  203. renderTestComponent();
  204. await userEvent.click(await screen.findByText('Edit Access:'));
  205. await userEvent.type(screen.getByPlaceholderText('Search Teams'), 'team2');
  206. expect(screen.getByText('#team2')).toBeInTheDocument();
  207. });
  208. });