loaderSettings.spec.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import selectEvent from 'react-select-event';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import {t} from 'sentry/locale';
  5. import {Organization, Project} from 'sentry/types';
  6. import {ProjectKey} from 'sentry/views/settings/project/projectKeys/types';
  7. import {KeySettings} from './keySettings';
  8. import {LoaderSettings} from './loaderSettings';
  9. const dynamicSdkLoaderOptions = {
  10. hasPerformance: false,
  11. hasReplay: true,
  12. hasDebug: false,
  13. };
  14. const fullDynamicSdkLoaderOptions = {
  15. hasPerformance: true,
  16. hasReplay: true,
  17. hasDebug: true,
  18. };
  19. function renderMockRequests(
  20. organizationSlug: Organization['slug'],
  21. projectSlug: Project['slug'],
  22. keyId: ProjectKey['id']
  23. ) {
  24. const projectKeys = MockApiClient.addMockResponse({
  25. url: `/projects/${organizationSlug}/${projectSlug}/keys/${keyId}/`,
  26. method: 'PUT',
  27. body: TestStubs.ProjectKeys()[0],
  28. });
  29. return {projectKeys};
  30. }
  31. describe('Loader Script Settings', function () {
  32. it('renders Loader Script Settings', function () {
  33. const params = {
  34. projectId: '1',
  35. keyId: '1',
  36. };
  37. const {organization, project} = initializeOrg({
  38. ...initializeOrg(),
  39. organization: {
  40. ...initializeOrg().organization,
  41. },
  42. router: {
  43. params,
  44. },
  45. });
  46. const data = {
  47. ...(TestStubs.ProjectKeys()[0] as ProjectKey),
  48. dynamicSdkLoaderOptions,
  49. } as ProjectKey;
  50. const updateData = jest.fn();
  51. render(
  52. <KeySettings
  53. data={data}
  54. updateData={updateData}
  55. onRemove={jest.fn()}
  56. organization={organization}
  57. project={project}
  58. params={params}
  59. />
  60. );
  61. // Panel title
  62. expect(screen.getByText('JavaScript Loader Script')).toBeInTheDocument();
  63. expect(screen.getByText(t('Enable Performance Monitoring'))).toBeInTheDocument();
  64. expect(screen.getByText(t('Enable Session Replay'))).toBeInTheDocument();
  65. expect(screen.getByText(t('Enable Debug Bundles & Logging'))).toBeInTheDocument();
  66. const performanceCheckbox = screen.getByRole('checkbox', {
  67. name: t('Enable Performance Monitoring'),
  68. });
  69. expect(performanceCheckbox).toBeEnabled();
  70. expect(performanceCheckbox).not.toBeChecked();
  71. const replayCheckbox = screen.getByRole('checkbox', {
  72. name: t('Enable Session Replay'),
  73. });
  74. expect(replayCheckbox).toBeEnabled();
  75. expect(replayCheckbox).toBeChecked();
  76. const debugCheckbox = screen.getByRole('checkbox', {
  77. name: t('Enable Debug Bundles & Logging'),
  78. });
  79. expect(debugCheckbox).toBeEnabled();
  80. expect(debugCheckbox).not.toBeChecked();
  81. });
  82. it('allows to toggle options', async function () {
  83. const {organization, project} = initializeOrg();
  84. const params = {
  85. projectSlug: project.slug,
  86. keyId: '1',
  87. };
  88. const data = {
  89. ...(TestStubs.ProjectKeys()[0] as ProjectKey),
  90. dynamicSdkLoaderOptions,
  91. } as ProjectKey;
  92. const mockRequests = renderMockRequests(
  93. organization.slug,
  94. params.projectSlug,
  95. params.keyId
  96. );
  97. const updateData = jest.fn();
  98. render(
  99. <LoaderSettings
  100. orgSlug={organization.slug}
  101. keyId={params.keyId}
  102. project={project}
  103. data={data}
  104. updateData={updateData}
  105. />
  106. );
  107. // Toggle performance option
  108. userEvent.click(
  109. screen.getByRole('checkbox', {
  110. name: t('Enable Performance Monitoring'),
  111. })
  112. );
  113. await waitFor(() => {
  114. expect(mockRequests.projectKeys).toHaveBeenCalledWith(
  115. `/projects/${organization.slug}/${params.projectSlug}/keys/${params.keyId}/`,
  116. expect.objectContaining({
  117. data: expect.objectContaining({
  118. dynamicSdkLoaderOptions: {
  119. ...dynamicSdkLoaderOptions,
  120. hasPerformance: true,
  121. },
  122. }),
  123. })
  124. );
  125. });
  126. // Update SDK version
  127. await selectEvent.select(screen.getByText('latest'), '7.x');
  128. await waitFor(() => {
  129. expect(mockRequests.projectKeys).toHaveBeenCalledWith(
  130. `/projects/${organization.slug}/${params.projectSlug}/keys/${params.keyId}/`,
  131. expect.objectContaining({
  132. data: expect.objectContaining({
  133. browserSdkVersion: '7.x',
  134. }),
  135. })
  136. );
  137. });
  138. });
  139. it('resets performance & replay when selecting SDK version <7', async function () {
  140. const {organization, project} = initializeOrg();
  141. const params = {
  142. projectSlug: project.slug,
  143. keyId: '1',
  144. };
  145. const data = {
  146. ...(TestStubs.ProjectKeys()[0] as ProjectKey),
  147. dynamicSdkLoaderOptions: fullDynamicSdkLoaderOptions,
  148. } as ProjectKey;
  149. const mockRequests = renderMockRequests(
  150. organization.slug,
  151. params.projectSlug,
  152. params.keyId
  153. );
  154. render(
  155. <LoaderSettings
  156. updateData={jest.fn()}
  157. orgSlug={organization.slug}
  158. keyId={params.keyId}
  159. project={project}
  160. data={data}
  161. />
  162. );
  163. // Update SDK version - should reset performance & replay
  164. await selectEvent.select(screen.getByText('latest'), '6.x');
  165. await waitFor(() => {
  166. expect(mockRequests.projectKeys).toHaveBeenCalledWith(
  167. `/projects/${organization.slug}/${params.projectSlug}/keys/${params.keyId}/`,
  168. expect.objectContaining({
  169. data: {
  170. browserSdkVersion: '6.x',
  171. dynamicSdkLoaderOptions: {
  172. hasPerformance: false,
  173. hasReplay: false,
  174. hasDebug: true,
  175. },
  176. },
  177. })
  178. );
  179. });
  180. });
  181. it('disabled performance & replay when SDK version <7 is selected', function () {
  182. const {organization, project} = initializeOrg();
  183. const params = {
  184. projectSlug: project.slug,
  185. keyId: '1',
  186. };
  187. const data = {
  188. ...(TestStubs.ProjectKeys()[0] as ProjectKey),
  189. dynamicSdkLoaderOptions: {
  190. hasPerformance: false,
  191. hasReplay: false,
  192. hasDebug: true,
  193. },
  194. browserSdkVersion: '6.x',
  195. } as ProjectKey;
  196. render(
  197. <LoaderSettings
  198. updateData={jest.fn()}
  199. orgSlug={organization.slug}
  200. keyId={params.keyId}
  201. project={project}
  202. data={data}
  203. />
  204. );
  205. const performanceCheckbox = screen.getByRole('checkbox', {
  206. name: t('Enable Performance Monitoring'),
  207. });
  208. expect(performanceCheckbox).not.toBeChecked();
  209. const replayCheckbox = screen.getByRole('checkbox', {
  210. name: t('Enable Session Replay'),
  211. });
  212. expect(replayCheckbox).not.toBeChecked();
  213. const debugCheckbox = screen.getByRole('checkbox', {
  214. name: t('Enable Debug Bundles & Logging'),
  215. });
  216. expect(debugCheckbox).toBeChecked();
  217. expect(
  218. screen.getAllByText('Only available in SDK version 7.x and above')
  219. ).toHaveLength(2);
  220. });
  221. it('shows replay message when it is enabled', function () {
  222. const {organization, project} = initializeOrg();
  223. const params = {
  224. projectSlug: project.slug,
  225. keyId: '1',
  226. };
  227. const data = {
  228. ...(TestStubs.ProjectKeys()[0] as ProjectKey),
  229. dynamicSdkLoaderOptions: fullDynamicSdkLoaderOptions,
  230. } as ProjectKey;
  231. const {rerender} = render(
  232. <LoaderSettings
  233. updateData={jest.fn()}
  234. orgSlug={organization.slug}
  235. keyId={params.keyId}
  236. project={project}
  237. data={data}
  238. />
  239. );
  240. expect(
  241. screen.getByText(
  242. 'When using Replay, the loader will load the ES6 bundle instead of the ES5 bundle.'
  243. )
  244. ).toBeInTheDocument();
  245. data.dynamicSdkLoaderOptions.hasReplay = false;
  246. rerender(
  247. <LoaderSettings
  248. updateData={jest.fn()}
  249. orgSlug={organization.slug}
  250. keyId={params.keyId}
  251. project={project}
  252. data={data}
  253. />
  254. );
  255. expect(
  256. screen.queryByText(
  257. 'When using Replay, the loader will load the ES6 bundle instead of the ES5 bundle.'
  258. )
  259. ).not.toBeInTheDocument();
  260. });
  261. });