Skip to content

Commit

Permalink
feat(useAsyncState): add test cases (#2657)
Browse files Browse the repository at this point in the history
Co-authored-by: wheat <jacobrclevenger@gmail.com>
  • Loading branch information
Alfred-Skyblue and wheatjs committed Jan 17, 2023
1 parent b06b84e commit 5907f45
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions packages/core/useAsyncState/index.test.ts
@@ -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'))
})
})

0 comments on commit 5907f45

Please sign in to comment.