multipleEnvironmentSelector.spec.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import React from 'react';
  2. import {mount} from 'enzyme';
  3. import MultipleEnvironmentSelector from 'app/components/organizations/multipleEnvironmentSelector';
  4. describe('MultipleEnvironmentSelector', function() {
  5. let wrapper;
  6. const onChange = jest.fn();
  7. const onUpdate = jest.fn();
  8. const envs = ['production', 'staging', 'dev'];
  9. const organization = TestStubs.Organization({
  10. projects: [
  11. TestStubs.Project({
  12. id: '1',
  13. slug: 'first',
  14. environments: ['production', 'staging'],
  15. }),
  16. TestStubs.Project({
  17. id: '2',
  18. slug: 'second',
  19. environments: ['dev'],
  20. }),
  21. ],
  22. });
  23. const selectedProjects = [1, 2];
  24. const routerContext = TestStubs.routerContext([
  25. {
  26. organization,
  27. },
  28. ]);
  29. beforeEach(function() {
  30. onChange.mockReset();
  31. onUpdate.mockReset();
  32. wrapper = mount(
  33. <MultipleEnvironmentSelector
  34. organization={organization}
  35. selectedProjects={selectedProjects}
  36. onChange={onChange}
  37. onUpdate={onUpdate}
  38. />,
  39. routerContext
  40. );
  41. });
  42. it('can select and change environments', async function() {
  43. await wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  44. // Select all envs
  45. envs.forEach((env, i) => {
  46. wrapper
  47. .find('EnvironmentSelectorItem')
  48. .at(i)
  49. .find('CheckboxWrapper')
  50. .simulate('click', {});
  51. });
  52. expect(onChange).toHaveBeenCalledTimes(3);
  53. expect(onChange).toHaveBeenLastCalledWith(envs, expect.anything());
  54. wrapper.setProps({value: envs});
  55. wrapper.update();
  56. wrapper
  57. .find('MultipleEnvironmentSelector')
  58. .instance()
  59. .doUpdate();
  60. expect(onUpdate).toHaveBeenCalledWith();
  61. });
  62. it('will call onUpdate when project selection change causes invalid values', async function() {
  63. await wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  64. // Select 'production'
  65. await wrapper
  66. .find('MultipleEnvironmentSelector AutoCompleteItem CheckboxWrapper')
  67. .at(0)
  68. .simulate('click');
  69. await wrapper.update();
  70. // Update project selection so that 'production' is no longer an option.
  71. wrapper.setProps({selectedProjects: [2]});
  72. await wrapper.update();
  73. expect(onChange).toHaveBeenCalled();
  74. const selector = wrapper.find('MultipleEnvironmentSelector').instance();
  75. expect(selector.state.selectedEnvs).toEqual(new Set([]));
  76. });
  77. it('selects multiple environments and uses chevron to update', async function() {
  78. await wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  79. await wrapper
  80. .find('MultipleEnvironmentSelector AutoCompleteItem CheckboxWrapper')
  81. .at(0)
  82. .simulate('click');
  83. expect(onChange).toHaveBeenLastCalledWith(['production'], expect.anything());
  84. wrapper
  85. .find('MultipleEnvironmentSelector AutoCompleteItem CheckboxWrapper')
  86. .at(1)
  87. .simulate('click');
  88. expect(onChange).toHaveBeenLastCalledWith(
  89. ['production', 'staging'],
  90. expect.anything()
  91. );
  92. wrapper.find('MultipleEnvironmentSelector StyledChevron').simulate('click');
  93. expect(onUpdate).toHaveBeenCalledWith();
  94. });
  95. it('does not update when there are no changes', async function() {
  96. await wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  97. wrapper.find('MultipleEnvironmentSelector StyledChevron').simulate('click');
  98. expect(onUpdate).not.toHaveBeenCalled();
  99. });
  100. it('updates environment options when projects selection changes', async function() {
  101. // project 2 only has 1 environment.
  102. wrapper.setProps({selectedProjects: [2]});
  103. wrapper.update();
  104. await wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  105. const items = wrapper.find('MultipleEnvironmentSelector GlobalSelectionHeaderRow');
  106. expect(items.length).toEqual(1);
  107. expect(items.at(0).text()).toBe('dev');
  108. });
  109. it('shows the all environments when there are no projects selected', async function() {
  110. wrapper.setProps({selectedProjects: []});
  111. wrapper.update();
  112. await wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  113. const items = wrapper.find('MultipleEnvironmentSelector GlobalSelectionHeaderRow');
  114. expect(items.length).toEqual(3);
  115. expect(items.at(0).text()).toBe('production');
  116. expect(items.at(1).text()).toBe('staging');
  117. expect(items.at(2).text()).toBe('dev');
  118. });
  119. it('shows the distinct union of environments across all projects', async function() {
  120. wrapper.setProps({selectedProjects: [1, 2]});
  121. await wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
  122. const items = wrapper.find('MultipleEnvironmentSelector GlobalSelectionHeaderRow');
  123. expect(items.length).toEqual(3);
  124. expect(items.at(0).text()).toBe('production');
  125. expect(items.at(1).text()).toBe('staging');
  126. expect(items.at(2).text()).toBe('dev');
  127. });
  128. });