projectEnvironments.spec.tsx 5.2 KB

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