projectEnvironments.spec.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import recreateRoute from 'sentry/utils/recreateRoute';
  3. import ProjectEnvironments from 'sentry/views/settings/project/projectEnvironments';
  4. jest.mock('sentry/utils/recreateRoute');
  5. recreateRoute.mockReturnValue('/org-slug/project-slug/settings/environments/');
  6. function renderComponent(isHidden) {
  7. const org = TestStubs.Organization();
  8. const project = TestStubs.Project();
  9. const pathname = isHidden ? 'environments/hidden/' : 'environments/';
  10. return render(
  11. <ProjectEnvironments
  12. params={{
  13. orgId: org.slug,
  14. projectId: project.slug,
  15. }}
  16. location={{pathname}}
  17. routes={[]}
  18. />
  19. );
  20. }
  21. describe('ProjectEnvironments', function () {
  22. let project;
  23. beforeEach(function () {
  24. project = TestStubs.Project({
  25. defaultEnvironment: 'production',
  26. });
  27. MockApiClient.addMockResponse({
  28. url: '/projects/org-slug/project-slug/',
  29. body: project,
  30. });
  31. });
  32. afterEach(function () {
  33. MockApiClient.clearMockResponses();
  34. });
  35. describe('render active', function () {
  36. it('renders empty message', function () {
  37. MockApiClient.addMockResponse({
  38. url: '/projects/org-slug/project-slug/environments/',
  39. body: [],
  40. });
  41. const {container} = renderComponent(false);
  42. expect(
  43. screen.getByText("You don't have any environments yet.")
  44. ).toBeInTheDocument();
  45. expect(container).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. renderComponent(false);
  53. expect(screen.getByText('production')).toBeInTheDocument();
  54. expect(screen.getAllByRole('button', {name: 'Hide'})).toHaveLength(3);
  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 {container} = renderComponent(true);
  64. expect(
  65. screen.getByText("You don't have any hidden environments.")
  66. ).toBeInTheDocument();
  67. expect(container).toSnapshot();
  68. });
  69. it('renders environment list', function () {
  70. MockApiClient.addMockResponse({
  71. url: '/projects/org-slug/project-slug/environments/',
  72. body: TestStubs.Environments(true),
  73. });
  74. const {container} = renderComponent(true);
  75. // Hidden buttons should not have "Set as default"
  76. expect(screen.getByRole('button', {name: 'Show'})).toBeInTheDocument();
  77. expect(container).toSnapshot();
  78. });
  79. });
  80. describe('toggle', function () {
  81. let hideMock, showMock;
  82. const baseUrl = '/projects/org-slug/project-slug/environments/';
  83. beforeEach(function () {
  84. hideMock = MockApiClient.addMockResponse({
  85. url: `${baseUrl}production/`,
  86. method: 'PUT',
  87. });
  88. showMock = MockApiClient.addMockResponse({
  89. url: `${baseUrl}zzz/`,
  90. method: 'PUT',
  91. });
  92. MockApiClient.addMockResponse({
  93. url: baseUrl,
  94. });
  95. });
  96. it('hides', function () {
  97. MockApiClient.addMockResponse({
  98. url: baseUrl,
  99. body: TestStubs.Environments(false),
  100. });
  101. renderComponent(false);
  102. // Click first row 'hide' (production)
  103. //
  104. // XXX(epurkhiser): In the future we should improve the accessability of
  105. // lists, because right now there's no way to associate the hide button
  106. // with it's environment
  107. userEvent.click(screen.getAllByRole('button', {name: 'Hide'})[0]);
  108. expect(hideMock).toHaveBeenCalledWith(
  109. `${baseUrl}production/`,
  110. expect.objectContaining({
  111. data: expect.objectContaining({isHidden: true}),
  112. })
  113. );
  114. });
  115. it('hides names requiring encoding', function () {
  116. MockApiClient.addMockResponse({
  117. url: baseUrl,
  118. body: [{id: '1', name: '%app_env%', isHidden: false}],
  119. });
  120. hideMock = MockApiClient.addMockResponse({
  121. url: `${baseUrl}%25app_env%25/`,
  122. method: 'PUT',
  123. });
  124. renderComponent(false);
  125. userEvent.click(screen.getByRole('button', {name: 'Hide'}));
  126. expect(hideMock).toHaveBeenCalledWith(
  127. `${baseUrl}%25app_env%25/`,
  128. expect.objectContaining({
  129. data: expect.objectContaining({isHidden: true}),
  130. })
  131. );
  132. });
  133. it('shows', function () {
  134. MockApiClient.addMockResponse({
  135. url: baseUrl,
  136. body: TestStubs.Environments(true),
  137. });
  138. renderComponent(true);
  139. userEvent.click(screen.getByRole('button', {name: 'Show'}));
  140. expect(showMock).toHaveBeenCalledWith(
  141. `${baseUrl}zzz/`,
  142. expect.objectContaining({
  143. data: expect.objectContaining({isHidden: false}),
  144. })
  145. );
  146. });
  147. it('does not have "All Environments" rows', function () {
  148. MockApiClient.addMockResponse({
  149. url: baseUrl,
  150. body: TestStubs.Environments(true),
  151. });
  152. renderComponent(true);
  153. expect(screen.queryByText('All Environments')).not.toBeInTheDocument();
  154. });
  155. });
  156. });