loaderScript.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {
  3. render,
  4. screen,
  5. userEvent,
  6. waitForElementToBeRemoved,
  7. } from 'sentry-test/reactTestingLibrary';
  8. import {t} from 'sentry/locale';
  9. import {Organization, Project, ProjectKey} from 'sentry/types';
  10. import LoaderScript from 'sentry/views/settings/project/loaderScript';
  11. function mockApi({
  12. organization,
  13. project,
  14. projectKeys,
  15. }: {
  16. organization: Organization;
  17. project: Project;
  18. projectKeys: ProjectKey[];
  19. }) {
  20. MockApiClient.clearMockResponses();
  21. MockApiClient.addMockResponse({
  22. url: `/projects/${organization.slug}/${project.slug}/keys/`,
  23. method: 'GET',
  24. body: projectKeys,
  25. });
  26. }
  27. describe('LoaderScript', function () {
  28. it('renders error', async function () {
  29. const {organization, project} = initializeOrg();
  30. MockApiClient.clearMockResponses();
  31. MockApiClient.addMockResponse({
  32. url: `/projects/${organization.slug}/${project.slug}/keys/`,
  33. method: 'GET',
  34. statusCode: 400,
  35. });
  36. render(<LoaderScript project={project} />);
  37. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  38. expect(screen.getByTestId('loading-error')).toHaveTextContent(
  39. 'Failed to load project keys.'
  40. );
  41. });
  42. it('renders empty', async function () {
  43. const {organization, project} = initializeOrg();
  44. mockApi({organization, project, projectKeys: []});
  45. render(<LoaderScript project={project} />);
  46. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  47. expect(
  48. screen.getByText('There are no keys active for this project.')
  49. ).toBeInTheDocument();
  50. });
  51. it('renders for single project', async function () {
  52. const {organization, project} = initializeOrg();
  53. const projectKey = TestStubs.ProjectKeys()[0];
  54. const projectKeys = [projectKey];
  55. mockApi({organization, project, projectKeys});
  56. render(<LoaderScript project={project} />);
  57. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  58. // Loader Script is rendered
  59. expect(screen.getByText(`Client Key: ${projectKey.name}`)).toBeInTheDocument();
  60. const loaderScript = screen.getByRole('textbox', {
  61. name: 'Loader Script',
  62. }) as HTMLInputElement;
  63. const loaderScriptValue = loaderScript.value;
  64. expect(loaderScriptValue).toEqual(expect.stringContaining(projectKeys[0].dsn.cdn));
  65. });
  66. it('renders multiple keys', async function () {
  67. const {organization, project} = initializeOrg();
  68. const projectKeys = TestStubs.ProjectKeys([
  69. {
  70. dsn: {
  71. secret:
  72. 'http://188ee45a58094d939428d8585aa6f662:a33bf9aba64c4bbdaf873bb9023b6d2c@dev.getsentry.net:8000/1',
  73. minidump:
  74. 'http://dev.getsentry.net:8000/api/1/minidump?sentry_key=188ee45a58094d939428d8585aa6f662',
  75. public: 'http://188ee45a58094d939428d8585aa6f662@dev.getsentry.net:8000/1',
  76. csp: 'http://dev.getsentry.net:8000/api/1/csp-report/?sentry_key=188ee45a58094d939428d8585aa6f662',
  77. security:
  78. 'http://dev.getsentry.net:8000/api/1/security-report/?sentry_key=188ee45a58094d939428d8585aa6f662',
  79. },
  80. public: '188ee45a58094d939428d8585aa6f662',
  81. secret: 'a33bf9aba64c4bbdaf873bb9023b6d2c',
  82. name: 'Key 2',
  83. rateLimit: null,
  84. projectId: 1,
  85. dateCreated: '2018-02-28T07:13:51.087Z',
  86. id: '188ee45a58094d939428d8585aa6f662',
  87. isActive: true,
  88. label: 'Key 2',
  89. browserSdkVersion: 'latest',
  90. browserSdk: {
  91. choices: [
  92. ['latest', 'latest'],
  93. ['7.x', '7.x'],
  94. ['6.x', '6.x'],
  95. ['5.x', '5.x'],
  96. ['4.x', '4.x'],
  97. ],
  98. },
  99. dynamicSdkLoaderOptions: {
  100. hasPerformance: false,
  101. hasReplay: false,
  102. hasDebug: false,
  103. },
  104. },
  105. ]);
  106. mockApi({organization, project, projectKeys});
  107. render(<LoaderScript project={project} />);
  108. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  109. expect(screen.getByText(`Client Key: ${projectKeys[0].name}`)).toBeInTheDocument();
  110. expect(screen.getByText(`Client Key: ${projectKeys[1].name}`)).toBeInTheDocument();
  111. const allLoaderScripts = screen.getAllByRole('textbox', {
  112. name: 'Loader Script',
  113. }) as HTMLInputElement[];
  114. expect(allLoaderScripts).toHaveLength(2);
  115. });
  116. it('allows to update key settings', async function () {
  117. const {organization, project} = initializeOrg();
  118. const baseKey = TestStubs.ProjectKeys()[0];
  119. const projectKey = {
  120. ...baseKey,
  121. dynamicSdkLoaderOptions: {
  122. ...baseKey.dynamicSdkLoaderOptions,
  123. hasReplay: true,
  124. },
  125. };
  126. mockApi({organization, project, projectKeys: [projectKey]});
  127. const mockPut = MockApiClient.addMockResponse({
  128. url: `/projects/${organization.slug}/${project.slug}/keys/${projectKey.id}/`,
  129. method: 'PUT',
  130. body: {
  131. ...projectKey,
  132. dynamicSdkLoaderOptions: {
  133. ...projectKey.dynamicSdkLoaderOptions,
  134. hasPerformance: true,
  135. },
  136. },
  137. });
  138. render(<LoaderScript project={project} />);
  139. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  140. expect(screen.getByText(t('Enable Performance Monitoring'))).toBeInTheDocument();
  141. expect(screen.getByText(t('Enable Session Replay'))).toBeInTheDocument();
  142. expect(screen.getByText(t('Enable Debug Bundles & Logging'))).toBeInTheDocument();
  143. let performanceCheckbox = screen.getByRole('checkbox', {
  144. name: t('Enable Performance Monitoring'),
  145. });
  146. expect(performanceCheckbox).toBeEnabled();
  147. expect(performanceCheckbox).not.toBeChecked();
  148. const replayCheckbox = screen.getByRole('checkbox', {
  149. name: t('Enable Session Replay'),
  150. });
  151. expect(replayCheckbox).toBeEnabled();
  152. expect(replayCheckbox).toBeChecked();
  153. const debugCheckbox = screen.getByRole('checkbox', {
  154. name: t('Enable Debug Bundles & Logging'),
  155. });
  156. expect(debugCheckbox).toBeEnabled();
  157. expect(debugCheckbox).not.toBeChecked();
  158. // Toggle performance option
  159. await userEvent.click(
  160. screen.getByRole('checkbox', {
  161. name: t('Enable Performance Monitoring'),
  162. })
  163. );
  164. performanceCheckbox = await screen.findByRole('checkbox', {
  165. name: t('Enable Performance Monitoring'),
  166. checked: true,
  167. });
  168. expect(performanceCheckbox).toBeEnabled();
  169. expect(performanceCheckbox).toBeChecked();
  170. expect(mockPut).toHaveBeenCalledWith(
  171. `/projects/${organization.slug}/${project.slug}/keys/${projectKey.id}/`,
  172. expect.objectContaining({
  173. data: expect.objectContaining({
  174. dynamicSdkLoaderOptions: {
  175. ...projectKey.dynamicSdkLoaderOptions,
  176. hasPerformance: true,
  177. },
  178. }),
  179. })
  180. );
  181. });
  182. it('allows to update one of multiple keys', async function () {
  183. const {organization, project} = initializeOrg();
  184. const projectKeys = TestStubs.ProjectKeys([
  185. {
  186. dsn: {
  187. secret:
  188. 'http://188ee45a58094d939428d8585aa6f662:a33bf9aba64c4bbdaf873bb9023b6d2c@dev.getsentry.net:8000/1',
  189. minidump:
  190. 'http://dev.getsentry.net:8000/api/1/minidump?sentry_key=188ee45a58094d939428d8585aa6f662',
  191. public: 'http://188ee45a58094d939428d8585aa6f662@dev.getsentry.net:8000/1',
  192. csp: 'http://dev.getsentry.net:8000/api/1/csp-report/?sentry_key=188ee45a58094d939428d8585aa6f662',
  193. security:
  194. 'http://dev.getsentry.net:8000/api/1/security-report/?sentry_key=188ee45a58094d939428d8585aa6f662',
  195. },
  196. public: '188ee45a58094d939428d8585aa6f662',
  197. secret: 'a33bf9aba64c4bbdaf873bb9023b6d2c',
  198. name: 'Key 2',
  199. rateLimit: null,
  200. projectId: 1,
  201. dateCreated: '2018-02-28T07:13:51.087Z',
  202. id: '188ee45a58094d939428d8585aa6f662',
  203. isActive: true,
  204. label: 'Key 2',
  205. browserSdkVersion: 'latest',
  206. browserSdk: {
  207. choices: [
  208. ['latest', 'latest'],
  209. ['7.x', '7.x'],
  210. ['6.x', '6.x'],
  211. ['5.x', '5.x'],
  212. ['4.x', '4.x'],
  213. ],
  214. },
  215. dynamicSdkLoaderOptions: {
  216. hasPerformance: false,
  217. hasReplay: false,
  218. hasDebug: false,
  219. },
  220. },
  221. ]);
  222. const projectKey = projectKeys[1];
  223. mockApi({organization, project, projectKeys});
  224. const mockPut = MockApiClient.addMockResponse({
  225. url: `/projects/${organization.slug}/${project.slug}/keys/${projectKey.id}/`,
  226. method: 'PUT',
  227. body: {
  228. ...projectKey,
  229. dynamicSdkLoaderOptions: {
  230. ...projectKey.dynamicSdkLoaderOptions,
  231. hasPerformance: true,
  232. },
  233. },
  234. });
  235. render(<LoaderScript project={project} />);
  236. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  237. expect(
  238. screen.getAllByRole('checkbox', {
  239. name: t('Enable Performance Monitoring'),
  240. checked: false,
  241. })
  242. ).toHaveLength(2);
  243. expect(
  244. screen.getAllByRole('checkbox', {
  245. name: t('Enable Session Replay'),
  246. checked: false,
  247. })
  248. ).toHaveLength(2);
  249. expect(
  250. screen.getAllByRole('checkbox', {
  251. name: t('Enable Debug Bundles & Logging'),
  252. checked: false,
  253. })
  254. ).toHaveLength(2);
  255. // Toggle performance option
  256. await userEvent.click(
  257. screen.getAllByRole('checkbox', {
  258. name: t('Enable Performance Monitoring'),
  259. })[1]
  260. );
  261. expect(
  262. await screen.findByRole('checkbox', {
  263. name: t('Enable Performance Monitoring'),
  264. checked: true,
  265. })
  266. ).toBeInTheDocument();
  267. expect(
  268. screen.getByRole('checkbox', {
  269. name: t('Enable Performance Monitoring'),
  270. checked: false,
  271. })
  272. ).toBeInTheDocument();
  273. expect(
  274. screen.getAllByRole('checkbox', {
  275. name: t('Enable Session Replay'),
  276. checked: false,
  277. })
  278. ).toHaveLength(2);
  279. expect(
  280. screen.getAllByRole('checkbox', {
  281. name: t('Enable Debug Bundles & Logging'),
  282. checked: false,
  283. })
  284. ).toHaveLength(2);
  285. expect(mockPut).toHaveBeenCalledWith(
  286. `/projects/${organization.slug}/${project.slug}/keys/${projectKey.id}/`,
  287. expect.objectContaining({
  288. data: expect.objectContaining({
  289. dynamicSdkLoaderOptions: {
  290. ...projectKey.dynamicSdkLoaderOptions,
  291. hasPerformance: true,
  292. },
  293. }),
  294. })
  295. );
  296. });
  297. });