123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- import {ReactNode} from 'react';
- import {Organization} from 'sentry-fixture/organization';
- import {makeTestQueryClient} from 'sentry-test/queryClient';
- import {reactHooks} from 'sentry-test/reactTestingLibrary';
- import {IssueCategory} from 'sentry/types';
- import {QueryClientProvider} from 'sentry/utils/queryClient';
- import {useLocation} from 'sentry/utils/useLocation';
- import useReplaysCount from './useReplaysCount';
- jest.mock('sentry/utils/useLocation');
- function wrapper({children}: {children?: ReactNode}) {
- return (
- <QueryClientProvider client={makeTestQueryClient()}>{children}</QueryClientProvider>
- );
- }
- describe('useReplaysCount', () => {
- const mockGroupIds = ['123', '456'];
- const mockReplayIds = ['abc', 'def'];
- const mockTransactionNames = ['/home', '/profile'];
- jest.mocked(useLocation).mockReturnValue({
- pathname: '',
- search: '',
- query: {},
- hash: '',
- state: undefined,
- action: 'PUSH',
- key: '',
- });
- const organization = Organization({
- features: ['session-replay'],
- });
- it('should throw if none of groupIds, replayIds, transactionNames is provided', () => {
- const {result} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- },
- });
- expect(result.error).toBeTruthy();
- });
- it('should throw if more than one of groupIds, replayIds, transactionNames are provided', () => {
- const {result: result1} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- groupIds: [],
- transactionNames: [],
- },
- });
- expect(result1.error).toBeTruthy();
- const {result: result2} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- groupIds: [],
- replayIds: [],
- },
- });
- expect(result2.error).toBeTruthy();
- const {result: result3} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- replayIds: [],
- transactionNames: [],
- },
- });
- expect(result3.error).toBeTruthy();
- });
- it('should query for groupIds', async () => {
- const replayCountRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {},
- });
- const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- groupIds: mockGroupIds,
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `issue.id:[${mockGroupIds.join(',')}]`,
- data_source: 'discover',
- statsPeriod: '14d',
- project: -1,
- },
- })
- );
- await waitForNextUpdate();
- });
- it('should query for groupIds on performance issues', async () => {
- const replayCountRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {},
- });
- const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- issueCategory: IssueCategory.PERFORMANCE,
- groupIds: mockGroupIds,
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `issue.id:[${mockGroupIds.join(',')}]`,
- data_source: 'search_issues',
- statsPeriod: '14d',
- project: -1,
- },
- })
- );
- await waitForNextUpdate();
- });
- it('should return the count of each groupId, or zero if not included in the response', async () => {
- const replayCountRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {
- 123: 42,
- },
- });
- const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- groupIds: mockGroupIds,
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalled();
- await waitForNextUpdate();
- expect(result.current).toEqual({
- '123': 42,
- '456': 0,
- });
- });
- it('should request the count for a group only once', async () => {
- const replayCountRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {},
- });
- const {result, rerender, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- groupIds: mockGroupIds,
- },
- });
- await waitForNextUpdate();
- expect(replayCountRequest).toHaveBeenCalledTimes(1);
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `issue.id:[123,456]`,
- data_source: 'discover',
- statsPeriod: '14d',
- project: -1,
- },
- })
- );
- expect(result.current).toEqual({
- 123: 0,
- 456: 0,
- });
- rerender({
- organization,
- groupIds: [...mockGroupIds, '789'],
- });
- await waitForNextUpdate();
- expect(replayCountRequest).toHaveBeenCalledTimes(2);
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `issue.id:[789]`,
- data_source: 'discover',
- statsPeriod: '14d',
- project: -1,
- },
- })
- );
- expect(result.current).toEqual({
- 123: 0,
- 456: 0,
- 789: 0,
- });
- });
- it('should not request anything if there are no new ids to query', async () => {
- const replayCountRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {},
- });
- const {result, rerender, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- groupIds: mockGroupIds,
- },
- });
- await waitForNextUpdate();
- expect(replayCountRequest).toHaveBeenCalledTimes(1);
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `issue.id:[123,456]`,
- data_source: 'discover',
- statsPeriod: '14d',
- project: -1,
- },
- })
- );
- expect(result.current).toEqual({
- 123: 0,
- 456: 0,
- });
- rerender({
- organization,
- groupIds: mockGroupIds,
- });
- expect(replayCountRequest).toHaveBeenCalledTimes(1);
- expect(result.current).toEqual({
- 123: 0,
- 456: 0,
- });
- });
- it('should query for replayId', async () => {
- const replayCountRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {},
- });
- const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- replayIds: mockReplayIds,
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `replay_id:[abc,def]`,
- data_source: 'discover',
- statsPeriod: '14d',
- project: -1,
- },
- })
- );
- await waitForNextUpdate();
- });
- it('should return the count of each replayId, or zero if not included in the response', async () => {
- const countRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {
- abc: 42,
- },
- });
- const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- replayIds: mockReplayIds,
- },
- });
- expect(result.current).toEqual({});
- expect(countRequest).toHaveBeenCalled();
- await waitForNextUpdate();
- expect(result.current).toEqual({
- abc: 42,
- def: 0,
- });
- });
- it('should query for transactionNames', async () => {
- const replayCountRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {},
- });
- const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- transactionNames: mockTransactionNames,
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `transaction:["/home","/profile"]`,
- data_source: 'discover',
- statsPeriod: '14d',
- project: -1,
- },
- })
- );
- await waitForNextUpdate();
- });
- it('should return the count of each transactionName, or zero if not included in the response', async () => {
- const countRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {
- '/home': 42,
- },
- });
- const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- transactionNames: mockTransactionNames,
- },
- });
- expect(result.current).toEqual({});
- expect(countRequest).toHaveBeenCalled();
- await waitForNextUpdate();
- expect(result.current).toEqual({
- '/home': 42,
- '/profile': 0,
- });
- });
- it('should accept start and end times and override statsPeriod', async () => {
- const replayCountRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {},
- });
- const mockDate = new Date(Date.now());
- const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- transactionNames: mockTransactionNames,
- datetime: {
- start: mockDate,
- end: mockDate,
- },
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `transaction:["/home","/profile"]`,
- data_source: 'discover',
- project: -1,
- start: mockDate,
- end: mockDate,
- },
- })
- );
- await waitForNextUpdate();
- });
- it('passes along extra conditions and appends them to the query', async () => {
- const replayCountRequest = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/replay-count/`,
- method: 'GET',
- body: {},
- });
- const {result, waitForNextUpdate} = reactHooks.renderHook(useReplaysCount, {
- wrapper,
- initialProps: {
- organization,
- transactionNames: mockTransactionNames,
- extraConditions: 'transaction.duration>:300ms',
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `transaction:["/home","/profile"] transaction.duration>:300ms`,
- data_source: 'discover',
- project: -1,
- statsPeriod: '14d',
- },
- })
- );
- await waitForNextUpdate();
- });
- });
|