environmentSelector.spec.tsx 9.3 KB

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