|
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' |
3 | 3 |
|
4 | 4 | afterEach(() => {
|
5 | 5 | jest.clearAllMocks()
|
6 | 6 | })
|
7 | 7 |
|
| 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 | + |
8 | 52 | 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) |
11 | 84 | })
|
12 | 85 | })
|
0 commit comments