-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
559 additions
and
206 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { renderHook, waitFor } from 'test/test-utils'; | ||
import { queryClient } from 'test/query-client'; | ||
import { todosFixture } from '__fixtures__/todos'; | ||
import { QueryKeys } from 'utils/constants'; | ||
import { Task } from '../useGetUserTasks'; | ||
|
||
import { useUpdateTask } from '../useUpdateTask'; | ||
|
||
describe('useUpdateTask', () => { | ||
it('should update task', async () => { | ||
// ARRANGE | ||
const updatedTask = todosFixture[0]; | ||
let isSuccess = false; | ||
const { result } = renderHook(() => useUpdateTask()); | ||
await waitFor(() => expect(result.current).not.toBeNull()); | ||
|
||
// ACT | ||
result.current.mutate( | ||
{ task: updatedTask }, | ||
{ | ||
onSuccess: () => { | ||
isSuccess = true; | ||
}, | ||
}, | ||
); | ||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
// ASSERT | ||
expect(isSuccess).toBe(true); | ||
}); | ||
|
||
it('should create cached data when none exists', async () => { | ||
// ARRANGE | ||
const updatedTask = todosFixture[0]; | ||
let isSuccess = false; | ||
const { result } = renderHook(() => useUpdateTask()); | ||
await waitFor(() => expect(result.current).not.toBeNull()); | ||
|
||
// ACT | ||
result.current.mutate( | ||
{ task: updatedTask }, | ||
{ | ||
onSuccess: () => { | ||
isSuccess = true; | ||
}, | ||
}, | ||
); | ||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
// ASSERT | ||
expect(isSuccess).toBe(true); | ||
expect(queryClient.getQueryData([QueryKeys.Tasks, { userId: updatedTask.userId }])).toEqual([ | ||
updatedTask, | ||
]); | ||
}); | ||
|
||
it('should update cached data when exists', async () => { | ||
// ARRANGE | ||
const updatedTask = todosFixture[0]; | ||
queryClient.setQueryData([QueryKeys.Tasks, { userId: updatedTask.userId }], todosFixture); | ||
let isSuccess = false; | ||
const { result } = renderHook(() => useUpdateTask()); | ||
await waitFor(() => expect(result.current).not.toBeNull()); | ||
|
||
// ACT | ||
result.current.mutate( | ||
{ task: updatedTask }, | ||
{ | ||
onSuccess: () => { | ||
isSuccess = true; | ||
}, | ||
}, | ||
); | ||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
// ASSERT | ||
expect(isSuccess).toBe(true); | ||
expect( | ||
queryClient.getQueryData<Task[]>([QueryKeys.Tasks, { userId: updatedTask.userId }])?.length, | ||
).toEqual(todosFixture.length); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import reject from 'lodash/reject'; | ||
|
||
import { QueryKeys } from 'utils/constants'; | ||
import { Task } from './useGetUserTasks'; | ||
import { useConfig } from 'hooks/useConfig'; | ||
import { useAxios } from 'hooks/useAxios'; | ||
|
||
/** | ||
* The `useUpdateTask` mutation function variables. | ||
*/ | ||
export type UpdateTaskVariables = { | ||
task: Task; | ||
}; | ||
|
||
/** | ||
* An API hook which updates a single `Task`. Returns a `UseMutationResult` | ||
* object whose `mutate` attribute is a function to update as `Task`. | ||
* | ||
* When successful, the hook updates cached `Task` query data. | ||
* @returns Returns a `UseMutationResult`. | ||
*/ | ||
export const useUpdateTask = () => { | ||
const queryClient = useQueryClient(); | ||
const config = useConfig(); | ||
const axios = useAxios(); | ||
|
||
/** | ||
* Update a `Task`. | ||
* @param {UpdateTaskVariables} variables - The mutation function variables. | ||
* @returns The updated `Task` object. | ||
*/ | ||
const updateTask = async ({ task }: UpdateTaskVariables): Promise<Task> => { | ||
const response = await axios.request({ | ||
method: 'put', | ||
url: `${config.VITE_BASE_URL_API}/todos/${task.id}`, | ||
data: task, | ||
}); | ||
return response.data; | ||
}; | ||
|
||
return useMutation({ | ||
mutationFn: updateTask, | ||
onSuccess: (data, variables) => { | ||
// update cached query data | ||
queryClient.setQueryData<Task[]>( | ||
[QueryKeys.Tasks, { userId: variables.task.userId }], | ||
(cachedTasks) => (cachedTasks ? [...reject(cachedTasks, { id: data.id }), data] : [data]), | ||
); | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { | ||
Button, | ||
ButtonVariant, | ||
PropsWithClassName, | ||
PropsWithTestId, | ||
} from '@leanstacks/react-common'; | ||
import classNames from 'classnames'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { Task } from '../api/useGetUserTasks'; | ||
import { useUpdateTask } from '../api/useUpdateTask'; | ||
import { useToasts } from 'hooks/useToasts'; | ||
import Icon from 'components/Icon/Icon'; | ||
|
||
/** | ||
* Propeties for the`TaskCompleteToggle` component. | ||
* @param {Task} task - A Task object. | ||
* @see {@link PropsWithClassName} | ||
* @see {@link PropsWithTestId} | ||
*/ | ||
interface TaskCompleteToggleProps extends PropsWithClassName, PropsWithTestId { | ||
task: Task; | ||
} | ||
|
||
/** | ||
* The `TaskCompleteToggle` component renders a `Button` which allows a user | ||
* to toggle the value of the Task `complete` attribute. | ||
* @param {TaskCompleteToggleProps} props - Component properties. | ||
* @returns {JSX.Element} JSX | ||
*/ | ||
const TaskCompleteToggle = ({ | ||
className, | ||
task, | ||
testId = 'toggle-task-complete', | ||
}: TaskCompleteToggleProps): JSX.Element => { | ||
const { t } = useTranslation(); | ||
const { mutate: updateTask } = useUpdateTask(); | ||
const { createToast } = useToasts(); | ||
|
||
const buttonTitle = task.completed | ||
? t('task.markIncomplete', { ns: 'users' }) | ||
: t('task.markComplete', { ns: 'users' }); | ||
|
||
/** | ||
* Actions to perform when the task complete toggle button is clicked. | ||
*/ | ||
const handleButtonClick = () => { | ||
updateTask( | ||
{ | ||
task: { | ||
...task, | ||
completed: !task.completed, | ||
}, | ||
}, | ||
{ | ||
onSuccess: (data) => { | ||
createToast({ | ||
text: data.completed | ||
? t('task.markedComplete', { ns: 'users' }) | ||
: t('task.markedIncomplete', { ns: 'users' }), | ||
isAutoDismiss: true, | ||
}); | ||
}, | ||
}, | ||
); | ||
}; | ||
|
||
return ( | ||
<Button | ||
className={classNames('!m-0 contents !border-none !p-0', className)} | ||
variant={ButtonVariant.Text} | ||
title={buttonTitle} | ||
onClick={handleButtonClick} | ||
data-testid={testId} | ||
> | ||
<Icon | ||
name={task.completed ? 'task_alt' : 'circle'} | ||
fill={0} | ||
className={classNames('text-lg', { 'text-green-600': task.completed })} | ||
testId={`${testId}-icon`} | ||
/> | ||
</Button> | ||
); | ||
}; | ||
|
||
export default TaskCompleteToggle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.