-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.test.ts
48 lines (44 loc) · 1.42 KB
/
basic.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { $fetch } from 'ofetch'
import { expect, it } from 'vitest'
import { createTest, next, sleep } from '.'
import { useFetch } from '../src'
createTest(3000, 'basic', (listener, getURL) => {
it('$fetch', async () => {
expect(await $fetch(getURL('ok'))).toBe('ok')
expect(await $fetch(getURL('params'), { query: { one: '1' } })).toEqual({ one: '1' })
expect(await $fetch('ok', { baseURL: listener.value.url })).toBe('ok')
expect(await $fetch(getURL('url/123'))).toBe('/123')
expect(await $fetch(getURL('post'), {
method: 'post',
body: { one: 1 },
})).toEqual({ one: 1 })
expect(await $fetch(getURL('token'), {
async onRequest(ctx) {
ctx.options.headers ??= new Headers()
const headers = ctx.options.headers
if (headers instanceof Headers) {
await sleep(10)
headers.append('token', 'my-auth-token')
}
},
})).toBe('my-auth-token')
let testResponse = 'not Ok'
await $fetch(getURL('ok'), {
onResponse(ctx) {
testResponse = ctx.response._data
},
})
expect(testResponse).toBe('ok')
})
it('basic', async () => {
const { data } = useFetch(getURL('ok'))
expect(await next(data)).toBe('ok')
})
it('basic options', async () => {
const { data } = useFetch('params', {
baseURL: listener.value.url,
query: { one: '1' },
})
expect(await next(data)).toEqual({ one: '1' })
})
})