editAccessSelector.spec.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import {DashboardFixture} from 'sentry-fixture/dashboard';
  2. import {LocationFixture} from 'sentry-fixture/locationFixture';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {UserFixture} from 'sentry-fixture/user';
  5. import {initializeOrg} from 'sentry-test/initializeOrg';
  6. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  7. import ConfigStore from 'sentry/stores/configStore';
  8. import EditAccessSelector from './editAccessSelector';
  9. function renderTestComponent(
  10. initialData,
  11. mockDashboard = DashboardFixture([], {
  12. id: '1',
  13. title: 'test dashboard 2',
  14. createdBy: UserFixture({id: '35478'}),
  15. })
  16. ) {
  17. render(
  18. <EditAccessSelector dashboard={mockDashboard} onChangeEditAccess={jest.fn()} />,
  19. {
  20. router: initialData.router,
  21. organization: {
  22. user: UserFixture({id: '1'}),
  23. features: ['dashboards-edit-access', 'dashboards-edit'],
  24. ...initialData.organization,
  25. },
  26. }
  27. );
  28. }
  29. describe('When EditAccessSelector is rendered', () => {
  30. let initialData;
  31. const organization = OrganizationFixture({});
  32. beforeEach(() => {
  33. initialData = initializeOrg({
  34. organization,
  35. router: {
  36. location: LocationFixture(),
  37. },
  38. });
  39. MockApiClient.addMockResponse({
  40. url: '/organizations/org-slug/dashboards/',
  41. body: [
  42. {
  43. ...DashboardFixture([], {
  44. id: 'default-overview',
  45. title: 'Default',
  46. }),
  47. },
  48. {
  49. ...DashboardFixture([], {
  50. id: '1',
  51. title: 'test dashboard 2',
  52. createdBy: UserFixture({id: '35478'}),
  53. }),
  54. },
  55. ],
  56. });
  57. MockApiClient.addMockResponse({
  58. url: '/organizations/org-slug/dashboards/1/',
  59. body: DashboardFixture([], {
  60. id: '1',
  61. title: 'Custom Errors',
  62. filters: {},
  63. }),
  64. });
  65. });
  66. afterEach(() => {
  67. MockApiClient.clearMockResponses();
  68. jest.clearAllMocks();
  69. });
  70. it('renders with creator and everyone options', async function () {
  71. renderTestComponent(initialData);
  72. await userEvent.click(await screen.findByText('Edit Access:'));
  73. expect(screen.getByText('Creator')).toBeInTheDocument();
  74. expect(screen.getByText('Everyone')).toBeInTheDocument();
  75. });
  76. it('renders All badge when dashboards has no perms defined', async function () {
  77. renderTestComponent(initialData);
  78. await userEvent.click(await screen.findByText('Edit Access:'));
  79. expect(screen.getByText('All')).toBeInTheDocument();
  80. });
  81. it('renders All badge when perms is set to everyone', async function () {
  82. const mockDashboard = DashboardFixture([], {
  83. id: '1',
  84. createdBy: UserFixture({id: '1'}),
  85. title: 'Custom Errors',
  86. permissions: {isCreatorOnlyEditable: false}, // set to false
  87. });
  88. renderTestComponent(initialData, mockDashboard);
  89. await screen.findByText('Edit Access:');
  90. expect(screen.getByText('All')).toBeInTheDocument();
  91. });
  92. it('renders All badge when everyone is selected', async function () {
  93. const mockDashboard = DashboardFixture([], {
  94. id: '1',
  95. createdBy: UserFixture({id: '1'}),
  96. title: 'Custom Errors',
  97. permissions: {isCreatorOnlyEditable: true}, // set to false
  98. });
  99. renderTestComponent(initialData, mockDashboard);
  100. await userEvent.click(await screen.findByText('Edit Access:'));
  101. expect(screen.queryByText('All')).not.toBeInTheDocument();
  102. // Select everyone
  103. expect(await screen.findByRole('option', {name: 'Everyone'})).toHaveAttribute(
  104. 'aria-selected',
  105. 'false'
  106. );
  107. await userEvent.click(screen.getByRole('option', {name: 'Everyone'}));
  108. expect(await screen.findByRole('option', {name: 'Everyone'})).toHaveAttribute(
  109. 'aria-selected',
  110. 'true'
  111. );
  112. expect(screen.getByText('All')).toBeInTheDocument();
  113. });
  114. it('renders User badge when creator-only is selected', async function () {
  115. const currentUser = UserFixture({id: '781629', name: 'John Doe'});
  116. ConfigStore.set('user', currentUser);
  117. const mockDashboard = DashboardFixture([], {
  118. id: '1',
  119. createdBy: UserFixture({id: '1', name: 'Lorem Ipsum'}),
  120. title: 'Custom Errors',
  121. permissions: {isCreatorOnlyEditable: true}, // set to true
  122. });
  123. renderTestComponent(initialData, mockDashboard);
  124. await screen.findByText('Edit Access:');
  125. expect(screen.getByText('LI')).toBeInTheDocument(); // dashboard owner's initials
  126. expect(screen.queryByText('All')).not.toBeInTheDocument();
  127. });
  128. it('disables dropdown options when current user is not dashboard creator', async function () {
  129. const currentUser = UserFixture({id: '781629'});
  130. ConfigStore.set('user', currentUser);
  131. renderTestComponent(initialData);
  132. await userEvent.click(await screen.findByText('Edit Access:'));
  133. // Everyone option should be disabled
  134. expect(await screen.findByRole('option', {name: 'Everyone'})).toHaveAttribute(
  135. 'aria-selected',
  136. 'true'
  137. );
  138. await userEvent.click(screen.getByRole('option', {name: 'Everyone'}));
  139. expect(await screen.findByRole('option', {name: 'Everyone'})).toHaveAttribute(
  140. 'aria-selected',
  141. 'true'
  142. );
  143. });
  144. // [WIP] (Teams based access)
  145. it('renders all teams', async function () {});
  146. it('selects all teams when everyone is selected', async function () {});
  147. it('retains team selection on re-opening selector', async function () {});
  148. it('makes a post request with success message when different teams are selected', async function () {});
  149. });