Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useAsyncState): add test cases (#2657)
Co-authored-by: wheat <jacobrclevenger@gmail.com>
- Loading branch information
1 parent
b06b84e
commit 5907f45
Showing
1 changed file
with
73 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { promiseTimeout } from '@vueuse/shared' | ||
import { useAsyncState } from '../useAsyncState' | ||
|
||
describe('useAsyncState', () => { | ||
it('should be defined', () => { | ||
expect(useAsyncState).toBeDefined() | ||
}) | ||
|
||
const p1 = (num = 1) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(num) | ||
}, 50) | ||
}) | ||
} | ||
const p2 = async (id?: string) => { | ||
if (!id) | ||
throw new Error('error') | ||
return id | ||
} | ||
|
||
it('should work', async () => { | ||
const { execute, state } = useAsyncState(p1, 0) | ||
expect(state.value).toBe(0) | ||
await execute(0, 2) | ||
expect(state.value).toBe(2) | ||
}) | ||
|
||
it('should work with isLoading', () => { | ||
const { execute, isLoading } = useAsyncState(p1, 0, { immediate: false }) | ||
expect(isLoading.value).toBeFalsy() | ||
execute(1) | ||
expect(isLoading.value).toBeTruthy() | ||
}) | ||
|
||
it('should work with isReady', async () => { | ||
const { execute, isReady } = useAsyncState(p1, 0, { immediate: false }) | ||
expect(isReady.value).toBeFalsy() | ||
await execute(1) | ||
expect(isReady.value).toBeTruthy() | ||
}) | ||
|
||
it('should work with error', async () => { | ||
const { execute, error } = useAsyncState(p2, '0', { immediate: false }) | ||
expect(error.value).toBeUndefined() | ||
await execute() | ||
expect(error.value).toBeInstanceOf(Error) | ||
}) | ||
|
||
it('should work with delay', async () => { | ||
const { execute, state } = useAsyncState(p1, 0, { delay: 100 }) | ||
await promiseTimeout(50) | ||
expect(state.value).toBe(0) | ||
await execute(0, 2) | ||
expect(state.value).toBe(2) | ||
}) | ||
|
||
it('should work with onSuccess', async () => { | ||
const onSuccess = vi.fn() | ||
const { execute } = useAsyncState(p1, 0, { onSuccess }) | ||
await execute(0, 2) | ||
expect(onSuccess).toHaveBeenCalled() | ||
expect(onSuccess).toHaveBeenCalledWith(2) | ||
}) | ||
|
||
it('should work with onError', async () => { | ||
const onError = vi.fn() | ||
const { execute } = useAsyncState(p2, '0', { onError, immediate: false }) | ||
await execute() | ||
expect(onError).toHaveBeenCalled() | ||
expect(onError).toHaveBeenCalledWith(new Error('error')) | ||
}) | ||
}) |