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
10 changed files
with
290 additions
and
6 deletions.
There are no files selected for viewing
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,16 @@ | ||
[cozy-client](../README.md) / useMutation | ||
|
||
# Namespace: useMutation | ||
|
||
## Variables | ||
|
||
### propTypes | ||
|
||
• **propTypes**: `Object` | ||
|
||
*Type declaration* | ||
|
||
| Name | Type | | ||
| :------ | :------ | | ||
| `onError` | `Requireable`<(...`args`: `any`\[]) => `any`> | | ||
| `onSuccess` | `Requireable`<(...`args`: `any`\[]) => `any`> | |
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
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,56 @@ | ||
import { useState, useCallback } from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
import useClient from './useClient' | ||
|
||
/** | ||
* This hook manages the state during the saving of a document | ||
* | ||
* @returns {import("../types").UseMutationReturnValue} | ||
*/ | ||
const useMutation = ({ onSuccess, onError }) => { | ||
const client = useClient() | ||
|
||
/** @type {import("../types").useState<import("../types").QueryFetchStatus>} */ | ||
const [mutationStatus, setMutationStatus] = useState('pending') | ||
const [error, setError] = useState() | ||
const [data, setData] = useState() | ||
|
||
const mutate = useCallback( | ||
async doc => { | ||
setError(undefined) | ||
setMutationStatus('loading') | ||
try { | ||
const resp = await client.save(doc) | ||
setData(resp.data) | ||
if (typeof onSuccess === 'function') { | ||
await onSuccess(resp.data) | ||
} | ||
setMutationStatus('loaded') | ||
} catch (e) { | ||
setMutationStatus('failed') | ||
setError(e) | ||
if (typeof onError === 'function') { | ||
await onError(e) | ||
} | ||
} | ||
}, | ||
[client, onError, onSuccess] | ||
) | ||
|
||
return { | ||
mutate, | ||
mutationStatus, | ||
error, | ||
data | ||
} | ||
} | ||
|
||
useMutation.propTypes = { | ||
/** This function is triggered when the save is successful */ | ||
onSuccess: PropTypes.func, | ||
/** This function is triggered when the save has failed */ | ||
onError: PropTypes.func | ||
} | ||
|
||
export { useMutation } |
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,81 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
|
||
import { useMutation } from './useMutation' | ||
import useClient from './useClient' | ||
|
||
jest.mock('./useClient') | ||
|
||
describe('useMutation', () => { | ||
const setup = ({ onSuccess, onError, saveSpy = jest.fn() } = {}) => { | ||
useClient.mockReturnValue({ | ||
save: saveSpy | ||
}) | ||
|
||
return renderHook(() => | ||
useMutation({ | ||
onSuccess, | ||
onError | ||
}) | ||
) | ||
} | ||
|
||
it('should be in pending status', () => { | ||
const { | ||
result: { current } | ||
} = setup() | ||
expect(current.mutationStatus).toBe('pending') | ||
}) | ||
|
||
it('should be in loading status after mutate', async () => { | ||
const saveSpy = jest.fn().mockImplementation(() => new Promise(() => {})) | ||
|
||
const { result } = setup({ saveSpy }) | ||
|
||
await act(async () => { | ||
result.current.mutate({ | ||
doctype: 'io.cozy.simpsons' | ||
}) | ||
}) | ||
|
||
expect(result.current.mutationStatus).toBe('loading') | ||
}) | ||
|
||
it('should be in loaded status after mutate', async () => { | ||
const successSpy = jest.fn() | ||
const saveSpy = jest.fn().mockResolvedValue({ data: 'test' }) | ||
const { result } = setup({ | ||
onSuccess: successSpy, | ||
saveSpy | ||
}) | ||
|
||
await act(async () => { | ||
result.current.mutate({ | ||
doctype: 'io.cozy.simpsons' | ||
}) | ||
}) | ||
|
||
expect(result.current.mutationStatus).toBe('loaded') | ||
expect(saveSpy).toBeCalledTimes(1) | ||
expect(successSpy).toBeCalledTimes(1) | ||
expect(result.current.data).toBe('test') | ||
}) | ||
|
||
it('should be in failed status after mutate', async () => { | ||
const errorSpy = jest.fn() | ||
const saveSpy = jest.fn().mockRejectedValue({ error: 'test' }) | ||
const { result } = setup({ | ||
onError: errorSpy, | ||
saveSpy | ||
}) | ||
|
||
await act(async () => { | ||
result.current.mutate({ | ||
doctype: 'io.cozy.simpsons' | ||
}) | ||
}) | ||
|
||
expect(result.current.mutationStatus).toBe('failed') | ||
expect(errorSpy).toBeCalledTimes(1) | ||
expect(result.current.error).toStrictEqual({ error: 'test' }) | ||
}) | ||
}) |
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
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,16 @@ | ||
/** | ||
* This hook manages the state during the saving of a document | ||
* | ||
* @returns {import("../types").UseMutationReturnValue} | ||
*/ | ||
export function useMutation({ onSuccess, onError }: { | ||
onSuccess: any; | ||
onError: any; | ||
}): import("../types").UseMutationReturnValue; | ||
export namespace useMutation { | ||
namespace propTypes { | ||
const onSuccess: PropTypes.Requireable<(...args: any[]) => any>; | ||
const onError: PropTypes.Requireable<(...args: any[]) => any>; | ||
} | ||
} | ||
import PropTypes from "prop-types"; |
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