projectEnvironments.spec.jsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import React from 'react';
  2. import {mount} from 'enzyme';
  3. import EnvironmentStore from 'app/stores/environmentStore';
  4. import ProjectEnvironments from 'app/views/projectEnvironments';
  5. import recreateRoute from 'app/utils/recreateRoute';
  6. import {ALL_ENVIRONMENTS_KEY} from 'app/constants';
  7. jest.mock('app/utils/recreateRoute');
  8. recreateRoute.mockReturnValue('/org-slug/project-slug/settings/environments/');
  9. function mountComponent(isHidden) {
  10. const org = TestStubs.Organization();
  11. const project = TestStubs.Project();
  12. let pathname = isHidden ? 'environments/hidden/' : 'environments/';
  13. return mount(
  14. <ProjectEnvironments
  15. params={{
  16. orgId: org.slug,
  17. projectId: project.slug,
  18. }}
  19. location={{pathname}}
  20. routes={[]}
  21. />,
  22. TestStubs.routerContext()
  23. );
  24. }
  25. describe('ProjectEnvironments', function() {
  26. let project;
  27. let updateDefaultMock;
  28. beforeEach(function() {
  29. project = TestStubs.Project({
  30. defaultEnvironment: 'production',
  31. });
  32. MockApiClient.addMockResponse({
  33. url: '/projects/org-slug/project-slug/',
  34. body: project,
  35. });
  36. updateDefaultMock = MockApiClient.addMockResponse({
  37. url: '/projects/org-slug/project-slug/',
  38. method: 'PUT',
  39. });
  40. });
  41. afterEach(function() {
  42. MockApiClient.clearMockResponses();
  43. });
  44. describe('render active', function() {
  45. it('renders empty message', function() {
  46. EnvironmentStore.loadInitialData([]);
  47. const wrapper = mountComponent(false);
  48. let errorMessage = wrapper.find('div').first();
  49. expect(errorMessage.text()).toContain("You don't have any environments yet");
  50. expect(wrapper.find('ProjectEnvironments')).toMatchSnapshot();
  51. });
  52. it('renders environment list and sets staging as default env', async function() {
  53. EnvironmentStore.loadInitialData(TestStubs.Environments(false));
  54. const wrapper = mountComponent(false);
  55. // Production environment is default
  56. const productionRow = wrapper.find('EnvironmentRow[name="production"]');
  57. const stagingRow = wrapper.find('EnvironmentRow[name="staging"]');
  58. expect(productionRow.find('Tag').prop('children')).toBe('Default');
  59. expect(productionRow.find('Button')).toHaveLength(1);
  60. expect(stagingRow.find('Tag')).toHaveLength(0);
  61. expect(
  62. stagingRow
  63. .find('Button')
  64. .first()
  65. .text()
  66. ).toBe('Set as default');
  67. // Can set as default
  68. stagingRow
  69. .find('Button')
  70. .first()
  71. .simulate('click');
  72. expect(updateDefaultMock).toHaveBeenCalledWith(
  73. expect.anything(),
  74. expect.objectContaining({
  75. data: {
  76. defaultEnvironment: 'staging',
  77. },
  78. })
  79. );
  80. expect(wrapper.find('EnvironmentRow[name="staging"] Tag')).toHaveLength(1);
  81. expect(wrapper.find('EnvironmentRow[name="production"] Tag')).toHaveLength(0);
  82. expect(wrapper.find('ProjectEnvironments')).toMatchSnapshot();
  83. });
  84. it('can set "(No Environment)" as default', function() {
  85. EnvironmentStore.loadInitialData([
  86. ...TestStubs.Environments(false),
  87. {
  88. id: 'no-environment-id',
  89. name: '',
  90. displayName: '(No Environment)',
  91. },
  92. ]);
  93. const wrapper = mountComponent(false);
  94. const noEnvironmentsRow = wrapper.find('EnvironmentRow[name=""]');
  95. // Not default
  96. expect(noEnvironmentsRow.find('Tag')).toHaveLength(0);
  97. // Is able to hide
  98. expect(noEnvironmentsRow.find('Button')).toHaveLength(2);
  99. expect(
  100. noEnvironmentsRow
  101. .find('Button')
  102. .first()
  103. .text()
  104. ).toBe('Set as default');
  105. noEnvironmentsRow
  106. .find('Button')
  107. .first()
  108. .simulate('click');
  109. expect(updateDefaultMock).toHaveBeenCalledWith(
  110. expect.anything(),
  111. expect.objectContaining({
  112. data: {
  113. defaultEnvironment: '',
  114. },
  115. })
  116. );
  117. // Is default
  118. expect(wrapper.find('EnvironmentRow[name=""]').find('Tag')).toHaveLength(1);
  119. });
  120. it('can set "All Environments" as default', function() {
  121. EnvironmentStore.loadInitialData(TestStubs.Environments(false));
  122. const wrapper = mountComponent(false);
  123. const allEnvironmentsRow = wrapper.find(
  124. `EnvironmentRow[name="${ALL_ENVIRONMENTS_KEY}"]`
  125. );
  126. // Not default
  127. expect(allEnvironmentsRow.find('Tag')).toHaveLength(0);
  128. // Should not have hide button
  129. expect(allEnvironmentsRow.find('Button')).toHaveLength(1);
  130. expect(allEnvironmentsRow.find('Button').text()).toBe('Set as default');
  131. // Set as default
  132. allEnvironmentsRow.find('Button').simulate('click');
  133. expect(updateDefaultMock).toHaveBeenCalledWith(
  134. expect.anything(),
  135. expect.objectContaining({
  136. data: {
  137. defaultEnvironment: null,
  138. },
  139. })
  140. );
  141. expect(
  142. wrapper.find(`EnvironmentRow[name="${ALL_ENVIRONMENTS_KEY}"]`).find('Tag')
  143. ).toHaveLength(1);
  144. });
  145. it('displays invalid environment in list with no actions', function() {
  146. project = TestStubs.Project({
  147. defaultEnvironment: 'invalid-environment',
  148. });
  149. MockApiClient.addMockResponse({
  150. url: '/projects/org-slug/project-slug/',
  151. body: project,
  152. });
  153. EnvironmentStore.loadInitialData(TestStubs.Environments(false));
  154. const wrapper = mountComponent(false);
  155. const row = wrapper.find('EnvironmentRow[name="invalid-environment"]');
  156. // Is default
  157. expect(row.find('Tag')).toHaveLength(1);
  158. // Can not hide or set as default
  159. expect(row.find('Button')).toHaveLength(0);
  160. expect(wrapper.find('InvalidDefaultEnvironmentIcon')).toHaveLength(1);
  161. });
  162. });
  163. describe('render hidden', function() {
  164. it('renders empty message', function() {
  165. EnvironmentStore.loadHiddenData([]);
  166. const wrapper = mountComponent(true);
  167. let errorMessage = wrapper.find('div').first();
  168. expect(errorMessage.text()).toContain("You don't have any hidden environments");
  169. expect(wrapper.find('ProjectEnvironments')).toMatchSnapshot();
  170. });
  171. it('renders environment list', function() {
  172. EnvironmentStore.loadHiddenData(TestStubs.Environments(true));
  173. const wrapper = mountComponent(true);
  174. // Hidden buttons should not have "Set as default"
  175. expect(wrapper.find('Button').text()).toBe('Show');
  176. expect(wrapper.find('ProjectEnvironments')).toMatchSnapshot();
  177. });
  178. });
  179. describe('toggle', function() {
  180. let hideMock, showMock;
  181. const baseUrl = '/projects/org-slug/project-slug/environments/';
  182. beforeEach(function() {
  183. hideMock = MockApiClient.addMockResponse({
  184. url: `${baseUrl}production/`,
  185. method: 'PUT',
  186. });
  187. showMock = MockApiClient.addMockResponse({
  188. url: `${baseUrl}zzz/`,
  189. method: 'PUT',
  190. });
  191. MockApiClient.addMockResponse({
  192. url: baseUrl,
  193. });
  194. });
  195. it('hides', function() {
  196. EnvironmentStore.loadInitialData(TestStubs.Environments(false));
  197. const wrapper = mountComponent(false);
  198. wrapper.find('EnvironmentRow[name="production"] Button').simulate('click');
  199. expect(hideMock).toHaveBeenCalledWith(
  200. `${baseUrl}production/`,
  201. expect.objectContaining({
  202. data: expect.objectContaining({isHidden: true}),
  203. })
  204. );
  205. });
  206. it('shows', function() {
  207. EnvironmentStore.loadHiddenData(TestStubs.Environments(true));
  208. const wrapper = mountComponent(true);
  209. wrapper.find('EnvironmentRow[name="zzz"] Button').simulate('click');
  210. expect(showMock).toHaveBeenCalledWith(
  211. `${baseUrl}zzz/`,
  212. expect.objectContaining({
  213. data: expect.objectContaining({isHidden: false}),
  214. })
  215. );
  216. });
  217. it('does not have "All Enviroments" rows', function() {
  218. EnvironmentStore.loadHiddenData(TestStubs.Environments(true));
  219. const wrapper = mountComponent(true);
  220. expect(wrapper.find(`EnvironmentRow[name="${ALL_ENVIRONMENTS_KEY}"]`)).toHaveLength(
  221. 0
  222. );
  223. });
  224. });
  225. });