|
1 |
| -import { mount } from '@vue/test-utils' |
2 |
| -import Demo from '../docs/basic/demo/demo.vue' |
| 1 | +import renderHook from 'test-utils/renderHook' |
| 2 | +import { sleep } from 'test-utils/sleep' |
| 3 | +import useRequest from '../useRequest' |
3 | 4 |
|
4 |
| -describe('useRequest/Basic', () => { |
5 |
| - const wrapper = mount(Demo) |
6 |
| - |
7 |
| - const currentName = wrapper.findAll('div').at(0) |
8 |
| - const currentName1 = wrapper.findAll('div').at(1) |
| 5 | +function getUsername(params: { desc: string }): Promise<string> { |
| 6 | + return new Promise(resolve => { |
| 7 | + setTimeout(() => { |
| 8 | + resolve(`vue-hooks-plus ${params.desc}`) |
| 9 | + }, 200) |
| 10 | + }) |
| 11 | +} |
9 | 12 |
|
10 |
| - it('should init work is loading', () => { |
11 |
| - { |
12 |
| - expect(currentName?.text()).toBe('name:loading') |
13 |
| - expect(currentName1?.text()).toBe('name1:loading') |
14 |
| - } |
| 13 | +describe('useRequest/Basic', () => { |
| 14 | + it('should auto work', async () => { |
| 15 | + const [hook] = renderHook(() => |
| 16 | + useRequest(getUsername, { |
| 17 | + defaultParams: [ |
| 18 | + { |
| 19 | + desc: 'nice', |
| 20 | + }, |
| 21 | + ], |
| 22 | + }), |
| 23 | + ) |
| 24 | + await sleep(200) |
| 25 | + expect(hook.data?.value).toBe('vue-hooks-plus nice') |
15 | 26 | })
|
16 | 27 |
|
17 |
| - it('should auto run params is null', () => { |
18 |
| - { |
19 |
| - const currentAutoRunParams = wrapper.findAll('span').at(0) |
20 |
| - expect(currentAutoRunParams?.text()).toBe('[]') |
21 |
| - } |
| 28 | + it('should manual run', async () => { |
| 29 | + const [hook] = renderHook(() => |
| 30 | + useRequest(getUsername, { |
| 31 | + manual: true, |
| 32 | + defaultParams: [ |
| 33 | + { |
| 34 | + desc: 'nice', |
| 35 | + }, |
| 36 | + ], |
| 37 | + }), |
| 38 | + ) |
| 39 | + |
| 40 | + await sleep(200) |
| 41 | + expect(hook.data?.value).toBeUndefined() |
| 42 | + hook.run({ desc: 'nice1' }) |
| 43 | + await sleep(200) |
| 44 | + expect(hook.data?.value).toBe('vue-hooks-plus nice1') |
22 | 45 | })
|
23 | 46 |
|
24 |
| - it('should manual params work', () => { |
25 |
| - { |
26 |
| - const currentManualParams = wrapper.findAll('span').at(1) |
27 |
| - expect(currentManualParams?.text()).toBe('[{"desc":"nice"}]') |
28 |
| - } |
| 47 | + it('should params work', async () => { |
| 48 | + const [hook] = renderHook(() => |
| 49 | + useRequest(getUsername, { |
| 50 | + defaultParams: [ |
| 51 | + { |
| 52 | + desc: 'nice', |
| 53 | + }, |
| 54 | + ], |
| 55 | + }), |
| 56 | + ) |
| 57 | + await sleep(200) |
| 58 | + expect(hook.params.value[0]?.desc).toBe('nice') |
| 59 | + hook.run({ desc: 'nice1' }) |
| 60 | + await sleep(200) |
| 61 | + expect(hook.params.value[0]?.desc).toBe('nice1') |
29 | 62 | })
|
30 | 63 | })
|
0 commit comments