Add tests for /modules/activities/tasks/hooks (#4495)
Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com>
This commit is contained in:
committed by
GitHub
parent
21cd38d6fb
commit
683f1f1f33
@ -0,0 +1,85 @@
|
|||||||
|
import { ReactNode } from 'react';
|
||||||
|
import { MockedProvider, MockedResponse } from '@apollo/client/testing';
|
||||||
|
import { act, renderHook } from '@testing-library/react';
|
||||||
|
import gql from 'graphql-tag';
|
||||||
|
import { RecoilRoot } from 'recoil';
|
||||||
|
|
||||||
|
import { useCompleteTask } from '@/activities/tasks/hooks/useCompleteTask';
|
||||||
|
|
||||||
|
const task = { id: '123', completedAt: '2024-03-15T07:33:14.212Z' };
|
||||||
|
|
||||||
|
const mockedDate = task.completedAt;
|
||||||
|
const toISOStringMock = jest.fn(() => mockedDate);
|
||||||
|
global.Date.prototype.toISOString = toISOStringMock;
|
||||||
|
|
||||||
|
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: task.id,
|
||||||
|
input: { completedAt: task.completedAt },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
result: jest.fn(() => ({
|
||||||
|
data: {
|
||||||
|
updateActivity: {
|
||||||
|
__typename: 'Activity',
|
||||||
|
createdAt: '2024-03-15T07:33:14.212Z',
|
||||||
|
reminderAt: null,
|
||||||
|
authorId: '123',
|
||||||
|
title: 'Test',
|
||||||
|
completedAt: '2024-03-15T07:33:14.212Z',
|
||||||
|
updatedAt: '2024-03-15T07:33:14.212Z',
|
||||||
|
body: 'Test',
|
||||||
|
dueAt: '2024-03-15T07:33:14.212Z',
|
||||||
|
type: 'Task',
|
||||||
|
id: '123',
|
||||||
|
assigneeId: '123',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||||
|
<RecoilRoot>
|
||||||
|
<MockedProvider mocks={mocks} addTypename={false}>
|
||||||
|
{children}
|
||||||
|
</MockedProvider>
|
||||||
|
</RecoilRoot>
|
||||||
|
);
|
||||||
|
|
||||||
|
describe('useCompleteTask', () => {
|
||||||
|
it('should complete task', async () => {
|
||||||
|
const { result } = renderHook(() => useCompleteTask(task), {
|
||||||
|
wrapper: Wrapper,
|
||||||
|
});
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
await result.current.completeTask(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mocks[0].result).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
import { ReactNode } from 'react';
|
||||||
|
import { renderHook } from '@testing-library/react';
|
||||||
|
import { RecoilRoot } from 'recoil';
|
||||||
|
|
||||||
|
import { useCurrentUserTaskCount } from '@/activities/tasks/hooks/useCurrentUserDueTaskCount';
|
||||||
|
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
|
||||||
|
import { mockedActivities } from '~/testing/mock-data/activities';
|
||||||
|
|
||||||
|
const useFindManyRecordsMock = jest.fn(() => ({
|
||||||
|
records: [...mockedActivities, { id: '2' }],
|
||||||
|
}));
|
||||||
|
|
||||||
|
jest.mock('@/object-record/hooks/useFindManyRecords', () => ({
|
||||||
|
useFindManyRecords: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
(useFindManyRecords as jest.Mock).mockImplementation(useFindManyRecordsMock);
|
||||||
|
|
||||||
|
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||||
|
<RecoilRoot>{children}</RecoilRoot>
|
||||||
|
);
|
||||||
|
|
||||||
|
describe('useCurrentUserTaskCount', () => {
|
||||||
|
it('should return the current user task count', async () => {
|
||||||
|
const { result } = renderHook(() => useCurrentUserTaskCount(), {
|
||||||
|
wrapper: Wrapper,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current.currentUserDueTaskCount).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,85 @@
|
|||||||
|
import { ReactNode } from 'react';
|
||||||
|
import { renderHook } from '@testing-library/react';
|
||||||
|
import { RecoilRoot } from 'recoil';
|
||||||
|
|
||||||
|
import { useActivities } from '@/activities/hooks/useActivities';
|
||||||
|
import { useTasks } from '@/activities/tasks/hooks/useTasks';
|
||||||
|
import { ObjectFilterDropdownScope } from '@/object-record/object-filter-dropdown/scopes/ObjectFilterDropdownScope';
|
||||||
|
|
||||||
|
const completedTasks = [
|
||||||
|
{
|
||||||
|
id: '1',
|
||||||
|
completedAt: '2024-03-15T07:33:14.212Z',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2',
|
||||||
|
completedAt: '2024-03-15T07:33:14.212Z',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '3',
|
||||||
|
completedAt: '2024-03-15T07:33:14.212Z',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const unscheduledTasks = [
|
||||||
|
{
|
||||||
|
id: '4',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const todayOrPreviousTasks = [
|
||||||
|
{
|
||||||
|
id: '5',
|
||||||
|
dueAt: '2024-03-15T07:33:14.212Z',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '6',
|
||||||
|
dueAt: '2024-03-15T07:33:14.212Z',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const useActivitiesMock = jest.fn(
|
||||||
|
({
|
||||||
|
activitiesFilters,
|
||||||
|
}: {
|
||||||
|
activitiesFilters: { completedAt: { is: 'NULL' | 'NOT_NULL' } };
|
||||||
|
}) => {
|
||||||
|
const isCompletedFilter = activitiesFilters.completedAt.is === 'NOT_NULL';
|
||||||
|
return {
|
||||||
|
activities: isCompletedFilter
|
||||||
|
? completedTasks
|
||||||
|
: [...todayOrPreviousTasks, ...unscheduledTasks],
|
||||||
|
initialized: true,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
jest.mock('@/activities/hooks/useActivities', () => ({
|
||||||
|
useActivities: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
(useActivities as jest.Mock).mockImplementation(useActivitiesMock);
|
||||||
|
|
||||||
|
const Wrapper = ({ children }: { children: ReactNode }) => (
|
||||||
|
<RecoilRoot>
|
||||||
|
<ObjectFilterDropdownScope filterScopeId="entity-tasks-filter-scope">
|
||||||
|
{children}
|
||||||
|
</ObjectFilterDropdownScope>
|
||||||
|
</RecoilRoot>
|
||||||
|
);
|
||||||
|
|
||||||
|
describe('useTasks', () => {
|
||||||
|
it("should return a user's tasks", () => {
|
||||||
|
const { result } = renderHook(() => useTasks({ targetableObjects: [] }), {
|
||||||
|
wrapper: Wrapper,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current).toEqual({
|
||||||
|
todayOrPreviousTasks,
|
||||||
|
upcomingTasks: [],
|
||||||
|
unscheduledTasks,
|
||||||
|
completedTasks,
|
||||||
|
initialized: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user