Skip to content

Commit e544384

Browse files
committed
improvement(useFetch): Added tests to useFetch
1 parent cbf3cfa commit e544384

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

src/functions/useFetch/stories/UseFetchDemo.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</use-fetch-demo-table>
4949

5050
<!-- Fetched -->
51-
<use-fetch-demo-table status="Success" v-else>
51+
<use-fetch-demo-table status="Success" v-else-if="data">
5252
<p>
5353
Resource fetched successfully with status code
5454
<strong>{{ status }}</strong> and message
@@ -71,8 +71,8 @@ export default Vue.extend({
7171
components: { UseFetchDemoTable },
7272
setup() {
7373
const isInit = ref(false)
74-
const slowApi = 'http://slowwly.robertomurray.co.uk/delay/1000/url'
75-
const randomDogUrl = `${slowApi}/https://dog.ceo/api/breeds/image/random`
74+
const delayUrl = 'http://deelay.me/2000'
75+
const randomDogUrl = `${delayUrl}/https://dog.ceo/api/breeds/image/random`
7676
const url = ref(randomDogUrl)
7777
const {
7878
data,
@@ -93,7 +93,7 @@ export default Vue.extend({
9393
9494
const startWithFailed = () => {
9595
isInit.value = true
96-
url.value = `${slowApi}/https://dog.ceo`
96+
url.value = `${delayUrl}/https://dog.ceo`
9797
start()
9898
}
9999

src/functions/useFetch/stories/useFetch.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ function useFetch(
4949
<div>
5050
<div v-if="isFailed">Failed!</div>
5151
<div v-else-if="isLoading">Loading...</div>
52-
<div v-else><img :src="data.message" alt="" /></div>
52+
<div v-else-if="data">
53+
<img :src="data.message" alt="" />
54+
</div>
5355
</div>
5456
</template>
5557

+77-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,85 @@
1-
// import { mount } from '@src/helpers/test'
2-
// import { useFetch } from '@src/vue-use-kit'
1+
import { mount, flushPromises } from '@src/helpers/test'
2+
import { useFetch } from '@src/vue-use-kit'
33

44
afterEach(() => {
55
jest.clearAllMocks()
66
})
77

8+
const abortError = 'AbortError'
9+
10+
const mockFetch = ({
11+
data = { text: 'Message here' },
12+
header = 'abcd;application/json',
13+
isAborted = false
14+
} = {}) => {
15+
;(window as any).fetch = () => {
16+
if (isAborted) {
17+
const err = new Error()
18+
err.name = abortError
19+
throw err
20+
}
21+
22+
return {
23+
headers: {
24+
get: () => header
25+
},
26+
json: () => data
27+
}
28+
}
29+
}
30+
31+
const testComponent = (url = '', opts: RequestInit = {}, onMount = true) => ({
32+
template: `
33+
<div>
34+
<div id="isLoading" v-if="isLoading"></div>
35+
<div id="isFailed" v-if="isFailed"></div>
36+
<div id="isAborted" v-if="isAborted"></div>
37+
<div id="data" v-if="data" v-text="data.text"></div>
38+
<button id="start" @click="start"></button>
39+
<button id="stop" @click="stop"></button>
40+
</div>
41+
`,
42+
setup() {
43+
const { data, isLoading, isFailed, isAborted, start, stop } = useFetch(
44+
url,
45+
opts,
46+
onMount
47+
)
48+
return { data, isLoading, isFailed, isAborted, start, stop }
49+
}
50+
})
51+
852
describe('useFetch', () => {
9-
it('should do something', () => {
10-
// Add test here
53+
const url = 'http://test.com'
54+
55+
it('should show #isLoading in the correct order', async () => {
56+
mockFetch()
57+
const wrapper = mount(testComponent(url))
58+
expect(wrapper.find('#isLoading').exists()).toBe(false)
59+
await wrapper.vm.$nextTick()
60+
expect(wrapper.find('#isLoading').exists()).toBe(true)
61+
})
62+
63+
it('should show #data when provided', async () => {
64+
const data = { text: 'Here is some data' }
65+
mockFetch({ data })
66+
const wrapper = mount(testComponent(url))
67+
await flushPromises()
68+
expect(wrapper.find('#data').text()).toBe(data.text)
69+
})
70+
71+
it('should show #isFailed when the header is not of json type', async () => {
72+
mockFetch({ header: 'brokenHeader' })
73+
const wrapper = mount(testComponent(url))
74+
await flushPromises()
75+
expect(wrapper.find('#isFailed').exists()).toBe(true)
76+
})
77+
78+
it('should show #isAborted when aborted is triggered', async () => {
79+
mockFetch({ isAborted: true })
80+
const wrapper = mount(testComponent(url))
81+
await flushPromises()
82+
expect(wrapper.find('#isFailed').exists()).toBe(true)
83+
expect(wrapper.find('#isAborted').exists()).toBe(true)
1184
})
1285
})

src/helpers/test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@ export const checkElementExistenceOnMount = async (
8282
await wrapper.vm.$nextTick()
8383
expect(wrapper.find(elementName).exists()).toBe(mountType)
8484
}
85+
86+
export const flushPromises = () => new Promise(setImmediate)

0 commit comments

Comments
 (0)