projectEnvironments.spec.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import recreateRoute from 'sentry/utils/recreateRoute';
  4. import ProjectEnvironments from 'sentry/views/settings/project/projectEnvironments';
  5. jest.mock('sentry/utils/recreateRoute');
  6. jest
  7. .mocked(recreateRoute)
  8. .mockReturnValue('/org-slug/project-slug/settings/environments/');
  9. function renderComponent(isHidden: boolean) {
  10. const {organization, project, routerProps} = initializeOrg();
  11. const pathname = isHidden ? 'environments/hidden/' : 'environments/';
  12. return render(
  13. <ProjectEnvironments
  14. {...routerProps}
  15. params={{projectId: project.slug}}
  16. location={TestStubs.location({pathname})}
  17. organization={organization}
  18. project={project}
  19. />
  20. );
  21. }
  22. describe('ProjectEnvironments', function () {
  23. const project = TestStubs.Project({
  24. defaultEnvironment: 'production',
  25. });
  26. beforeEach(function () {
  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(),
  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.HiddenEnvironments(),
  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: jest.Mock;
  82. let showMock: jest.Mock;
  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', async function () {
  98. MockApiClient.addMockResponse({
  99. url: baseUrl,
  100. body: TestStubs.Environments(),
  101. });
  102. renderComponent(false);
  103. // Click first row 'hide' (production)
  104. //
  105. // XXX(epurkhiser): In the future we should improve the accessability of
  106. // lists, because right now there's no way to associate the hide button
  107. // with it's environment
  108. await userEvent.click(screen.getAllByRole('button', {name: 'Hide'})[0]);
  109. expect(hideMock).toHaveBeenCalledWith(
  110. `${baseUrl}production/`,
  111. expect.objectContaining({
  112. data: expect.objectContaining({isHidden: true}),
  113. })
  114. );
  115. });
  116. it('hides names requiring encoding', async function () {
  117. MockApiClient.addMockResponse({
  118. url: baseUrl,
  119. body: [{id: '1', name: '%app_env%', isHidden: false}],
  120. });
  121. hideMock = MockApiClient.addMockResponse({
  122. url: `${baseUrl}%25app_env%25/`,
  123. method: 'PUT',
  124. });
  125. renderComponent(false);
  126. await userEvent.click(screen.getByRole('button', {name: 'Hide'}));
  127. expect(hideMock).toHaveBeenCalledWith(
  128. `${baseUrl}%25app_env%25/`,
  129. expect.objectContaining({
  130. data: expect.objectContaining({isHidden: true}),
  131. })
  132. );
  133. });
  134. it('shows', async function () {
  135. MockApiClient.addMockResponse({
  136. url: baseUrl,
  137. body: TestStubs.HiddenEnvironments(),
  138. });
  139. renderComponent(true);
  140. await userEvent.click(screen.getByRole('button', {name: 'Show'}));
  141. expect(showMock).toHaveBeenCalledWith(
  142. `${baseUrl}zzz/`,
  143. expect.objectContaining({
  144. data: expect.objectContaining({isHidden: false}),
  145. })
  146. );
  147. });
  148. it('does not have "All Environments" rows', function () {
  149. MockApiClient.addMockResponse({
  150. url: baseUrl,
  151. body: TestStubs.HiddenEnvironments(),
  152. });
  153. renderComponent(true);
  154. expect(screen.queryByText('All Environments')).not.toBeInTheDocument();
  155. });
  156. });
  157. });