processInitQueue.spec.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {TeamFixture} from 'sentry-fixture/team';
  4. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import {processInitQueue} from 'sentry/bootstrap/processInitQueue';
  6. import AlertStore from 'sentry/stores/alertStore';
  7. import IndicatorStore from 'sentry/stores/indicatorStore';
  8. import {SentryInitRenderReactComponent} from 'sentry/types/system';
  9. describe('processInitQueue', function () {
  10. describe('renderReact', function () {
  11. it('renders password strength input', async () => {
  12. window.__onSentryInit = [
  13. {
  14. name: 'passwordStrength',
  15. input: '#password',
  16. element: '#password-strength',
  17. },
  18. ];
  19. render(
  20. <div>
  21. <input id="password" placeholder="password" />
  22. <div id="password-strength" />
  23. </div>
  24. );
  25. processInitQueue();
  26. // Assert that password strength renders and reacts to user input
  27. await userEvent.type(screen.getByPlaceholderText('password'), '!');
  28. expect(await screen.findByText('Very Weak')).toBeInTheDocument();
  29. // Type the rest of the password
  30. await userEvent.type(
  31. screen.getByPlaceholderText('password'),
  32. '!!!!!supersecretpassword!!!!!!'
  33. );
  34. expect(await screen.findByText('Very Strong')).toBeInTheDocument();
  35. });
  36. it('renders indicators', async () => {
  37. window.__onSentryInit = [
  38. {
  39. component: SentryInitRenderReactComponent.INDICATORS,
  40. container: '#indicator-container',
  41. name: 'renderReact',
  42. },
  43. ];
  44. IndicatorStore.add('Indicator Alert', 'success');
  45. render(<div id="indicator-container" />);
  46. processInitQueue();
  47. expect(await screen.findByText('Indicator Alert')).toBeInTheDocument();
  48. });
  49. it('renders system alerts', async () => {
  50. window.__onSentryInit = [
  51. {
  52. component: SentryInitRenderReactComponent.SYSTEM_ALERTS,
  53. container: '#system-alerts-container',
  54. name: 'renderReact',
  55. },
  56. ];
  57. AlertStore.addAlert({
  58. message: 'System Alert',
  59. type: 'success',
  60. });
  61. render(<div id="system-alerts-container" />);
  62. processInitQueue();
  63. expect(await screen.findByText('System Alert')).toBeInTheDocument();
  64. });
  65. it('renders setup wizard', async () => {
  66. window.__onSentryInit = [
  67. {
  68. component: SentryInitRenderReactComponent.SETUP_WIZARD,
  69. container: '#setup-wizard-container',
  70. name: 'renderReact',
  71. props: {
  72. enableProjectSelection: true,
  73. hash: '1',
  74. },
  75. },
  76. ];
  77. MockApiClient.addMockResponse({
  78. url: '/organizations/',
  79. body: [
  80. OrganizationFixture({
  81. id: '1',
  82. slug: 'organization-1',
  83. name: 'Organization 1',
  84. access: [],
  85. features: [],
  86. }),
  87. ],
  88. });
  89. MockApiClient.addMockResponse({
  90. url: '/organizations/organization-1/',
  91. body: OrganizationFixture({
  92. id: '1',
  93. slug: 'organization-1',
  94. name: 'Organization 1',
  95. features: [],
  96. access: [],
  97. }),
  98. });
  99. MockApiClient.addMockResponse({
  100. url: '/organizations/organization-1/projects/',
  101. body: [
  102. ProjectFixture({
  103. id: '1',
  104. slug: 'project-1',
  105. name: 'Project 1',
  106. }),
  107. ],
  108. });
  109. MockApiClient.addMockResponse({
  110. url: '/organizations/organization-1/user-teams/',
  111. body: [TeamFixture({id: '1', slug: 'team-1', name: 'Team 1'})],
  112. });
  113. render(<div id="setup-wizard-container" />);
  114. processInitQueue();
  115. expect(await screen.findByText('Select your Sentry project')).toBeInTheDocument();
  116. });
  117. it('renders u2f sign', async () => {
  118. window.__onSentryInit = [
  119. {
  120. component: SentryInitRenderReactComponent.U2F_SIGN,
  121. container: '#u2f-sign-container',
  122. name: 'renderReact',
  123. props: {
  124. displayMode: 'signin',
  125. },
  126. },
  127. ];
  128. render(<div id="u2f-sign-container" />);
  129. processInitQueue();
  130. // U2F is not supported in the test environment
  131. expect(
  132. await screen.findByText(/Unfortunately your browser does not support U2F/)
  133. ).toBeInTheDocument();
  134. });
  135. it('renders superuser staff access form', async () => {
  136. window.__onSentryInit = [
  137. {
  138. component: SentryInitRenderReactComponent.SU_STAFF_ACCESS_FORM,
  139. container: '#su-staff-access-form-container',
  140. name: 'renderReact',
  141. },
  142. ];
  143. const authenticatorsResponse = MockApiClient.addMockResponse({
  144. url: '/authenticators/',
  145. body: [],
  146. });
  147. render(<div id="su-staff-access-form-container" />);
  148. processInitQueue();
  149. await waitFor(() => {
  150. expect(authenticatorsResponse).toHaveBeenCalled();
  151. });
  152. expect(await screen.findByText('COPS/CSM')).toBeInTheDocument();
  153. });
  154. });
  155. it('processes queued up items', function () {
  156. const mock = jest.fn();
  157. const init = {
  158. name: 'onReady',
  159. onReady: mock,
  160. } as const;
  161. window.__onSentryInit = [init];
  162. processInitQueue();
  163. expect(mock).toHaveBeenCalledTimes(1);
  164. processInitQueue();
  165. expect(mock).toHaveBeenCalledTimes(1);
  166. window.__onSentryInit.push(init);
  167. expect(mock).toHaveBeenCalledTimes(2);
  168. });
  169. it('is called after `processInitQueue` has already run', function () {
  170. processInitQueue();
  171. const mock = jest.fn();
  172. const init = {
  173. name: 'onReady',
  174. onReady: mock,
  175. } as const;
  176. window.__onSentryInit.push(init);
  177. expect(mock).toHaveBeenCalledTimes(1);
  178. processInitQueue();
  179. expect(mock).toHaveBeenCalledTimes(1);
  180. });
  181. });