useReplaysCount.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import {ReactNode} from 'react';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {makeTestQueryClient} from 'sentry-test/queryClient';
  4. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  5. import {IssueCategory} from 'sentry/types';
  6. import {QueryClientProvider} from 'sentry/utils/queryClient';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import useReplaysCount from './useReplaysCount';
  9. jest.mock('sentry/utils/useLocation');
  10. function wrapper({children}: {children?: ReactNode}) {
  11. return (
  12. <QueryClientProvider client={makeTestQueryClient()}>{children}</QueryClientProvider>
  13. );
  14. }
  15. describe('useReplaysCount', () => {
  16. const mockGroupIds = ['123', '456'];
  17. const mockReplayIds = ['abc', 'def'];
  18. const mockTransactionNames = ['/home', '/profile'];
  19. jest.mocked(useLocation).mockReturnValue({
  20. pathname: '',
  21. search: '',
  22. query: {},
  23. hash: '',
  24. state: undefined,
  25. action: 'PUSH',
  26. key: '',
  27. });
  28. const organization = Organization({
  29. features: ['session-replay'],
  30. });
  31. it('should throw if none of groupIds, replayIds, transactionNames is provided', () => {
  32. const {result} = reactHooks.renderHook(useReplaysCount, {
  33. wrapper,
  34. initialProps: {
  35. organization,
  36. },
  37. });
  38. expect(result.error).toBeTruthy();
  39. });
  40. it('should throw if more than one of groupIds, replayIds, transactionNames are provided', () => {
  41. const {result: result1} = reactHooks.renderHook(useReplaysCount, {
  42. wrapper,
  43. initialProps: {
  44. organization,
  45. groupIds: [],
  46. transactionNames: [],
  47. },
  48. });
  49. expect(result1.error).toBeTruthy();
  50. const {result: result2} = reactHooks.renderHook(useReplaysCount, {
  51. wrapper,
  52. initialProps: {
  53. organization,
  54. groupIds: [],
  55. replayIds: [],
  56. },
  57. });
  58. expect(result2.error).toBeTruthy();
  59. const {result: result3} = reactHooks.renderHook(useReplaysCount, {
  60. wrapper,
  61. initialProps: {
  62. organization,
  63. replayIds: [],
  64. transactionNames: [],
  65. },
  66. });
  67. expect(result3.error).toBeTruthy();
  68. });
  69. it('should query for groupIds', async () => {
  70. const replayCountRequest = MockApiClient.addMockResponse({
  71. url: `/organizations/${organization.slug}/replay-count/`,
  72. method: 'GET',
  73. body: {},
  74. });
  75. const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  76. wrapper,
  77. initialProps: {
  78. organization,
  79. groupIds: mockGroupIds,
  80. },
  81. });
  82. expect(result.current).toEqual({});
  83. expect(replayCountRequest).toHaveBeenCalledWith(
  84. '/organizations/org-slug/replay-count/',
  85. expect.objectContaining({
  86. query: {
  87. query: `issue.id:[${mockGroupIds.join(',')}]`,
  88. data_source: 'discover',
  89. statsPeriod: '14d',
  90. project: -1,
  91. },
  92. })
  93. );
  94. await waitForNextUpdate();
  95. });
  96. it('should query for groupIds on performance issues', async () => {
  97. const replayCountRequest = MockApiClient.addMockResponse({
  98. url: `/organizations/${organization.slug}/replay-count/`,
  99. method: 'GET',
  100. body: {},
  101. });
  102. const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  103. wrapper,
  104. initialProps: {
  105. organization,
  106. issueCategory: IssueCategory.PERFORMANCE,
  107. groupIds: mockGroupIds,
  108. },
  109. });
  110. expect(result.current).toEqual({});
  111. expect(replayCountRequest).toHaveBeenCalledWith(
  112. '/organizations/org-slug/replay-count/',
  113. expect.objectContaining({
  114. query: {
  115. query: `issue.id:[${mockGroupIds.join(',')}]`,
  116. data_source: 'search_issues',
  117. statsPeriod: '14d',
  118. project: -1,
  119. },
  120. })
  121. );
  122. await waitForNextUpdate();
  123. });
  124. it('should return the count of each groupId, or zero if not included in the response', async () => {
  125. const replayCountRequest = MockApiClient.addMockResponse({
  126. url: `/organizations/${organization.slug}/replay-count/`,
  127. method: 'GET',
  128. body: {
  129. 123: 42,
  130. },
  131. });
  132. const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  133. wrapper,
  134. initialProps: {
  135. organization,
  136. groupIds: mockGroupIds,
  137. },
  138. });
  139. expect(result.current).toEqual({});
  140. expect(replayCountRequest).toHaveBeenCalled();
  141. await waitForNextUpdate();
  142. expect(result.current).toEqual({
  143. '123': 42,
  144. '456': 0,
  145. });
  146. });
  147. it('should request the count for a group only once', async () => {
  148. const replayCountRequest = MockApiClient.addMockResponse({
  149. url: `/organizations/${organization.slug}/replay-count/`,
  150. method: 'GET',
  151. body: {},
  152. });
  153. const {result, rerender, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  154. wrapper,
  155. initialProps: {
  156. organization,
  157. groupIds: mockGroupIds,
  158. },
  159. });
  160. await waitForNextUpdate();
  161. expect(replayCountRequest).toHaveBeenCalledTimes(1);
  162. expect(replayCountRequest).toHaveBeenCalledWith(
  163. '/organizations/org-slug/replay-count/',
  164. expect.objectContaining({
  165. query: {
  166. query: `issue.id:[123,456]`,
  167. data_source: 'discover',
  168. statsPeriod: '14d',
  169. project: -1,
  170. },
  171. })
  172. );
  173. expect(result.current).toEqual({
  174. 123: 0,
  175. 456: 0,
  176. });
  177. rerender({
  178. organization,
  179. groupIds: [...mockGroupIds, '789'],
  180. });
  181. await waitForNextUpdate();
  182. expect(replayCountRequest).toHaveBeenCalledTimes(2);
  183. expect(replayCountRequest).toHaveBeenCalledWith(
  184. '/organizations/org-slug/replay-count/',
  185. expect.objectContaining({
  186. query: {
  187. query: `issue.id:[789]`,
  188. data_source: 'discover',
  189. statsPeriod: '14d',
  190. project: -1,
  191. },
  192. })
  193. );
  194. expect(result.current).toEqual({
  195. 123: 0,
  196. 456: 0,
  197. 789: 0,
  198. });
  199. });
  200. it('should not request anything if there are no new ids to query', async () => {
  201. const replayCountRequest = MockApiClient.addMockResponse({
  202. url: `/organizations/${organization.slug}/replay-count/`,
  203. method: 'GET',
  204. body: {},
  205. });
  206. const {result, rerender, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  207. wrapper,
  208. initialProps: {
  209. organization,
  210. groupIds: mockGroupIds,
  211. },
  212. });
  213. await waitForNextUpdate();
  214. expect(replayCountRequest).toHaveBeenCalledTimes(1);
  215. expect(replayCountRequest).toHaveBeenCalledWith(
  216. '/organizations/org-slug/replay-count/',
  217. expect.objectContaining({
  218. query: {
  219. query: `issue.id:[123,456]`,
  220. data_source: 'discover',
  221. statsPeriod: '14d',
  222. project: -1,
  223. },
  224. })
  225. );
  226. expect(result.current).toEqual({
  227. 123: 0,
  228. 456: 0,
  229. });
  230. rerender({
  231. organization,
  232. groupIds: mockGroupIds,
  233. });
  234. expect(replayCountRequest).toHaveBeenCalledTimes(1);
  235. expect(result.current).toEqual({
  236. 123: 0,
  237. 456: 0,
  238. });
  239. });
  240. it('should query for replayId', async () => {
  241. const replayCountRequest = MockApiClient.addMockResponse({
  242. url: `/organizations/${organization.slug}/replay-count/`,
  243. method: 'GET',
  244. body: {},
  245. });
  246. const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  247. wrapper,
  248. initialProps: {
  249. organization,
  250. replayIds: mockReplayIds,
  251. },
  252. });
  253. expect(result.current).toEqual({});
  254. expect(replayCountRequest).toHaveBeenCalledWith(
  255. '/organizations/org-slug/replay-count/',
  256. expect.objectContaining({
  257. query: {
  258. query: `replay_id:[abc,def]`,
  259. data_source: 'discover',
  260. statsPeriod: '14d',
  261. project: -1,
  262. },
  263. })
  264. );
  265. await waitForNextUpdate();
  266. });
  267. it('should return the count of each replayId, or zero if not included in the response', async () => {
  268. const countRequest = MockApiClient.addMockResponse({
  269. url: `/organizations/${organization.slug}/replay-count/`,
  270. method: 'GET',
  271. body: {
  272. abc: 42,
  273. },
  274. });
  275. const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  276. wrapper,
  277. initialProps: {
  278. organization,
  279. replayIds: mockReplayIds,
  280. },
  281. });
  282. expect(result.current).toEqual({});
  283. expect(countRequest).toHaveBeenCalled();
  284. await waitForNextUpdate();
  285. expect(result.current).toEqual({
  286. abc: 42,
  287. def: 0,
  288. });
  289. });
  290. it('should query for transactionNames', async () => {
  291. const replayCountRequest = MockApiClient.addMockResponse({
  292. url: `/organizations/${organization.slug}/replay-count/`,
  293. method: 'GET',
  294. body: {},
  295. });
  296. const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  297. wrapper,
  298. initialProps: {
  299. organization,
  300. transactionNames: mockTransactionNames,
  301. },
  302. });
  303. expect(result.current).toEqual({});
  304. expect(replayCountRequest).toHaveBeenCalledWith(
  305. '/organizations/org-slug/replay-count/',
  306. expect.objectContaining({
  307. query: {
  308. query: `transaction:["/home","/profile"]`,
  309. data_source: 'discover',
  310. statsPeriod: '14d',
  311. project: -1,
  312. },
  313. })
  314. );
  315. await waitForNextUpdate();
  316. });
  317. it('should return the count of each transactionName, or zero if not included in the response', async () => {
  318. const countRequest = MockApiClient.addMockResponse({
  319. url: `/organizations/${organization.slug}/replay-count/`,
  320. method: 'GET',
  321. body: {
  322. '/home': 42,
  323. },
  324. });
  325. const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  326. wrapper,
  327. initialProps: {
  328. organization,
  329. transactionNames: mockTransactionNames,
  330. },
  331. });
  332. expect(result.current).toEqual({});
  333. expect(countRequest).toHaveBeenCalled();
  334. await waitForNextUpdate();
  335. expect(result.current).toEqual({
  336. '/home': 42,
  337. '/profile': 0,
  338. });
  339. });
  340. it('should accept start and end times and override statsPeriod', async () => {
  341. const replayCountRequest = MockApiClient.addMockResponse({
  342. url: `/organizations/${organization.slug}/replay-count/`,
  343. method: 'GET',
  344. body: {},
  345. });
  346. const mockDate = new Date(Date.now());
  347. const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  348. wrapper,
  349. initialProps: {
  350. organization,
  351. transactionNames: mockTransactionNames,
  352. datetime: {
  353. start: mockDate,
  354. end: mockDate,
  355. },
  356. },
  357. });
  358. expect(result.current).toEqual({});
  359. expect(replayCountRequest).toHaveBeenCalledWith(
  360. '/organizations/org-slug/replay-count/',
  361. expect.objectContaining({
  362. query: {
  363. query: `transaction:["/home","/profile"]`,
  364. data_source: 'discover',
  365. project: -1,
  366. start: mockDate,
  367. end: mockDate,
  368. },
  369. })
  370. );
  371. await waitForNextUpdate();
  372. });
  373. it('passes along extra conditions and appends them to the query', async () => {
  374. const replayCountRequest = MockApiClient.addMockResponse({
  375. url: `/organizations/${organization.slug}/replay-count/`,
  376. method: 'GET',
  377. body: {},
  378. });
  379. const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
  380. wrapper,
  381. initialProps: {
  382. organization,
  383. transactionNames: mockTransactionNames,
  384. extraConditions: 'transaction.duration>:300ms',
  385. },
  386. });
  387. expect(result.current).toEqual({});
  388. expect(replayCountRequest).toHaveBeenCalledWith(
  389. '/organizations/org-slug/replay-count/',
  390. expect.objectContaining({
  391. query: {
  392. query: `transaction:["/home","/profile"] transaction.duration>:300ms`,
  393. data_source: 'discover',
  394. project: -1,
  395. statsPeriod: '14d',
  396. },
  397. })
  398. );
  399. await waitForNextUpdate();
  400. });
  401. });