environmentSelector.spec.tsx 9.7 KB

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