environmentSelector.spec.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import EnvironmentSelector from 'sentry/components/organizations/environmentSelector';
  3. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. describe('EnvironmentSelector', function () {
  6. const onUpdate = jest.fn();
  7. const projects = [
  8. TestStubs.Project({
  9. id: '1',
  10. slug: 'first',
  11. environments: ['production', 'staging'],
  12. }),
  13. TestStubs.Project({
  14. id: '2',
  15. slug: 'second',
  16. environments: ['dev'],
  17. }),
  18. TestStubs.Project({
  19. id: '3',
  20. slug: 'no member',
  21. environments: ['no-env'],
  22. isMember: false,
  23. }),
  24. ];
  25. const organization = TestStubs.Organization({projects});
  26. const selectedProjects = [1, 2];
  27. const routerContext = TestStubs.routerContext([
  28. {
  29. organization,
  30. },
  31. ]);
  32. beforeEach(function () {
  33. ConfigStore.init();
  34. ConfigStore.loadInitialData(TestStubs.Config());
  35. onUpdate.mockReset();
  36. });
  37. afterEach(() => {
  38. ConfigStore.teardown();
  39. });
  40. const selectorProps = {
  41. organization,
  42. projects,
  43. value: [],
  44. loadingProjects: false,
  45. selectedProjects,
  46. onUpdate,
  47. };
  48. function renderSelector(
  49. props?: Partial<React.ComponentProps<typeof EnvironmentSelector>>
  50. ) {
  51. return render(<EnvironmentSelector {...selectorProps} {...props} />, {
  52. context: routerContext,
  53. });
  54. }
  55. async function clickMenu() {
  56. const button = await screen.findByRole('button', {name: 'All Environments'});
  57. userEvent.click(button);
  58. }
  59. it('can select and change environments', async function () {
  60. renderSelector();
  61. await clickMenu();
  62. screen.queryAllByRole('checkbox').forEach(box => userEvent.click(box));
  63. userEvent.click(await screen.findByLabelText('Apply'));
  64. expect(onUpdate).toHaveBeenCalledWith(['dev', 'production', 'staging']);
  65. });
  66. it('selects multiple environments and uses button to update', async function () {
  67. renderSelector();
  68. await clickMenu();
  69. userEvent.click(screen.queryAllByRole('checkbox')[0]);
  70. expect(onUpdate).not.toHaveBeenCalled();
  71. await clickMenu();
  72. expect(onUpdate).toHaveBeenCalledWith(['dev']);
  73. });
  74. it('does not update when there are no changes', async function () {
  75. renderSelector();
  76. await clickMenu();
  77. // Click and unclick boxes
  78. screen.queryAllByRole('checkbox').forEach(box => userEvent.click(box));
  79. screen.queryAllByRole('checkbox').forEach(box => userEvent.click(box));
  80. await clickMenu();
  81. expect(onUpdate).not.toHaveBeenCalled();
  82. });
  83. it('updates environment options when projects selection changes', async function () {
  84. const {rerender} = renderSelector();
  85. await clickMenu();
  86. screen.queryAllByRole('checkbox').forEach(box => userEvent.click(box));
  87. await clickMenu();
  88. // Changing projects will unselect environments. Project 2 has 1 environment
  89. rerender(<EnvironmentSelector {...selectorProps} selectedProjects={[2]} />);
  90. // There should just be 1 environment now
  91. await clickMenu();
  92. expect(screen.getByLabelText('dev')).toBeInTheDocument();
  93. expect(screen.queryByLabelText('production')).not.toBeInTheDocument();
  94. });
  95. it('shows non-member project environments when selected', async function () {
  96. renderSelector({selectedProjects: [3]});
  97. await clickMenu();
  98. expect(screen.getByRole('checkbox')).toBeInTheDocument();
  99. expect(screen.getByLabelText('no-env')).toBeInTheDocument();
  100. });
  101. it('shows member project environments when there are no projects selected', async function () {
  102. renderSelector({selectedProjects: []});
  103. await clickMenu();
  104. expect(screen.queryAllByRole('checkbox')).toHaveLength(3);
  105. expect(screen.getByLabelText('production')).toBeInTheDocument();
  106. expect(screen.getByLabelText('staging')).toBeInTheDocument();
  107. expect(screen.getByLabelText('dev')).toBeInTheDocument();
  108. });
  109. it('does not open selector menu when disabled', async function () {
  110. renderSelector({disabled: true});
  111. await clickMenu();
  112. // Dropdown not open
  113. expect(screen.queryByRole('checkbox')).not.toBeInTheDocument();
  114. });
  115. describe('Superuser My Projects / all environments', function () {
  116. it('shows env when no team belonging', async function () {
  117. ConfigStore.set('user', {...ConfigStore.get('user'), isSuperuser: true});
  118. renderSelector({
  119. selectedProjects: [],
  120. projects: [
  121. TestStubs.Project({
  122. id: '1',
  123. slug: 'first',
  124. environments: ['production', 'staging'],
  125. isMember: false,
  126. }),
  127. TestStubs.Project({
  128. id: '2',
  129. slug: 'second',
  130. environments: ['dev'],
  131. isMember: false,
  132. }),
  133. ],
  134. });
  135. await clickMenu();
  136. expect(screen.queryAllByRole('checkbox')).toHaveLength(3);
  137. expect(screen.getByLabelText('production')).toBeInTheDocument();
  138. expect(screen.getByLabelText('staging')).toBeInTheDocument();
  139. expect(screen.getByLabelText('dev')).toBeInTheDocument();
  140. });
  141. it('shows env when belongs one team', async function () {
  142. // XXX: Ideally, "My Projects" and "All Projects" should be different if a
  143. // superuser was to belong to at least one project
  144. ConfigStore.set('user', {...ConfigStore.get('user'), isSuperuser: true});
  145. // This user is member of one project
  146. renderSelector({
  147. selectedProjects: [],
  148. projects: [
  149. TestStubs.Project({
  150. id: '1',
  151. slug: 'first',
  152. environments: ['production', 'staging'],
  153. }),
  154. TestStubs.Project({
  155. id: '2',
  156. slug: 'second',
  157. environments: ['dev'],
  158. isMember: false,
  159. }),
  160. ],
  161. });
  162. await clickMenu();
  163. expect(screen.queryAllByRole('checkbox')).toHaveLength(3);
  164. expect(screen.getByLabelText('production')).toBeInTheDocument();
  165. expect(screen.getByLabelText('staging')).toBeInTheDocument();
  166. expect(screen.getByLabelText('dev')).toBeInTheDocument();
  167. });
  168. });
  169. describe('Superuser All Projects / all environments', function () {
  170. it('shows env when no team belonging', async function () {
  171. ConfigStore.set('user', {...ConfigStore.get('user'), isSuperuser: true});
  172. renderSelector({
  173. selectedProjects: [ALL_ACCESS_PROJECTS],
  174. projects: [
  175. TestStubs.Project({
  176. id: '1',
  177. slug: 'first',
  178. environments: ['production', 'staging'],
  179. isMember: false,
  180. }),
  181. TestStubs.Project({
  182. id: '2',
  183. slug: 'second',
  184. environments: ['dev'],
  185. isMember: false,
  186. }),
  187. ],
  188. });
  189. await clickMenu();
  190. expect(screen.queryAllByRole('checkbox')).toHaveLength(3);
  191. expect(screen.getByLabelText('production')).toBeInTheDocument();
  192. expect(screen.getByLabelText('staging')).toBeInTheDocument();
  193. expect(screen.getByLabelText('dev')).toBeInTheDocument();
  194. });
  195. it('shows env when belongs one team', async function () {
  196. // XXX: Ideally, "My Projects" and "All Projects" should be different if a
  197. // superuser was to belong to at least one project
  198. ConfigStore.set('user', {...ConfigStore.get('user'), isSuperuser: true});
  199. renderSelector({
  200. selectedProjects: [ALL_ACCESS_PROJECTS],
  201. projects: [
  202. TestStubs.Project({
  203. id: '1',
  204. slug: 'first',
  205. environments: ['production', 'staging'],
  206. }),
  207. TestStubs.Project({
  208. id: '2',
  209. slug: 'second',
  210. environments: ['dev'],
  211. isMember: false,
  212. }),
  213. ],
  214. });
  215. await clickMenu();
  216. expect(screen.queryAllByRole('checkbox')).toHaveLength(3);
  217. expect(screen.getByLabelText('production')).toBeInTheDocument();
  218. expect(screen.getByLabelText('staging')).toBeInTheDocument();
  219. expect(screen.getByLabelText('dev')).toBeInTheDocument();
  220. });
  221. });
  222. it('shows all project environments when "all projects" is selected', async function () {
  223. renderSelector({selectedProjects: [ALL_ACCESS_PROJECTS]});
  224. await clickMenu();
  225. expect(screen.queryAllByRole('checkbox')).toHaveLength(4);
  226. expect(screen.getByLabelText('production')).toBeInTheDocument();
  227. expect(screen.getByLabelText('staging')).toBeInTheDocument();
  228. expect(screen.getByLabelText('dev')).toBeInTheDocument();
  229. expect(screen.getByLabelText('no-env')).toBeInTheDocument();
  230. });
  231. it('shows the distinct union of environments across all projects', async function () {
  232. renderSelector({selectedProjects: [1, 2]});
  233. await clickMenu();
  234. expect(screen.queryAllByRole('checkbox')).toHaveLength(3);
  235. expect(screen.getByLabelText('production')).toBeInTheDocument();
  236. expect(screen.getByLabelText('staging')).toBeInTheDocument();
  237. expect(screen.getByLabelText('dev')).toBeInTheDocument();
  238. });
  239. it('can quick select an environment', async function () {
  240. renderSelector();
  241. await clickMenu();
  242. // Select something first, we want to make sure that having a changed
  243. // selection doesn't effect the quick select
  244. userEvent.click(screen.getByRole('checkbox', {name: 'dev'}));
  245. // Now 'quick select' the production environment
  246. userEvent.click(screen.getByText('production'));
  247. expect(onUpdate).toHaveBeenCalledTimes(1);
  248. expect(onUpdate).toHaveBeenCalledWith(['production']);
  249. });
  250. });