forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
131 lines (91 loc) · 3.77 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { useFetch, createFetch } from '.'
import fetchMock from 'jest-fetch-mock'
import { when } from '@vueuse/shared'
import { nextTick, ref } from 'vue-demi'
describe('useFetch', () => {
beforeEach(() => {
fetchMock.resetMocks()
fetchMock.enableMocks()
fetchMock.doMock()
})
test('should have status code of 200 and message of Hello World', async() => {
fetchMock.mockResponse('Hello World', { status: 200 })
const { data, statusCode, isFinished } = useFetch('https://example.com')
await when(isFinished).toBe(true)
expect(statusCode.value).toBe(200)
expect(data.value).toBe('Hello World')
})
test('should parse response as json', async() => {
fetchMock.mockResponse(JSON.stringify({ message: 'Hello World' }), { status: 200 })
const { data, isFinished } = useFetch('https://example.com').json()
await when(isFinished).toBe(true)
expect(data.value).toStrictEqual({ message: 'Hello World' })
})
test('should have an error on 400', async() => {
fetchMock.mockResponse('', { status: 400 })
const { error, statusCode, isFinished } = useFetch('https://example.com')
await when(isFinished).toBe(true)
expect(statusCode.value).toBe(400)
expect(error.value).toBe('Bad Request')
})
test('should abort request and set aborted to true', async() => {
fetchMock.mockResponse(() => new Promise(resolve => setTimeout(() => resolve({ body: 'ok' }), 1000)))
const { aborted, abort, isFinished, execute } = useFetch('https://example.com')
setTimeout(() => abort(), 0)
await when(isFinished).toBe(true)
expect(aborted.value).toBe(true)
execute()
setTimeout(() => abort(), 0)
await when(isFinished).toBe(true)
expect(aborted.value).toBe(true)
})
test('should not call if immediate is false', async() => {
fetchMock.mockResponse('')
useFetch('https://example.com', { immediate: false })
await nextTick()
expect(fetchMock).toBeCalledTimes(0)
})
test('should refetch if refetch is set to true', async() => {
fetchMock.mockResponse('')
const url = ref('https://example.com')
const { isFinished } = useFetch(url, { refetch: true })
await when(isFinished).toBe(true)
url.value = 'https://example.com/test'
await nextTick()
await when(isFinished).toBe(true)
expect(fetchMock).toBeCalledTimes(2)
})
test('should create an instance of useFetch with a base url', async() => {
fetchMock.mockResponse('')
const useMyFetch = createFetch({ baseUrl: 'https://example.com', fetchOptions: { headers: { Authorization: 'test' } } })
const { isFinished } = useMyFetch('test', { headers: { 'Accept-Language': 'en-US' } })
await when(isFinished).toBe(true)
expect(fetchMock.mock.calls[0][1]!.headers).toMatchObject({ Authorization: 'test', 'Accept-Language': 'en-US' })
expect(fetchMock.mock.calls[0][0]).toEqual('https://example.com/test')
})
test('should run the beforeFetch function and add headers to the request', async() => {
fetchMock.mockResponse('')
const { isFinished } = useFetch('https://example.com', { headers: { 'Accept-Language': 'en-US' } }, {
beforeFetch({ options }) {
options.headers = {
...options.headers,
Authorization: 'my-auth-token',
}
return { options }
},
})
await when(isFinished).toBe(true)
expect(fetchMock.mock.calls[0][1]!.headers).toMatchObject({ Authorization: 'my-auth-token', 'Accept-Language': 'en-US' })
})
test('should run the beforeFetch function and cancel the request', async() => {
fetchMock.mockResponse('')
const { execute } = useFetch('https://example.com', {
immediate: false,
beforeFetch({ cancel }) {
cancel()
},
})
await execute()
expect(fetchMock).toBeCalledTimes(0)
})
})