loaderSettings.spec.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import selectEvent from 'react-select-event';
  2. import {ProjectKeys} from 'sentry-fixture/projectKeys';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import {t} from 'sentry/locale';
  6. import {Organization, Project} from 'sentry/types';
  7. import {ProjectKey} from 'sentry/views/settings/project/projectKeys/types';
  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: ProjectKeys()[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. ...TestStubs.ProjectKeys()[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. ...(TestStubs.ProjectKeys()[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. 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('latest'), '7.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: '7.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. ...(TestStubs.ProjectKeys()[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('latest'), '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. ...(TestStubs.ProjectKeys()[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. ...(TestStubs.ProjectKeys()[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. )
  241. ).toBeInTheDocument();
  242. data.dynamicSdkLoaderOptions.hasReplay = false;
  243. rerender(
  244. <LoaderSettings
  245. updateData={jest.fn()}
  246. orgSlug={organization.slug}
  247. keyId={params.keyId}
  248. project={project}
  249. data={data}
  250. />
  251. );
  252. expect(
  253. screen.queryByText(
  254. 'When using Replay, the loader will load the ES6 bundle instead of the ES5 bundle.'
  255. )
  256. ).not.toBeInTheDocument();
  257. });
  258. });