editAccessSelector.spec.tsx 7.1 KB

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