projectEnvironments.spec.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. renderComponent(false);
  42. expect(
  43. screen.getByText("You don't have any environments yet.")
  44. ).toBeInTheDocument();
  45. });
  46. it('renders environment list', function () {
  47. MockApiClient.addMockResponse({
  48. url: '/projects/org-slug/project-slug/environments/',
  49. body: TestStubs.Environments(),
  50. });
  51. renderComponent(false);
  52. expect(screen.getByText('production')).toBeInTheDocument();
  53. expect(screen.getAllByRole('button', {name: 'Hide'})).toHaveLength(3);
  54. });
  55. });
  56. describe('render hidden', function () {
  57. it('renders empty message', function () {
  58. MockApiClient.addMockResponse({
  59. url: '/projects/org-slug/project-slug/environments/',
  60. body: [],
  61. });
  62. renderComponent(true);
  63. expect(
  64. screen.getByText("You don't have any hidden environments.")
  65. ).toBeInTheDocument();
  66. });
  67. it('renders environment list', function () {
  68. MockApiClient.addMockResponse({
  69. url: '/projects/org-slug/project-slug/environments/',
  70. body: TestStubs.HiddenEnvironments(),
  71. });
  72. renderComponent(true);
  73. // Hidden buttons should not have "Set as default"
  74. expect(screen.getByRole('button', {name: 'Show'})).toBeInTheDocument();
  75. });
  76. });
  77. describe('toggle', function () {
  78. let hideMock: jest.Mock;
  79. let showMock: jest.Mock;
  80. const baseUrl = '/projects/org-slug/project-slug/environments/';
  81. beforeEach(function () {
  82. hideMock = MockApiClient.addMockResponse({
  83. url: `${baseUrl}production/`,
  84. method: 'PUT',
  85. });
  86. showMock = MockApiClient.addMockResponse({
  87. url: `${baseUrl}zzz/`,
  88. method: 'PUT',
  89. });
  90. MockApiClient.addMockResponse({
  91. url: baseUrl,
  92. });
  93. });
  94. it('hides', async function () {
  95. MockApiClient.addMockResponse({
  96. url: baseUrl,
  97. body: TestStubs.Environments(),
  98. });
  99. renderComponent(false);
  100. // Click first row 'hide' (production)
  101. //
  102. // XXX(epurkhiser): In the future we should improve the accessability of
  103. // lists, because right now there's no way to associate the hide button
  104. // with it's environment
  105. await userEvent.click(screen.getAllByRole('button', {name: 'Hide'})[0]);
  106. expect(hideMock).toHaveBeenCalledWith(
  107. `${baseUrl}production/`,
  108. expect.objectContaining({
  109. data: expect.objectContaining({isHidden: true}),
  110. })
  111. );
  112. });
  113. it('hides names requiring encoding', async function () {
  114. MockApiClient.addMockResponse({
  115. url: baseUrl,
  116. body: [{id: '1', name: '%app_env%', isHidden: false}],
  117. });
  118. hideMock = MockApiClient.addMockResponse({
  119. url: `${baseUrl}%25app_env%25/`,
  120. method: 'PUT',
  121. });
  122. renderComponent(false);
  123. await userEvent.click(screen.getByRole('button', {name: 'Hide'}));
  124. expect(hideMock).toHaveBeenCalledWith(
  125. `${baseUrl}%25app_env%25/`,
  126. expect.objectContaining({
  127. data: expect.objectContaining({isHidden: true}),
  128. })
  129. );
  130. });
  131. it('shows', async function () {
  132. MockApiClient.addMockResponse({
  133. url: baseUrl,
  134. body: TestStubs.HiddenEnvironments(),
  135. });
  136. renderComponent(true);
  137. await userEvent.click(screen.getByRole('button', {name: 'Show'}));
  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 Environments" rows', function () {
  146. MockApiClient.addMockResponse({
  147. url: baseUrl,
  148. body: TestStubs.HiddenEnvironments(),
  149. });
  150. renderComponent(true);
  151. expect(screen.queryByText('All Environments')).not.toBeInTheDocument();
  152. });
  153. });
  154. });