From adfd1f655d999c6bb4a34c1e0a3ee9e86f008658 Mon Sep 17 00:00:00 2001 From: gitstart-twenty <140154534+gitstart-twenty@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:58:12 +0100 Subject: [PATCH] Add tests for `modules/analytics` (#3462) Co-authored-by: gitstart-twenty Co-authored-by: v1b3m Co-authored-by: KlingerMatheus --- .../hooks/__tests__/useEventTracker.test.tsx | 61 +++++++++++++++++++ .../hooks/__tests__/useTrackEvent.test.tsx | 25 ++++++++ 2 files changed, 86 insertions(+) create mode 100644 packages/twenty-front/src/modules/analytics/hooks/__tests__/useEventTracker.test.tsx create mode 100644 packages/twenty-front/src/modules/analytics/hooks/__tests__/useTrackEvent.test.tsx diff --git a/packages/twenty-front/src/modules/analytics/hooks/__tests__/useEventTracker.test.tsx b/packages/twenty-front/src/modules/analytics/hooks/__tests__/useEventTracker.test.tsx new file mode 100644 index 000000000..fe22734e8 --- /dev/null +++ b/packages/twenty-front/src/modules/analytics/hooks/__tests__/useEventTracker.test.tsx @@ -0,0 +1,61 @@ +import { ReactNode } from 'react'; +import { gql } from '@apollo/client'; +import { MockedProvider, MockedResponse } from '@apollo/client/testing'; +import { expect } from '@storybook/test'; +import { act, renderHook, waitFor } from '@testing-library/react'; +import { RecoilRoot } from 'recoil'; + +import { useEventTracker } from '../useEventTracker'; + +jest.mock('@/object-metadata/hooks/useMapFieldMetadataToGraphQLQuery', () => ({ + useMapFieldMetadataToGraphQLQuery: () => () => '\n', +})); + +const mocks: MockedResponse[] = [ + { + request: { + query: gql` + mutation CreateEvent($type: String!, $data: JSON!) { + createEvent(type: $type, data: $data) { + success + } + } + `, + variables: { + type: 'exampleType', + data: { location: { pathname: '/examplePath' } }, + }, + }, + result: jest.fn(() => ({ + data: { + createEvent: { + success: true, + }, + }, + })), + }, +]; + +const Wrapper = ({ children }: { children: ReactNode }) => ( + + + {children} + + +); + +describe('useEventTracker', () => { + it('should make the call to track the event', async () => { + const eventType = 'exampleType'; + const eventData = { location: { pathname: '/examplePath' } }; + const { result } = renderHook(() => useEventTracker(), { + wrapper: Wrapper, + }); + act(() => { + result.current(eventType, eventData); + }); + await waitFor(() => { + expect(mocks[0].result).toHaveBeenCalled(); + }); + }); +}); diff --git a/packages/twenty-front/src/modules/analytics/hooks/__tests__/useTrackEvent.test.tsx b/packages/twenty-front/src/modules/analytics/hooks/__tests__/useTrackEvent.test.tsx new file mode 100644 index 000000000..69eeecdf1 --- /dev/null +++ b/packages/twenty-front/src/modules/analytics/hooks/__tests__/useTrackEvent.test.tsx @@ -0,0 +1,25 @@ +import { expect } from '@storybook/test'; +import { renderHook } from '@testing-library/react'; +import { RecoilRoot } from 'recoil'; + +import { useTrackEvent } from '../useTrackEvent'; + +const mockCreateEventMutation = jest.fn(); + +jest.mock('~/generated/graphql', () => ({ + useCreateEventMutation: () => [mockCreateEventMutation], +})); + +describe('useTrackEvent', () => { + it('should call useEventTracker with the correct arguments', async () => { + const eventType = 'exampleType'; + const eventData = { location: { pathname: '/examplePath' } }; + renderHook(() => useTrackEvent(eventType, eventData), { + wrapper: RecoilRoot, + }); + expect(mockCreateEventMutation).toHaveBeenCalledTimes(1); + expect(mockCreateEventMutation).toHaveBeenCalledWith({ + variables: { type: eventType, data: eventData }, + }); + }); +});