loaderScript.spec.tsx 11 KB

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