projectEnvironments.spec.jsx 5.2 KB

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