123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- import {reactHooks} from 'sentry-test/reactTestingLibrary';
- import {useLocation} from 'sentry/utils/useLocation';
- import useReplaysCount from './useReplaysCount';
- jest.mock('sentry/utils/useLocation');
- describe('useReplaysCount', () => {
- const MockUseLocation = useLocation as jest.MockedFunction<typeof useLocation>;
- const mockGroupIds = ['123', '456'];
- const mockReplayIds = ['abc', 'def'];
- const mockTransactionNames = ['/home', '/profile'];
- MockUseLocation.mockReturnValue({
- pathname: '',
- search: '',
- query: {},
- hash: '',
- state: undefined,
- action: 'PUSH',
- key: '',
- });
- const organization = TestStubs.Organization({
- features: ['session-replay'],
- });
- it('should throw if none of groupIds, replayIds, transactionNames is provided', () => {
- const {result} = reactHooks.renderHook(useReplaysCount, {
- 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, {
- initialProps: {
- organization,
- groupIds: [],
- transactionNames: [],
- },
- });
- expect(result1.error).toBeTruthy();
- const {result: result2} = reactHooks.renderHook(useReplaysCount, {
- initialProps: {
- organization,
- groupIds: [],
- replayIds: [],
- },
- });
- expect(result2.error).toBeTruthy();
- const {result: result3} = reactHooks.renderHook(useReplaysCount, {
- 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, {
- initialProps: {
- organization,
- groupIds: mockGroupIds,
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `issue.id:[${mockGroupIds.join(',')}]`,
- 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, {
- 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, {
- 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]`,
- 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]`,
- 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, {
- 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]`,
- 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, {
- initialProps: {
- organization,
- replayIds: mockReplayIds,
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `replay_id:[abc,def]`,
- 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, {
- 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, {
- initialProps: {
- organization,
- transactionNames: mockTransactionNames,
- },
- });
- expect(result.current).toEqual({});
- expect(replayCountRequest).toHaveBeenCalledWith(
- '/organizations/org-slug/replay-count/',
- expect.objectContaining({
- query: {
- query: `transaction:["/home","/profile"]`,
- 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, {
- initialProps: {
- organization,
- transactionNames: mockTransactionNames,
- },
- });
- expect(result.current).toEqual({});
- expect(countRequest).toHaveBeenCalled();
- await waitForNextUpdate();
- expect(result.current).toEqual({
- '/home': 42,
- '/profile': 0,
- });
- });
- });
|