processInitQueue.spec.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. MockApiClient.addMockResponse({
  114. url: '/organizations/organization-1/teams/',
  115. body: [TeamFixture({id: '1', slug: 'team-1', name: 'Team 1'})],
  116. });
  117. render(<div id="setup-wizard-container" />);
  118. processInitQueue();
  119. await waitFor(
  120. () => {
  121. expect(screen.getByText('Select your Sentry project')).toBeInTheDocument();
  122. },
  123. {timeout: 5000}
  124. );
  125. });
  126. it('renders u2f sign', async () => {
  127. window.__onSentryInit = [
  128. {
  129. component: SentryInitRenderReactComponent.U2F_SIGN,
  130. container: '#u2f-sign-container',
  131. name: 'renderReact',
  132. props: {
  133. displayMode: 'signin',
  134. },
  135. },
  136. ];
  137. render(<div id="u2f-sign-container" />);
  138. processInitQueue();
  139. // U2F is not supported in the test environment
  140. expect(
  141. await screen.findByText(/Unfortunately your browser does not support U2F/)
  142. ).toBeInTheDocument();
  143. });
  144. it('renders superuser staff access form', async () => {
  145. window.__onSentryInit = [
  146. {
  147. component: SentryInitRenderReactComponent.SU_STAFF_ACCESS_FORM,
  148. container: '#su-staff-access-form-container',
  149. name: 'renderReact',
  150. },
  151. ];
  152. const authenticatorsResponse = MockApiClient.addMockResponse({
  153. url: '/authenticators/',
  154. body: [],
  155. });
  156. render(<div id="su-staff-access-form-container" />);
  157. processInitQueue();
  158. await waitFor(() => {
  159. expect(authenticatorsResponse).toHaveBeenCalled();
  160. });
  161. expect(await screen.findByText('COPS/CSM')).toBeInTheDocument();
  162. });
  163. });
  164. it('processes queued up items', function () {
  165. const mock = jest.fn();
  166. const init = {
  167. name: 'onReady',
  168. onReady: mock,
  169. } as const;
  170. window.__onSentryInit = [init];
  171. processInitQueue();
  172. expect(mock).toHaveBeenCalledTimes(1);
  173. processInitQueue();
  174. expect(mock).toHaveBeenCalledTimes(1);
  175. window.__onSentryInit.push(init);
  176. expect(mock).toHaveBeenCalledTimes(2);
  177. });
  178. it('is called after `processInitQueue` has already run', function () {
  179. processInitQueue();
  180. const mock = jest.fn();
  181. const init = {
  182. name: 'onReady',
  183. onReady: mock,
  184. } as const;
  185. window.__onSentryInit.push(init);
  186. expect(mock).toHaveBeenCalledTimes(1);
  187. processInitQueue();
  188. expect(mock).toHaveBeenCalledTimes(1);
  189. });
  190. });