loaderSettings.spec.tsx 9.2 KB

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