Add tests for modules/analytics (#3462)

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: KlingerMatheus <klinger.matheus@gitstart.dev>
This commit is contained in:
gitstart-twenty 2024-01-17 15:58:12 +01:00 committed by GitHub
parent 6af2513528
commit adfd1f655d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 86 additions and 0 deletions

View File

@ -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();
});
});
});

View File

@ -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 },
});
});
});