Skip to content

The open alternative to Salesforce, designed for AI.

Add tests for `modules/analytics` (#3462)

gitstart-twentyadfd1f655d99
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 00000000000..fe22734e822
--- /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 }) => (
+ <RecoilRoot>
+ <MockedProvider mocks={mocks} addTypename={false}>
+ {children}
+ </MockedProvider>
+ </RecoilRoot>
+);
+
+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 00000000000..69eeecdf1ca
--- /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 },
+ });
+ });
+});