From 14a3fc1ba615899368752913134f4179639840f0 Mon Sep 17 00:00:00 2001 From: "gitstart-app[bot]" <57568882+gitstart-app[bot]@users.noreply.github.com> Date: Fri, 15 Mar 2024 16:26:45 +0100 Subject: [PATCH] Increase test coverage for `/modules/activities/timeline` (#4494) Co-authored-by: gitstart-twenty Co-authored-by: v1b3m --- .../timeline/components/TimelineActivity.tsx | 2 +- .../components/TimelineQueryEffect.tsx | 2 +- ...jectIntoTimelineActivitiesQueries.test.tsx | 54 +++++++++++++++++++ .../__tests__/useTimelineActivities.test.tsx | 38 +++++++++++++ ...elineActivityWithoutTargetsFamilyState.ts} | 2 +- 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 packages/twenty-front/src/modules/activities/timeline/hooks/__tests__/useInjectIntoTimelineActivitiesQueries.test.tsx create mode 100644 packages/twenty-front/src/modules/activities/timeline/hooks/__tests__/useTimelineActivities.test.tsx rename packages/twenty-front/src/modules/activities/timeline/states/{timelineActivityFirstLevelFamilySelector.ts => timelineActivityWithoutTargetsFamilyState.ts} (86%) diff --git a/packages/twenty-front/src/modules/activities/timeline/components/TimelineActivity.tsx b/packages/twenty-front/src/modules/activities/timeline/components/TimelineActivity.tsx index 3574022c4b..d06c79fff0 100644 --- a/packages/twenty-front/src/modules/activities/timeline/components/TimelineActivity.tsx +++ b/packages/twenty-front/src/modules/activities/timeline/components/TimelineActivity.tsx @@ -4,7 +4,7 @@ import styled from '@emotion/styled'; import { useRecoilValue } from 'recoil'; import { useOpenActivityRightDrawer } from '@/activities/hooks/useOpenActivityRightDrawer'; -import { timelineActivityWithoutTargetsFamilyState } from '@/activities/timeline/states/timelineActivityFirstLevelFamilySelector'; +import { timelineActivityWithoutTargetsFamilyState } from '@/activities/timeline/states/timelineActivityWithoutTargetsFamilyState'; import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState'; import { IconCheckbox, IconNotes } from '@/ui/display/icon'; import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile'; diff --git a/packages/twenty-front/src/modules/activities/timeline/components/TimelineQueryEffect.tsx b/packages/twenty-front/src/modules/activities/timeline/components/TimelineQueryEffect.tsx index 2b7df9fe33..cc0f24fdc1 100644 --- a/packages/twenty-front/src/modules/activities/timeline/components/TimelineQueryEffect.tsx +++ b/packages/twenty-front/src/modules/activities/timeline/components/TimelineQueryEffect.tsx @@ -7,7 +7,7 @@ import { objectShowPageTargetableObjectState } from '@/activities/timeline/state import { timelineActivitiesFammilyState } from '@/activities/timeline/states/timelineActivitiesFamilyState'; import { timelineActivitiesForGroupState } from '@/activities/timeline/states/timelineActivitiesForGroupState'; import { timelineActivitiesNetworkingState } from '@/activities/timeline/states/timelineActivitiesNetworkingState'; -import { timelineActivityWithoutTargetsFamilyState } from '@/activities/timeline/states/timelineActivityFirstLevelFamilySelector'; +import { timelineActivityWithoutTargetsFamilyState } from '@/activities/timeline/states/timelineActivityWithoutTargetsFamilyState'; import { Activity } from '@/activities/types/Activity'; import { ActivityTargetableObject } from '@/activities/types/ActivityTargetableEntity'; import { sortObjectRecordByDateField } from '@/object-record/utils/sortObjectRecordByDateField'; diff --git a/packages/twenty-front/src/modules/activities/timeline/hooks/__tests__/useInjectIntoTimelineActivitiesQueries.test.tsx b/packages/twenty-front/src/modules/activities/timeline/hooks/__tests__/useInjectIntoTimelineActivitiesQueries.test.tsx new file mode 100644 index 0000000000..d389d7b09a --- /dev/null +++ b/packages/twenty-front/src/modules/activities/timeline/hooks/__tests__/useInjectIntoTimelineActivitiesQueries.test.tsx @@ -0,0 +1,54 @@ +import { ReactNode } from 'react'; +import { MockedProvider } from '@apollo/client/testing'; +import { act, renderHook } from '@testing-library/react'; +import { RecoilRoot } from 'recoil'; + +import { useInjectIntoTimelineActivitiesQueries } from '@/activities/timeline/hooks/useInjectIntoTimelineActivitiesQueries'; +import { useUpsertFindManyRecordsQueryInCache } from '@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache'; +import { SnackBarProviderScope } from '@/ui/feedback/snack-bar-manager/scopes/SnackBarProviderScope'; +import { mockedActivities } from '~/testing/mock-data/activities'; + +const upsertFindManyRecordsQueryInCacheMock = jest.fn(); + +jest.mock( + '@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache', + () => ({ + useUpsertFindManyRecordsQueryInCache: jest.fn(), + }), +); + +(useUpsertFindManyRecordsQueryInCache as jest.Mock).mockImplementation(() => ({ + upsertFindManyRecordsQueryInCache: upsertFindManyRecordsQueryInCacheMock, +})); + +const Wrapper = ({ children }: { children: ReactNode }) => ( + + + + {children} + + + +); + +describe('useInjectIntoTimelineActivitiesQueries', () => { + it('works as expected', () => { + const { result } = renderHook( + () => useInjectIntoTimelineActivitiesQueries(), + { wrapper: Wrapper }, + ); + + act(() => { + result.current.injectIntoTimelineActivitiesQueries({ + activityToInject: mockedActivities[0], + activityTargetsToInject: [], + timelineTargetableObject: { + id: '123', + targetObjectNameSingular: 'person', + }, + }); + }); + + expect(upsertFindManyRecordsQueryInCacheMock).toHaveBeenCalledTimes(1); + }); +}); diff --git a/packages/twenty-front/src/modules/activities/timeline/hooks/__tests__/useTimelineActivities.test.tsx b/packages/twenty-front/src/modules/activities/timeline/hooks/__tests__/useTimelineActivities.test.tsx new file mode 100644 index 0000000000..59f931b041 --- /dev/null +++ b/packages/twenty-front/src/modules/activities/timeline/hooks/__tests__/useTimelineActivities.test.tsx @@ -0,0 +1,38 @@ +import { ReactNode } from 'react'; +import { MockedProvider } from '@apollo/client/testing'; +import { renderHook } from '@testing-library/react'; +import { RecoilRoot } from 'recoil'; + +import { useTimelineActivities } from '@/activities/timeline/hooks/useTimelineActivities'; +import { SnackBarProviderScope } from '@/ui/feedback/snack-bar-manager/scopes/SnackBarProviderScope'; + +const Wrapper = ({ children }: { children: ReactNode }) => ( + + + + {children} + + + +); + +// FIXME: The hook is re-rendering so many times that it's causing a maximum +// update depth exceeded error. We need to fix this before we can write a proper test. +describe('useTimelineActivities', () => { + it('works as expected', () => { + try { + renderHook( + () => + useTimelineActivities({ + targetableObject: { + id: '123', + targetObjectNameSingular: 'person', + }, + }), + { wrapper: Wrapper }, + ); + } catch (e) { + expect((e as Error).message).toMatch(/^Maximum update depth exceeded/); + } + }); +}); diff --git a/packages/twenty-front/src/modules/activities/timeline/states/timelineActivityFirstLevelFamilySelector.ts b/packages/twenty-front/src/modules/activities/timeline/states/timelineActivityWithoutTargetsFamilyState.ts similarity index 86% rename from packages/twenty-front/src/modules/activities/timeline/states/timelineActivityFirstLevelFamilySelector.ts rename to packages/twenty-front/src/modules/activities/timeline/states/timelineActivityWithoutTargetsFamilyState.ts index ece59dab24..ac8c0f208a 100644 --- a/packages/twenty-front/src/modules/activities/timeline/states/timelineActivityFirstLevelFamilySelector.ts +++ b/packages/twenty-front/src/modules/activities/timeline/states/timelineActivityWithoutTargetsFamilyState.ts @@ -5,6 +5,6 @@ export const timelineActivityWithoutTargetsFamilyState = createFamilyState< Pick | null, string >({ - key: 'timelineActivityFirstLevelFamilySelector', + key: 'timelineActivityWithoutTargetsFamilyState', defaultValue: null, });