projectEnvironments.spec.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import React from 'react';
  2. import {mount} from 'enzyme';
  3. import ProjectEnvironments from 'app/views/settings/project/projectEnvironments';
  4. import recreateRoute from 'app/utils/recreateRoute';
  5. import {ALL_ENVIRONMENTS_KEY} from 'app/constants';
  6. jest.mock('app/utils/recreateRoute');
  7. recreateRoute.mockReturnValue('/org-slug/project-slug/settings/environments/');
  8. function mountComponent(isHidden) {
  9. const org = TestStubs.Organization();
  10. const project = TestStubs.Project();
  11. const pathname = isHidden ? 'environments/hidden/' : 'environments/';
  12. return mount(
  13. <ProjectEnvironments
  14. params={{
  15. orgId: org.slug,
  16. projectId: project.slug,
  17. }}
  18. location={{pathname}}
  19. routes={[]}
  20. />,
  21. TestStubs.routerContext()
  22. );
  23. }
  24. describe('ProjectEnvironments', function() {
  25. let project;
  26. beforeEach(function() {
  27. project = TestStubs.Project({
  28. defaultEnvironment: 'production',
  29. });
  30. MockApiClient.addMockResponse({
  31. url: '/projects/org-slug/project-slug/',
  32. body: project,
  33. });
  34. });
  35. afterEach(function() {
  36. MockApiClient.clearMockResponses();
  37. });
  38. describe('render active', function() {
  39. it('renders empty message', function() {
  40. MockApiClient.addMockResponse({
  41. url: '/projects/org-slug/project-slug/environments/',
  42. body: [],
  43. });
  44. const wrapper = mountComponent(false);
  45. const errorMessage = wrapper.find('div').first();
  46. expect(errorMessage.text()).toContain("You don't have any environments yet");
  47. expect(wrapper.find('ProjectEnvironments')).toMatchSnapshot();
  48. });
  49. it('renders environment list', async function() {
  50. MockApiClient.addMockResponse({
  51. url: '/projects/org-slug/project-slug/environments/',
  52. body: TestStubs.Environments(false),
  53. });
  54. const wrapper = mountComponent(false);
  55. const productionRow = wrapper.find('EnvironmentRow[name="production"]');
  56. expect(productionRow.find('Button')).toHaveLength(1);
  57. });
  58. });
  59. describe('render hidden', function() {
  60. it('renders empty message', function() {
  61. MockApiClient.addMockResponse({
  62. url: '/projects/org-slug/project-slug/environments/',
  63. body: [],
  64. });
  65. const wrapper = mountComponent(true);
  66. const errorMessage = wrapper.find('div').first();
  67. expect(errorMessage.text()).toContain("You don't have any hidden environments");
  68. expect(wrapper.find('ProjectEnvironments')).toMatchSnapshot();
  69. });
  70. it('renders environment list', function() {
  71. MockApiClient.addMockResponse({
  72. url: '/projects/org-slug/project-slug/environments/',
  73. body: TestStubs.Environments(true),
  74. });
  75. const wrapper = mountComponent(true);
  76. // Hidden buttons should not have "Set as default"
  77. expect(wrapper.find('Button').text()).toBe('Show');
  78. expect(wrapper.find('ProjectEnvironments')).toMatchSnapshot();
  79. });
  80. });
  81. describe('toggle', function() {
  82. let hideMock, showMock;
  83. const baseUrl = '/projects/org-slug/project-slug/environments/';
  84. beforeEach(function() {
  85. hideMock = MockApiClient.addMockResponse({
  86. url: `${baseUrl}production/`,
  87. method: 'PUT',
  88. });
  89. showMock = MockApiClient.addMockResponse({
  90. url: `${baseUrl}zzz/`,
  91. method: 'PUT',
  92. });
  93. MockApiClient.addMockResponse({
  94. url: baseUrl,
  95. });
  96. });
  97. it('hides', function() {
  98. MockApiClient.addMockResponse({
  99. url: baseUrl,
  100. body: TestStubs.Environments(false),
  101. });
  102. const wrapper = mountComponent(false);
  103. wrapper.find('EnvironmentRow[name="production"] Button').simulate('click');
  104. expect(hideMock).toHaveBeenCalledWith(
  105. `${baseUrl}production/`,
  106. expect.objectContaining({
  107. data: expect.objectContaining({isHidden: true}),
  108. })
  109. );
  110. });
  111. it('hides names requiring encoding', function() {
  112. MockApiClient.addMockResponse({
  113. url: baseUrl,
  114. body: [{id: '1', name: '%app_env%', isHidden: false}],
  115. });
  116. hideMock = MockApiClient.addMockResponse({
  117. url: `${baseUrl}%25app_env%25/`,
  118. method: 'PUT',
  119. });
  120. const wrapper = mountComponent(false);
  121. wrapper
  122. .find('EnvironmentRow[name="%app_env%"] button[aria-label="Hide"]')
  123. .simulate('click');
  124. expect(hideMock).toHaveBeenCalledWith(
  125. `${baseUrl}%25app_env%25/`,
  126. expect.objectContaining({
  127. data: expect.objectContaining({isHidden: true}),
  128. })
  129. );
  130. });
  131. it('shows', function() {
  132. MockApiClient.addMockResponse({
  133. url: baseUrl,
  134. body: TestStubs.Environments(true),
  135. });
  136. const wrapper = mountComponent(true);
  137. wrapper.find('EnvironmentRow[name="zzz"] Button').simulate('click');
  138. expect(showMock).toHaveBeenCalledWith(
  139. `${baseUrl}zzz/`,
  140. expect.objectContaining({
  141. data: expect.objectContaining({isHidden: false}),
  142. })
  143. );
  144. });
  145. it('does not have "All Enviroments" rows', function() {
  146. MockApiClient.addMockResponse({
  147. url: baseUrl,
  148. body: TestStubs.Environments(true),
  149. });
  150. const wrapper = mountComponent(true);
  151. expect(wrapper.find(`EnvironmentRow[name="${ALL_ENVIRONMENTS_KEY}"]`)).toHaveLength(
  152. 0
  153. );
  154. });
  155. });
  156. });