Feat/put target object identifier on use activities (#4682)
When writing to the normalized cache (record), it's crucial to use _refs for relationships to avoid many problems. Essentially, we only deal with level 0 and generate all fields to be comfortable with their defaults. When writing in queries (which should be very rare, the only cases are prefetch and the case of activities due to the nested query; I've reduced this to a single file for activities usePrepareFindManyActivitiesQuery 🙂), it's important to use queryFields to avoid bugs. I've implemented them on the side of query generation and record generation. When doing an updateOne / createOne, etc., it's necessary to distinguish between optimistic writing (which we actually want to do with _refs) and the server response without refs. This allows for a clean write in the optimistic cache without worrying about nesting (as the first point). To simplify the whole activities part, write to the normalized cache first. Then, base queries on it in an idempotent manner. This way, there's no need to worry about the current page or action. The normalized cache is up-to-date, so I update the queries. Same idea as for optimisticEffects, actually. Finally, I've triggered optimisticEffects rather than the manual update of many queries. --------- Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
@ -21,7 +21,6 @@ const mockActivityTarget = {
|
||||
|
||||
const mockActivity = {
|
||||
__typename: 'Activity',
|
||||
activityTargets: [],
|
||||
updatedAt: '2021-08-03T19:20:06.000Z',
|
||||
createdAt: '2021-08-03T19:20:06.000Z',
|
||||
completedAt: '2021-08-03T19:20:06.000Z',
|
||||
@ -29,7 +28,6 @@ const mockActivity = {
|
||||
title: 'title',
|
||||
authorId: '1',
|
||||
body: 'body',
|
||||
comments: [],
|
||||
dueAt: '2021-08-03T19:20:06.000Z',
|
||||
type: 'type',
|
||||
assigneeId: '1',
|
||||
@ -66,9 +64,7 @@ const mocks: MockedResponse[] = [
|
||||
__typename
|
||||
updatedAt
|
||||
createdAt
|
||||
personId
|
||||
activityId
|
||||
companyId
|
||||
id
|
||||
}
|
||||
cursor
|
||||
|
||||
@ -1,80 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider, MockedResponse } from '@apollo/client/testing';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import gql from 'graphql-tag';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useActivityById } from '@/activities/hooks/useActivityById';
|
||||
import { SnackBarProviderScope } from '@/ui/feedback/snack-bar-manager/scopes/SnackBarProviderScope';
|
||||
import { mockedActivities } from '~/testing/mock-data/activities';
|
||||
|
||||
const mocks: MockedResponse[] = [
|
||||
{
|
||||
request: {
|
||||
query: gql`
|
||||
query FindOneActivity($objectRecordId: UUID!) {
|
||||
activity(filter: { id: { eq: $objectRecordId } }) {
|
||||
__typename
|
||||
createdAt
|
||||
reminderAt
|
||||
authorId
|
||||
title
|
||||
completedAt
|
||||
updatedAt
|
||||
body
|
||||
dueAt
|
||||
type
|
||||
id
|
||||
assigneeId
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: { objectRecordId: '1234' },
|
||||
},
|
||||
result: jest.fn(() => ({
|
||||
data: {
|
||||
activity: mockedActivities[0],
|
||||
},
|
||||
})),
|
||||
},
|
||||
];
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider mocks={mocks} addTypename={false}>
|
||||
<SnackBarProviderScope snackBarManagerScopeId="snack-bar-manager">
|
||||
{children}
|
||||
</SnackBarProviderScope>
|
||||
</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
describe('useActivityById', () => {
|
||||
it('works as expected', async () => {
|
||||
const { result } = renderHook(
|
||||
() => useActivityById({ activityId: '1234' }),
|
||||
{ wrapper: Wrapper },
|
||||
);
|
||||
|
||||
expect(result.current.loading).toBe(true);
|
||||
|
||||
await waitFor(() => !result.current.loading);
|
||||
|
||||
expect(result.current.activity).toEqual({
|
||||
__typename: 'Activity',
|
||||
assigneeId: '374fe3a5-df1e-4119-afe0-2a62a2ba481e',
|
||||
authorId: '374fe3a5-df1e-4119-afe0-2a62a2ba481e',
|
||||
body: '',
|
||||
comments: [],
|
||||
completedAt: null,
|
||||
createdAt: '2023-04-26T10:12:42.33625+00:00',
|
||||
activityTargets: [],
|
||||
dueAt: '2023-04-26T10:12:42.33625+00:00',
|
||||
id: '3ecaa1be-aac7-463a-a38e-64078dd451d5',
|
||||
reminderAt: null,
|
||||
title: 'My very first note',
|
||||
type: 'Note',
|
||||
updatedAt: '2023-04-26T10:23:42.33625+00:00',
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,111 +0,0 @@
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useActivityConnectionUtils } from '@/activities/hooks/useActivityConnectionUtils';
|
||||
import { Comment } from '@/activities/types/Comment';
|
||||
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
|
||||
import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock';
|
||||
import { ObjectRecordEdge } from '@/object-record/types/ObjectRecordEdge';
|
||||
|
||||
const mockActivityWithConnectionRelation = {
|
||||
activityTargets: {
|
||||
edges: [
|
||||
{
|
||||
__typename: 'ActivityTargetEdge',
|
||||
node: {
|
||||
id: '20202020-1029-4661-9e91-83bad932bdff',
|
||||
},
|
||||
},
|
||||
],
|
||||
pageInfo: {
|
||||
hasNextPage: false,
|
||||
hasPreviousPage: false,
|
||||
},
|
||||
},
|
||||
comments: {
|
||||
edges: [
|
||||
{
|
||||
__typename: 'CommentEdge',
|
||||
node: {
|
||||
id: '20202020-1029-4661-9e91-83bad932bdee',
|
||||
},
|
||||
},
|
||||
] as ObjectRecordEdge<Comment>[],
|
||||
pageInfo: {
|
||||
hasNextPage: false,
|
||||
hasPreviousPage: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const mockActivityWithArrayRelation = {
|
||||
activityTargets: [
|
||||
{
|
||||
id: '20202020-1029-4661-9e91-83bad932bdff',
|
||||
},
|
||||
],
|
||||
comments: [
|
||||
{
|
||||
id: '20202020-1029-4661-9e91-83bad932bdee',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
describe('useActivityConnectionUtils', () => {
|
||||
it('Should turn activity with connection relation in activity with array relation', async () => {
|
||||
const { result } = renderHook(() => useActivityConnectionUtils(), {
|
||||
wrapper: ({ children }) => (
|
||||
<RecoilRoot
|
||||
initializeState={(snapshot) => {
|
||||
snapshot.set(
|
||||
objectMetadataItemsState,
|
||||
getObjectMetadataItemsMock(),
|
||||
);
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</RecoilRoot>
|
||||
),
|
||||
});
|
||||
|
||||
const { makeActivityWithoutConnection } = result.current;
|
||||
|
||||
const { activity: activityWithArrayRelation } =
|
||||
makeActivityWithoutConnection(mockActivityWithConnectionRelation as any);
|
||||
|
||||
expect(activityWithArrayRelation).toBeDefined();
|
||||
|
||||
expect(activityWithArrayRelation.activityTargets[0].id).toEqual(
|
||||
mockActivityWithArrayRelation.activityTargets[0].id,
|
||||
);
|
||||
});
|
||||
|
||||
it('Should turn activity with connection relation in activity with array relation', async () => {
|
||||
const { result } = renderHook(() => useActivityConnectionUtils(), {
|
||||
wrapper: ({ children }) => (
|
||||
<RecoilRoot
|
||||
initializeState={(snapshot) => {
|
||||
snapshot.set(
|
||||
objectMetadataItemsState,
|
||||
getObjectMetadataItemsMock(),
|
||||
);
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</RecoilRoot>
|
||||
),
|
||||
});
|
||||
|
||||
const { makeActivityWithConnection } = result.current;
|
||||
|
||||
const { activityWithConnection } = makeActivityWithConnection(
|
||||
mockActivityWithArrayRelation as any,
|
||||
);
|
||||
|
||||
expect(activityWithConnection).toBeDefined();
|
||||
|
||||
expect(activityWithConnection.activityTargets.edges[0].node.id).toEqual(
|
||||
mockActivityWithConnectionRelation.activityTargets.edges[0].node.id,
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -1,167 +1,119 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider, MockedResponse } from '@apollo/client/testing';
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import gql from 'graphql-tag';
|
||||
import { gql, InMemoryCache } from '@apollo/client';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot, useSetRecoilState } from 'recoil';
|
||||
|
||||
import { useActivityTargetObjectRecords } from '@/activities/hooks/useActivityTargetObjectRecords';
|
||||
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
|
||||
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
|
||||
import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock';
|
||||
import { getRecordFromRecordNode } from '@/object-record/cache/utils/getRecordFromRecordNode';
|
||||
import { SnackBarProviderScope } from '@/ui/feedback/snack-bar-manager/scopes/SnackBarProviderScope';
|
||||
import { mockedActivities } from '~/testing/mock-data/activities';
|
||||
import { mockedCompaniesData } from '~/testing/mock-data/companies';
|
||||
import { mockedPeopleData } from '~/testing/mock-data/people';
|
||||
import { mockWorkspaceMembers } from '~/testing/mock-data/workspace-members';
|
||||
|
||||
const defaultResponseData = {
|
||||
pageInfo: {
|
||||
hasNextPage: false,
|
||||
startCursor: '',
|
||||
endCursor: '',
|
||||
},
|
||||
totalCount: 1,
|
||||
};
|
||||
|
||||
const mockActivityTarget = {
|
||||
__typename: 'ActivityTarget',
|
||||
updatedAt: '2021-08-03T19:20:06.000Z',
|
||||
createdAt: '2021-08-03T19:20:06.000Z',
|
||||
personId: '1',
|
||||
activityId: '234',
|
||||
companyId: '1',
|
||||
id: '123',
|
||||
person: { ...mockedPeopleData[0], __typename: 'Person', updatedAt: '' },
|
||||
company: { ...mockedCompaniesData[0], __typename: 'Company', updatedAt: '' },
|
||||
activity: mockedActivities[0],
|
||||
};
|
||||
|
||||
const mocks: MockedResponse[] = [
|
||||
{
|
||||
request: {
|
||||
query: gql`
|
||||
query FindManyActivityTargets(
|
||||
$filter: ActivityTargetFilterInput
|
||||
$orderBy: ActivityTargetOrderByInput
|
||||
$lastCursor: String
|
||||
$limit: Float
|
||||
) {
|
||||
activityTargets(
|
||||
filter: $filter
|
||||
orderBy: $orderBy
|
||||
first: $limit
|
||||
after: $lastCursor
|
||||
) {
|
||||
edges {
|
||||
node {
|
||||
__typename
|
||||
updatedAt
|
||||
createdAt
|
||||
company {
|
||||
__typename
|
||||
xLink {
|
||||
label
|
||||
url
|
||||
}
|
||||
linkedinLink {
|
||||
label
|
||||
url
|
||||
}
|
||||
domainName
|
||||
annualRecurringRevenue {
|
||||
amountMicros
|
||||
currencyCode
|
||||
}
|
||||
createdAt
|
||||
address
|
||||
updatedAt
|
||||
name
|
||||
accountOwnerId
|
||||
employees
|
||||
id
|
||||
idealCustomerProfile
|
||||
}
|
||||
personId
|
||||
activityId
|
||||
companyId
|
||||
id
|
||||
activity {
|
||||
__typename
|
||||
createdAt
|
||||
reminderAt
|
||||
authorId
|
||||
title
|
||||
completedAt
|
||||
updatedAt
|
||||
body
|
||||
dueAt
|
||||
type
|
||||
id
|
||||
assigneeId
|
||||
}
|
||||
person {
|
||||
__typename
|
||||
xLink {
|
||||
label
|
||||
url
|
||||
}
|
||||
id
|
||||
createdAt
|
||||
city
|
||||
email
|
||||
jobTitle
|
||||
name {
|
||||
firstName
|
||||
lastName
|
||||
}
|
||||
phone
|
||||
linkedinLink {
|
||||
label
|
||||
url
|
||||
}
|
||||
updatedAt
|
||||
avatarUrl
|
||||
companyId
|
||||
}
|
||||
}
|
||||
cursor
|
||||
}
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
startCursor
|
||||
endCursor
|
||||
}
|
||||
totalCount
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
filter: { activityId: { eq: '1234' } },
|
||||
limit: undefined,
|
||||
orderBy: undefined,
|
||||
},
|
||||
},
|
||||
result: jest.fn(() => ({
|
||||
data: {
|
||||
activityTargets: {
|
||||
...defaultResponseData,
|
||||
edges: [
|
||||
{
|
||||
node: mockActivityTarget,
|
||||
cursor: '1',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
})),
|
||||
},
|
||||
];
|
||||
|
||||
const mockObjectMetadataItems = getObjectMetadataItemsMock();
|
||||
|
||||
const cache = new InMemoryCache();
|
||||
|
||||
const activityNode = {
|
||||
id: '3ecaa1be-aac7-463a-a38e-64078dd451d5',
|
||||
createdAt: '2023-04-26T10:12:42.33625+00:00',
|
||||
updatedAt: '2023-04-26T10:23:42.33625+00:00',
|
||||
reminderAt: null,
|
||||
title: 'My very first note',
|
||||
type: 'Note',
|
||||
body: '',
|
||||
dueAt: '2023-04-26T10:12:42.33625+00:00',
|
||||
completedAt: null,
|
||||
author: null,
|
||||
assignee: null,
|
||||
assigneeId: null,
|
||||
authorId: null,
|
||||
comments: {
|
||||
edges: [],
|
||||
},
|
||||
activityTargets: {
|
||||
edges: [
|
||||
{
|
||||
node: {
|
||||
id: '89bb825c-171e-4bcc-9cf7-43448d6fb300',
|
||||
createdAt: '2023-04-26T10:12:42.33625+00:00',
|
||||
updatedAt: '2023-04-26T10:23:42.33625+00:00',
|
||||
personId: null,
|
||||
companyId: '89bb825c-171e-4bcc-9cf7-43448d6fb280',
|
||||
company: {
|
||||
id: '89bb825c-171e-4bcc-9cf7-43448d6fb280',
|
||||
name: 'Airbnb',
|
||||
domainName: 'airbnb.com',
|
||||
},
|
||||
person: null,
|
||||
activityId: '89bb825c-171e-4bcc-9cf7-43448d6fb230',
|
||||
activity: {
|
||||
id: '89bb825c-171e-4bcc-9cf7-43448d6fb230',
|
||||
createdAt: '2023-04-26T10:12:42.33625+00:00',
|
||||
updatedAt: '2023-04-26T10:23:42.33625+00:00',
|
||||
},
|
||||
__typename: 'ActivityTarget',
|
||||
},
|
||||
__typename: 'ActivityTargetEdge',
|
||||
},
|
||||
],
|
||||
__typename: 'ActivityTargetConnection',
|
||||
},
|
||||
__typename: 'Activity' as const,
|
||||
};
|
||||
|
||||
cache.writeFragment({
|
||||
fragment: gql`
|
||||
fragment CreateOneActivityInCache on Activity {
|
||||
id
|
||||
createdAt
|
||||
updatedAt
|
||||
reminderAt
|
||||
title
|
||||
body
|
||||
dueAt
|
||||
completedAt
|
||||
author
|
||||
assignee
|
||||
assigneeId
|
||||
authorId
|
||||
activityTargets {
|
||||
edges {
|
||||
node {
|
||||
id
|
||||
createdAt
|
||||
updatedAt
|
||||
targetObjectNameSingular
|
||||
personId
|
||||
companyId
|
||||
company {
|
||||
id
|
||||
name
|
||||
domainName
|
||||
}
|
||||
person
|
||||
activityId
|
||||
activity {
|
||||
id
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
__typename
|
||||
}
|
||||
}
|
||||
}
|
||||
__typename
|
||||
}
|
||||
`,
|
||||
id: activityNode.id,
|
||||
data: activityNode,
|
||||
});
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider mocks={mocks} addTypename={false}>
|
||||
<MockedProvider cache={cache}>
|
||||
<SnackBarProviderScope snackBarManagerScopeId="snack-bar-manager">
|
||||
{children}
|
||||
</SnackBarProviderScope>
|
||||
@ -170,19 +122,7 @@ const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
);
|
||||
|
||||
describe('useActivityTargetObjectRecords', () => {
|
||||
it('returns default response', () => {
|
||||
const { result } = renderHook(
|
||||
() => useActivityTargetObjectRecords({ activityId: '1234' }),
|
||||
{ wrapper: Wrapper },
|
||||
);
|
||||
|
||||
expect(result.current).toEqual({
|
||||
activityTargetObjectRecords: [],
|
||||
loadingActivityTargets: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('fetches records', async () => {
|
||||
it('return targetObjects', async () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const setCurrentWorkspaceMember = useSetRecoilState(
|
||||
@ -192,11 +132,12 @@ describe('useActivityTargetObjectRecords', () => {
|
||||
objectMetadataItemsState,
|
||||
);
|
||||
|
||||
const { activityTargetObjectRecords, loadingActivityTargets } =
|
||||
useActivityTargetObjectRecords({ activityId: '1234' });
|
||||
const { activityTargetObjectRecords } = useActivityTargetObjectRecords(
|
||||
getRecordFromRecordNode({ recordNode: activityNode as any }),
|
||||
);
|
||||
|
||||
return {
|
||||
activityTargetObjectRecords,
|
||||
loadingActivityTargets,
|
||||
setCurrentWorkspaceMember,
|
||||
setObjectMetadataItems,
|
||||
};
|
||||
@ -208,16 +149,18 @@ describe('useActivityTargetObjectRecords', () => {
|
||||
result.current.setCurrentWorkspaceMember(mockWorkspaceMembers[0]);
|
||||
result.current.setObjectMetadataItems(mockObjectMetadataItems);
|
||||
});
|
||||
const activityTargetObjectRecords =
|
||||
result.current.activityTargetObjectRecords;
|
||||
|
||||
expect(result.current.loadingActivityTargets).toBe(true);
|
||||
|
||||
// Wait for activityTargets to complete fetching
|
||||
await waitFor(() => !result.current.loadingActivityTargets);
|
||||
|
||||
expect(mocks[0].result).toHaveBeenCalled();
|
||||
expect(result.current.activityTargetObjectRecords).toHaveLength(1);
|
||||
expect(activityTargetObjectRecords).toHaveLength(1);
|
||||
expect(activityTargetObjectRecords[0].activityTarget).toEqual(
|
||||
activityNode.activityTargets.edges[0].node,
|
||||
);
|
||||
expect(activityTargetObjectRecords[0].targetObject).toEqual(
|
||||
activityNode.activityTargets.edges[0].node.company,
|
||||
);
|
||||
expect(
|
||||
result.current.activityTargetObjectRecords[0].targetObjectNameSingular,
|
||||
).toBe('person');
|
||||
activityTargetObjectRecords[0].targetObjectMetadataItem.nameSingular,
|
||||
).toEqual('company');
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,76 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider, MockedResponse } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot, useSetRecoilState } from 'recoil';
|
||||
|
||||
import { useAttachRelationInBothDirections } from '@/activities/hooks/useAttachRelationInBothDirections';
|
||||
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
|
||||
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
|
||||
import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock';
|
||||
import { SnackBarProviderScope } from '@/ui/feedback/snack-bar-manager/scopes/SnackBarProviderScope';
|
||||
import { mockWorkspaceMembers } from '~/testing/mock-data/workspace-members';
|
||||
|
||||
const mocks: MockedResponse[] = [];
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider mocks={mocks} addTypename={false}>
|
||||
<SnackBarProviderScope snackBarManagerScopeId="snack-bar-manager">
|
||||
{children}
|
||||
</SnackBarProviderScope>
|
||||
</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
const mockObjectMetadataItems = getObjectMetadataItemsMock();
|
||||
|
||||
describe('useAttachRelationInBothDirections', () => {
|
||||
it('works as expected', () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const setCurrentWorkspaceMember = useSetRecoilState(
|
||||
currentWorkspaceMemberState,
|
||||
);
|
||||
const setObjectMetadataItems = useSetRecoilState(
|
||||
objectMetadataItemsState,
|
||||
);
|
||||
|
||||
const res = useAttachRelationInBothDirections();
|
||||
return {
|
||||
...res,
|
||||
setCurrentWorkspaceMember,
|
||||
setObjectMetadataItems,
|
||||
};
|
||||
},
|
||||
{ wrapper: Wrapper },
|
||||
);
|
||||
|
||||
act(() => {
|
||||
result.current.setCurrentWorkspaceMember(mockWorkspaceMembers[0]);
|
||||
result.current.setObjectMetadataItems(mockObjectMetadataItems);
|
||||
});
|
||||
const targetRecords = [
|
||||
{ id: '5678', person: { id: '1234' } },
|
||||
{ id: '91011', person: { id: '1234' } },
|
||||
];
|
||||
|
||||
const forEachSpy = jest.spyOn(targetRecords, 'forEach');
|
||||
|
||||
act(() => {
|
||||
result.current.attachRelationInBothDirections({
|
||||
sourceRecord: {
|
||||
id: '1234',
|
||||
company: { id: '5678' },
|
||||
},
|
||||
targetRecords,
|
||||
sourceObjectNameSingular: 'person',
|
||||
targetObjectNameSingular: 'company',
|
||||
fieldNameOnSourceRecord: 'company',
|
||||
fieldNameOnTargetRecord: 'person',
|
||||
});
|
||||
});
|
||||
|
||||
// expect forEach to have been called on targetRecords
|
||||
expect(forEachSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@ -1,53 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import pick from 'lodash.pick';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useDeleteActivityFromCache } from '@/activities/hooks/useDeleteActivityFromCache';
|
||||
import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect';
|
||||
import { mockedActivities } from '~/testing/mock-data/activities';
|
||||
|
||||
const triggerDeleteRecordsOptimisticEffectMock = jest.fn();
|
||||
|
||||
// mock the triggerDeleteRecordsOptimisticEffect function
|
||||
jest.mock(
|
||||
'@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect',
|
||||
() => ({
|
||||
triggerDeleteRecordsOptimisticEffect: jest.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
(triggerDeleteRecordsOptimisticEffect as jest.Mock).mockImplementation(
|
||||
triggerDeleteRecordsOptimisticEffectMock,
|
||||
);
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider addTypename={false}>{children}</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
describe('useDeleteActivityFromCache', () => {
|
||||
it('works as expected', () => {
|
||||
const { result } = renderHook(() => useDeleteActivityFromCache(), {
|
||||
wrapper: Wrapper,
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current.deleteActivityFromCache(
|
||||
pick(mockedActivities[0], [
|
||||
'id',
|
||||
'title',
|
||||
'body',
|
||||
'type',
|
||||
'completedAt',
|
||||
'dueAt',
|
||||
'updatedAt',
|
||||
]),
|
||||
);
|
||||
|
||||
expect(triggerDeleteRecordsOptimisticEffectMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,60 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useInjectIntoActivitiesQueries } from '@/activities/hooks/useInjectIntoActivitiesQueries';
|
||||
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 }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider addTypename={false}>
|
||||
<SnackBarProviderScope snackBarManagerScopeId="snack-bar-manager">
|
||||
{children}
|
||||
</SnackBarProviderScope>
|
||||
</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
describe('useInjectIntoActivitiesQueries', () => {
|
||||
it('works as expected', () => {
|
||||
const { result } = renderHook(() => useInjectIntoActivitiesQueries(), {
|
||||
wrapper: Wrapper,
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current.injectActivitiesQueries({
|
||||
activityToInject: mockedActivities[0],
|
||||
activityTargetsToInject: [],
|
||||
targetableObjects: [{ id: '123', targetObjectNameSingular: 'person' }],
|
||||
});
|
||||
|
||||
expect(upsertFindManyRecordsQueryInCacheMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current.injectActivitiesQueries({
|
||||
activityToInject: mockedActivities[0],
|
||||
activityTargetsToInject: [],
|
||||
targetableObjects: [],
|
||||
});
|
||||
|
||||
expect(upsertFindManyRecordsQueryInCacheMock).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,64 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useInjectIntoActivityTargetsQueries } from '@/activities/hooks/useInjectIntoActivityTargetsQueries';
|
||||
import { useUpsertFindManyRecordsQueryInCache } from '@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache';
|
||||
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 mockActivityTarget = {
|
||||
__typename: 'ActivityTarget',
|
||||
updatedAt: '2021-08-03T19:20:06.000Z',
|
||||
createdAt: '2021-08-03T19:20:06.000Z',
|
||||
personId: '1',
|
||||
activityId: '234',
|
||||
companyId: '1',
|
||||
id: '123',
|
||||
activity: mockedActivities[0],
|
||||
};
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider addTypename={false}>{children}</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
describe('useInjectIntoActivityTargetsQueries', () => {
|
||||
it('works as expected', () => {
|
||||
const { result } = renderHook(() => useInjectIntoActivityTargetsQueries(), {
|
||||
wrapper: Wrapper,
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current.injectActivityTargetsQueries({
|
||||
activityTargetsToInject: [mockActivityTarget],
|
||||
targetableObjects: [{ id: '123', targetObjectNameSingular: 'person' }],
|
||||
});
|
||||
|
||||
expect(upsertFindManyRecordsQueryInCacheMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current.injectActivityTargetsQueries({
|
||||
activityTargetsToInject: [mockActivityTarget],
|
||||
targetableObjects: [],
|
||||
});
|
||||
|
||||
expect(upsertFindManyRecordsQueryInCacheMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,44 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useModifyActivityOnActivityTargetsCache } from '@/activities/hooks/useModifyActivityOnActivityTargetCache';
|
||||
import { useModifyRecordFromCache } from '@/object-record/cache/hooks/useModifyRecordFromCache';
|
||||
import { mockedActivities } from '~/testing/mock-data/activities';
|
||||
|
||||
const useModifyRecordFromCacheMock = jest.fn();
|
||||
|
||||
jest.mock('@/object-record/cache/hooks/useModifyRecordFromCache', () => ({
|
||||
useModifyRecordFromCache: jest.fn(),
|
||||
}));
|
||||
|
||||
(useModifyRecordFromCache as jest.Mock).mockImplementation(
|
||||
() => useModifyRecordFromCacheMock,
|
||||
);
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider addTypename={false}>{children}</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
describe('useModifyActivityOnActivityTargetsCache', () => {
|
||||
it('works as expected', () => {
|
||||
const { result } = renderHook(
|
||||
() => useModifyActivityOnActivityTargetsCache(),
|
||||
{
|
||||
wrapper: Wrapper,
|
||||
},
|
||||
);
|
||||
|
||||
act(() => {
|
||||
result.current.modifyActivityOnActivityTargetsCache({
|
||||
activity: mockedActivities[0],
|
||||
activityTargetIds: ['123', '456'],
|
||||
});
|
||||
});
|
||||
|
||||
expect(useModifyRecordFromCacheMock).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@ -1,43 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useModifyActivityTargetsOnActivityCache } from '@/activities/hooks/useModifyActivityTargetsOnActivityCache';
|
||||
import { useModifyRecordFromCache } from '@/object-record/cache/hooks/useModifyRecordFromCache';
|
||||
|
||||
const useModifyRecordFromCacheMock = jest.fn();
|
||||
|
||||
jest.mock('@/object-record/cache/hooks/useModifyRecordFromCache', () => ({
|
||||
useModifyRecordFromCache: jest.fn(),
|
||||
}));
|
||||
|
||||
(useModifyRecordFromCache as jest.Mock).mockImplementation(
|
||||
() => useModifyRecordFromCacheMock,
|
||||
);
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider addTypename={false}>{children}</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
describe('useModifyActivityTargetsOnActivityCache', () => {
|
||||
it('works as expected', () => {
|
||||
const { result } = renderHook(
|
||||
() => useModifyActivityTargetsOnActivityCache(),
|
||||
{
|
||||
wrapper: Wrapper,
|
||||
},
|
||||
);
|
||||
|
||||
act(() => {
|
||||
result.current.modifyActivityTargetsOnActivityCache({
|
||||
activityId: '1234',
|
||||
activityTargets: [],
|
||||
});
|
||||
});
|
||||
|
||||
expect(useModifyRecordFromCacheMock).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@ -1,110 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot, useRecoilValue, useSetRecoilState } from 'recoil';
|
||||
|
||||
import { useOpenCreateActivityDrawer } from '@/activities/hooks/useOpenCreateActivityDrawer';
|
||||
import { useOpenCreateActivityDrawerForSelectedRowIds } from '@/activities/hooks/useOpenCreateActivityDrawerForSelectedRowIds';
|
||||
import { activityIdInDrawerState } from '@/activities/states/activityIdInDrawerState';
|
||||
import { viewableActivityIdState } from '@/activities/states/viewableActivityIdState';
|
||||
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
|
||||
import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock';
|
||||
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
|
||||
import { isRowSelectedComponentFamilyState } from '@/object-record/record-table/record-table-row/states/isRowSelectedComponentFamilyState';
|
||||
import { tableRowIdsComponentState } from '@/object-record/record-table/states/tableRowIdsComponentState';
|
||||
|
||||
const useOpenCreateActivityDrawerMock = jest.fn();
|
||||
jest.mock('@/activities/hooks/useOpenCreateActivityDrawer', () => ({
|
||||
useOpenCreateActivityDrawer: jest.fn(),
|
||||
}));
|
||||
|
||||
(useOpenCreateActivityDrawer as jest.Mock).mockImplementation(
|
||||
() => useOpenCreateActivityDrawerMock,
|
||||
);
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider addTypename={false}>{children}</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
const mockObjectMetadataItems = getObjectMetadataItemsMock();
|
||||
const recordTableId = 'recordTableId';
|
||||
const tableRowIds = ['123', '456'];
|
||||
const recordObject = {
|
||||
id: '789',
|
||||
};
|
||||
|
||||
describe('useOpenCreateActivityDrawerForSelectedRowIds', () => {
|
||||
it('works as expected', async () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const openCreateActivityDrawerForSelectedRowIds =
|
||||
useOpenCreateActivityDrawerForSelectedRowIds(recordTableId);
|
||||
const viewableActivityId = useRecoilValue(viewableActivityIdState);
|
||||
const activityIdInDrawer = useRecoilValue(activityIdInDrawerState);
|
||||
const setObjectMetadataItems = useSetRecoilState(
|
||||
objectMetadataItemsState,
|
||||
);
|
||||
const scopeId = `${recordTableId}-scope`;
|
||||
const setTableRowIds = useSetRecoilState(
|
||||
tableRowIdsComponentState({ scopeId }),
|
||||
);
|
||||
const setIsRowSelectedComponentFamilyState = useSetRecoilState(
|
||||
isRowSelectedComponentFamilyState({
|
||||
scopeId,
|
||||
familyKey: tableRowIds[0],
|
||||
}),
|
||||
);
|
||||
const setRecordStoreFamilyState = useSetRecoilState(
|
||||
recordStoreFamilyState(tableRowIds[0]),
|
||||
);
|
||||
return {
|
||||
openCreateActivityDrawerForSelectedRowIds,
|
||||
activityIdInDrawer,
|
||||
viewableActivityId,
|
||||
setObjectMetadataItems,
|
||||
setTableRowIds,
|
||||
setIsRowSelectedComponentFamilyState,
|
||||
setRecordStoreFamilyState,
|
||||
};
|
||||
},
|
||||
{
|
||||
wrapper: Wrapper,
|
||||
},
|
||||
);
|
||||
|
||||
act(() => {
|
||||
result.current.setTableRowIds(tableRowIds);
|
||||
result.current.setRecordStoreFamilyState(recordObject);
|
||||
result.current.setIsRowSelectedComponentFamilyState(true);
|
||||
result.current.setObjectMetadataItems(mockObjectMetadataItems);
|
||||
});
|
||||
|
||||
expect(result.current.activityIdInDrawer).toBeNull();
|
||||
expect(result.current.viewableActivityId).toBeNull();
|
||||
await act(async () => {
|
||||
result.current.openCreateActivityDrawerForSelectedRowIds(
|
||||
'Note',
|
||||
'person',
|
||||
[{ id: '176', targetObjectNameSingular: 'person' }],
|
||||
);
|
||||
});
|
||||
|
||||
expect(useOpenCreateActivityDrawerMock).toHaveBeenCalledWith({
|
||||
type: 'Note',
|
||||
targetableObjects: [
|
||||
{
|
||||
type: 'Custom',
|
||||
targetObjectNameSingular: 'person',
|
||||
id: '123',
|
||||
targetObjectRecord: { id: '789' },
|
||||
},
|
||||
{
|
||||
id: '176',
|
||||
targetObjectNameSingular: 'person',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,63 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useRemoveFromActivitiesQueries } from '@/activities/hooks/useRemoveFromActivitiesQueries';
|
||||
import { useReadFindManyRecordsQueryInCache } from '@/object-record/cache/hooks/useReadFindManyRecordsQueryInCache';
|
||||
import { useUpsertFindManyRecordsQueryInCache } from '@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache';
|
||||
|
||||
const upsertFindManyRecordsQueryInCacheMock = jest.fn();
|
||||
const useReadFindManyRecordsQueryInCacheMock = jest.fn(() => [
|
||||
{ activityId: '981' },
|
||||
{ activityId: '345' },
|
||||
]);
|
||||
jest.mock(
|
||||
'@/object-record/cache/hooks/useReadFindManyRecordsQueryInCache',
|
||||
() => ({
|
||||
useReadFindManyRecordsQueryInCache: jest.fn(),
|
||||
}),
|
||||
);
|
||||
jest.mock(
|
||||
'@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache',
|
||||
() => ({
|
||||
useUpsertFindManyRecordsQueryInCache: jest.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
(useReadFindManyRecordsQueryInCache as jest.Mock).mockImplementation(() => ({
|
||||
readFindManyRecordsQueryInCache: useReadFindManyRecordsQueryInCacheMock,
|
||||
}));
|
||||
|
||||
(useUpsertFindManyRecordsQueryInCache as jest.Mock).mockImplementation(() => ({
|
||||
upsertFindManyRecordsQueryInCache: upsertFindManyRecordsQueryInCacheMock,
|
||||
}));
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider addTypename={false}>{children}</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
describe('useRemoveFromActivitiesQueries', () => {
|
||||
it('works as expected', () => {
|
||||
const { result } = renderHook(() => useRemoveFromActivitiesQueries(), {
|
||||
wrapper: Wrapper,
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current.removeFromActivitiesQueries({
|
||||
activityIdToRemove: '123',
|
||||
targetableObjects: [],
|
||||
});
|
||||
});
|
||||
|
||||
expect(upsertFindManyRecordsQueryInCacheMock).toHaveBeenCalledWith({
|
||||
objectRecordsToOverwrite: [{ activityId: '981' }, { activityId: '345' }],
|
||||
queryVariables: {
|
||||
filter: { id: { in: ['345', '981'] } },
|
||||
orderBy: undefined,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,72 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useRemoveFromActivityTargetsQueries } from '@/activities/hooks/useRemoveFromActivityTargetsQueries';
|
||||
import { useReadFindManyRecordsQueryInCache } from '@/object-record/cache/hooks/useReadFindManyRecordsQueryInCache';
|
||||
import { useUpsertFindManyRecordsQueryInCache } from '@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache';
|
||||
import { mockedActivities } from '~/testing/mock-data/activities';
|
||||
|
||||
const upsertFindManyRecordsQueryInCacheMock = jest.fn();
|
||||
const useReadFindManyRecordsQueryInCacheMock = jest.fn(() => [
|
||||
{ id: '981' },
|
||||
{ id: '345' },
|
||||
]);
|
||||
jest.mock(
|
||||
'@/object-record/cache/hooks/useReadFindManyRecordsQueryInCache',
|
||||
() => ({
|
||||
useReadFindManyRecordsQueryInCache: jest.fn(),
|
||||
}),
|
||||
);
|
||||
jest.mock(
|
||||
'@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache',
|
||||
() => ({
|
||||
useUpsertFindManyRecordsQueryInCache: jest.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
(useReadFindManyRecordsQueryInCache as jest.Mock).mockImplementation(() => ({
|
||||
readFindManyRecordsQueryInCache: useReadFindManyRecordsQueryInCacheMock,
|
||||
}));
|
||||
(useUpsertFindManyRecordsQueryInCache as jest.Mock).mockImplementation(() => ({
|
||||
upsertFindManyRecordsQueryInCache: upsertFindManyRecordsQueryInCacheMock,
|
||||
}));
|
||||
|
||||
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<RecoilRoot>
|
||||
<MockedProvider addTypename={false}>{children}</MockedProvider>
|
||||
</RecoilRoot>
|
||||
);
|
||||
|
||||
const mockActivityTarget = {
|
||||
__typename: 'ActivityTarget',
|
||||
updatedAt: '2021-08-03T19:20:06.000Z',
|
||||
createdAt: '2021-08-03T19:20:06.000Z',
|
||||
personId: '1',
|
||||
activityId: '234',
|
||||
companyId: '1',
|
||||
id: '123',
|
||||
activity: mockedActivities[0],
|
||||
};
|
||||
|
||||
describe('useRemoveFromActivityTargetsQueries', () => {
|
||||
it('works as expected', () => {
|
||||
const { result } = renderHook(() => useRemoveFromActivityTargetsQueries(), {
|
||||
wrapper: Wrapper,
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current.removeFromActivityTargetsQueries({
|
||||
activityTargetsToRemove: [mockActivityTarget],
|
||||
targetableObjects: [],
|
||||
});
|
||||
});
|
||||
|
||||
expect(upsertFindManyRecordsQueryInCacheMock).toHaveBeenCalledWith({
|
||||
objectRecordsToOverwrite: [{ id: '981' }, { id: '345' }],
|
||||
queryVariables: { filter: {} },
|
||||
depth: 2,
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,187 +0,0 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { MockedProvider, MockedResponse } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import gql from 'graphql-tag';
|
||||
import { RecoilRoot, useSetRecoilState } from 'recoil';
|
||||
|
||||
import { useCreateActivityInDB } from '@/activities/hooks/useCreateActivityInDB';
|
||||
import { useUpsertActivity } from '@/activities/hooks/useUpsertActivity';
|
||||
import { currentNotesQueryVariablesState } from '@/activities/notes/states/currentNotesQueryVariablesState';
|
||||
import { isActivityInCreateModeState } from '@/activities/states/isActivityInCreateModeState';
|
||||
import { currentCompletedTaskQueryVariablesState } from '@/activities/tasks/states/currentCompletedTaskQueryVariablesState';
|
||||
import { currentIncompleteTaskQueryVariablesState } from '@/activities/tasks/states/currentIncompleteTaskQueryVariablesState';
|
||||
import { objectShowPageTargetableObjectState } from '@/activities/timeline/states/objectShowPageTargetableObjectIdState';
|
||||
import { Activity } from '@/activities/types/Activity';
|
||||
import { mockedActivities } from '~/testing/mock-data/activities';
|
||||
|
||||
const newId = 'new-id';
|
||||
const activity = mockedActivities[0];
|
||||
const input: Partial<Activity> = { id: newId };
|
||||
|
||||
const mockedDate = '2024-03-15T12:00:00.000Z';
|
||||
const toISOStringMock = jest.fn(() => mockedDate);
|
||||
global.Date.prototype.toISOString = toISOStringMock;
|
||||
|
||||
const useCreateActivityInDBMock = jest.fn();
|
||||
|
||||
jest.mock('@/activities/hooks/useCreateActivityInDB', () => ({
|
||||
useCreateActivityInDB: jest.fn(),
|
||||
}));
|
||||
(useCreateActivityInDB as jest.Mock).mockImplementation(() => ({
|
||||
createActivityInDB: useCreateActivityInDBMock,
|
||||
}));
|
||||
|
||||
const mocks: MockedResponse[] = [
|
||||
{
|
||||
request: {
|
||||
query: gql`
|
||||
mutation UpdateOneActivity(
|
||||
$idToUpdate: ID!
|
||||
$input: ActivityUpdateInput!
|
||||
) {
|
||||
updateActivity(id: $idToUpdate, data: $input) {
|
||||
__typename
|
||||
createdAt
|
||||
reminderAt
|
||||
authorId
|
||||
title
|
||||
completedAt
|
||||
updatedAt
|
||||
body
|
||||
dueAt
|
||||
type
|
||||
id
|
||||
assigneeId
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
idToUpdate: activity.id,
|
||||
input: { id: 'new-id' },
|
||||
},
|
||||
},
|
||||
result: jest.fn(() => ({
|
||||
data: {
|
||||
updateActivity: { ...activity, ...input },
|
||||
},
|
||||
})),
|
||||
},
|
||||
];
|
||||
|
||||
const getWrapper =
|
||||
(initialIndex: 0 | 1) =>
|
||||
({ children }: { children: ReactNode }) => (
|
||||
<MemoryRouter
|
||||
initialEntries={['/tasks', '/object', { pathname: '/three' }]}
|
||||
initialIndex={initialIndex}
|
||||
>
|
||||
<RecoilRoot>
|
||||
<MockedProvider mocks={mocks} addTypename={false}>
|
||||
{children}
|
||||
</MockedProvider>
|
||||
</RecoilRoot>
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
describe('useUpsertActivity', () => {
|
||||
it('updates an activity', async () => {
|
||||
const { result } = renderHook(() => useUpsertActivity(), {
|
||||
wrapper: getWrapper(0),
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
await result.current.upsertActivity({
|
||||
activity,
|
||||
input,
|
||||
});
|
||||
});
|
||||
|
||||
expect(mocks[0].result).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('creates an activity on tasks page', async () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const res = useUpsertActivity();
|
||||
const setIsActivityInCreateMode = useSetRecoilState(
|
||||
isActivityInCreateModeState,
|
||||
);
|
||||
|
||||
return { ...res, setIsActivityInCreateMode };
|
||||
},
|
||||
{
|
||||
wrapper: getWrapper(0),
|
||||
},
|
||||
);
|
||||
|
||||
act(() => {
|
||||
result.current.setIsActivityInCreateMode(true);
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
await result.current.upsertActivity({
|
||||
activity,
|
||||
input: {},
|
||||
});
|
||||
});
|
||||
|
||||
expect(useCreateActivityInDBMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('creates an activity on objects page', async () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const res = useUpsertActivity();
|
||||
const setIsActivityInCreateMode = useSetRecoilState(
|
||||
isActivityInCreateModeState,
|
||||
);
|
||||
const setObjectShowPageTargetableObject = useSetRecoilState(
|
||||
objectShowPageTargetableObjectState,
|
||||
);
|
||||
const setCurrentCompletedTaskQueryVariables = useSetRecoilState(
|
||||
currentCompletedTaskQueryVariablesState,
|
||||
);
|
||||
const setCurrentIncompleteTaskQueryVariables = useSetRecoilState(
|
||||
currentIncompleteTaskQueryVariablesState,
|
||||
);
|
||||
|
||||
const setCurrentNotesQueryVariables = useSetRecoilState(
|
||||
currentNotesQueryVariablesState,
|
||||
);
|
||||
|
||||
return {
|
||||
...res,
|
||||
setIsActivityInCreateMode,
|
||||
setObjectShowPageTargetableObject,
|
||||
setCurrentCompletedTaskQueryVariables,
|
||||
setCurrentIncompleteTaskQueryVariables,
|
||||
setCurrentNotesQueryVariables,
|
||||
};
|
||||
},
|
||||
{
|
||||
wrapper: getWrapper(1),
|
||||
},
|
||||
);
|
||||
|
||||
act(() => {
|
||||
result.current.setIsActivityInCreateMode(true);
|
||||
result.current.setObjectShowPageTargetableObject({
|
||||
id: '123',
|
||||
targetObjectNameSingular: 'people',
|
||||
});
|
||||
result.current.setCurrentCompletedTaskQueryVariables({});
|
||||
result.current.setCurrentIncompleteTaskQueryVariables({});
|
||||
result.current.setCurrentNotesQueryVariables({});
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
await result.current.upsertActivity({
|
||||
activity,
|
||||
input: {},
|
||||
});
|
||||
});
|
||||
|
||||
expect(useCreateActivityInDBMock).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user