projectEnvironments.spec.tsx 5.3 KB

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