|
1 | 1 | import useConditionWatcher from 'vue-condition-watcher' |
2 | | -import { isRef, isReactive, isReadonly, createApp, nextTick, defineComponent } from 'vue-demi' |
| 2 | +import { isRef, isReactive, isReadonly, defineComponent, isVue3 } from 'vue-demi' |
3 | 3 | import type { VueWrapper } from '@vue/test-utils' |
4 | | -import { mount } from '@vue/test-utils' |
5 | | -import { describe, expect, test, vi, beforeEach } from 'vitest' |
| 4 | +import { flushPromises, mount } from '@vue/test-utils' |
| 5 | +import { describe, expect, beforeEach } from 'vitest' |
6 | 6 |
|
7 | 7 | describe('Basic test of vue-condition-watcher', () => { |
8 | | - let basicTestConfig = {} |
| 8 | + let basicTestConfig: any = {} |
9 | 9 | beforeEach(() => { |
10 | 10 | basicTestConfig = { |
11 | | - fetcher: (params) => new Promise((resolve) => resolve(params)), |
| 11 | + fetcher: (params) => Promise.resolve(params), |
12 | 12 | conditions: { |
13 | 13 | gender: ['male'], |
14 | 14 | results: 9, |
15 | 15 | }, |
| 16 | + defaultParams: { |
| 17 | + name: 'runkids', |
| 18 | + }, |
16 | 19 | } |
17 | 20 | }) |
18 | 21 |
|
19 | 22 | it(`Check return value type`, () => { |
20 | | - const { conditions, data, error, isLoading, execute } = useConditionWatcher(basicTestConfig) |
| 23 | + const { conditions, data, error, isLoading, execute, isFetching } = useConditionWatcher(basicTestConfig) |
21 | 24 |
|
22 | 25 | expect(isReactive(conditions)).toBeTruthy() |
23 | 26 | expect(isRef(data)).toBeTruthy() |
24 | 27 | expect(isRef(error)).toBeTruthy() |
25 | 28 | expect(isRef(isLoading)).toBeTruthy() |
| 29 | + expect(isRef(isFetching)).toBeTruthy() |
26 | 30 | expect(isLoading.value).toBeTypeOf('boolean') |
| 31 | + expect(isLoading.value).toBe(true) |
| 32 | + expect(isFetching.value).toBe(false) |
27 | 33 | expect(execute).toBeTypeOf('function') |
28 | 34 | }) |
29 | 35 |
|
30 | | - it(`Check data, error, isLoading is readonly`, () => { |
31 | | - const { data, error, isLoading } = useConditionWatcher(basicTestConfig) |
| 36 | + it(`Check data, error, isLoading, isFetching is readonly`, () => { |
| 37 | + const { data, error, isLoading, isFetching } = useConditionWatcher(basicTestConfig) |
32 | 38 |
|
33 | 39 | expect(isReadonly(data)).toBeTruthy() |
34 | 40 | expect(isReadonly(error)).toBeTruthy() |
35 | 41 | expect(isReadonly(isLoading)).toBeTruthy() |
| 42 | + expect(isReadonly(isFetching)).toBeTruthy() |
36 | 43 | }) |
37 | 44 |
|
38 | 45 | it(`Condition should be change`, () => { |
@@ -85,6 +92,47 @@ describe('Basic test of vue-condition-watcher', () => { |
85 | 92 | }) |
86 | 93 | }) |
87 | 94 |
|
| 95 | +if (isVue3) { |
| 96 | + describe('useConditionWatcher', () => { |
| 97 | + const App = defineComponent({ |
| 98 | + template: `<div>{{ data }}</div>`, |
| 99 | + setup() { |
| 100 | + const { data, conditions } = useConditionWatcher({ |
| 101 | + fetcher: (params) => Promise.resolve(`Hello, world! ${params.name}!`), |
| 102 | + conditions: { |
| 103 | + name: '', |
| 104 | + }, |
| 105 | + }) |
| 106 | + return { |
| 107 | + data, |
| 108 | + conditions, |
| 109 | + } |
| 110 | + }, |
| 111 | + }) |
| 112 | + |
| 113 | + let wrapper: VueWrapper<any> |
| 114 | + |
| 115 | + beforeEach(() => { |
| 116 | + wrapper = mount(App) |
| 117 | + }) |
| 118 | + |
| 119 | + afterEach(() => { |
| 120 | + wrapper.unmount() |
| 121 | + }) |
| 122 | + |
| 123 | + it('Should be defined', async () => { |
| 124 | + expect(wrapper).toBeDefined() |
| 125 | + }) |
| 126 | + |
| 127 | + it('Refetch after conditions value changed.', async () => { |
| 128 | + expect(wrapper.text()).toContain('') |
| 129 | + wrapper.vm.conditions.name = 'RUNKIDS' |
| 130 | + await flushPromises() |
| 131 | + expect(wrapper.text()).toContain(`Hello, world! RUNKIDS!`) |
| 132 | + }) |
| 133 | + }) |
| 134 | +} |
| 135 | + |
88 | 136 | // const tick = async (times: number) => { |
89 | 137 | // for (let _ in [...Array(times).keys()]) { |
90 | 138 | // await nextTick() |
|
0 commit comments