Skip to content

Commit e33cc6f

Browse files
committed
chore: add test
1 parent 42897ed commit e33cc6f

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

test/use-condition-watcher.test.ts

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
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'
33
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'
66

77
describe('Basic test of vue-condition-watcher', () => {
8-
let basicTestConfig = {}
8+
let basicTestConfig: any = {}
99
beforeEach(() => {
1010
basicTestConfig = {
11-
fetcher: (params) => new Promise((resolve) => resolve(params)),
11+
fetcher: (params) => Promise.resolve(params),
1212
conditions: {
1313
gender: ['male'],
1414
results: 9,
1515
},
16+
defaultParams: {
17+
name: 'runkids',
18+
},
1619
}
1720
})
1821

1922
it(`Check return value type`, () => {
20-
const { conditions, data, error, isLoading, execute } = useConditionWatcher(basicTestConfig)
23+
const { conditions, data, error, isLoading, execute, isFetching } = useConditionWatcher(basicTestConfig)
2124

2225
expect(isReactive(conditions)).toBeTruthy()
2326
expect(isRef(data)).toBeTruthy()
2427
expect(isRef(error)).toBeTruthy()
2528
expect(isRef(isLoading)).toBeTruthy()
29+
expect(isRef(isFetching)).toBeTruthy()
2630
expect(isLoading.value).toBeTypeOf('boolean')
31+
expect(isLoading.value).toBe(true)
32+
expect(isFetching.value).toBe(false)
2733
expect(execute).toBeTypeOf('function')
2834
})
2935

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)
3238

3339
expect(isReadonly(data)).toBeTruthy()
3440
expect(isReadonly(error)).toBeTruthy()
3541
expect(isReadonly(isLoading)).toBeTruthy()
42+
expect(isReadonly(isFetching)).toBeTruthy()
3643
})
3744

3845
it(`Condition should be change`, () => {
@@ -85,6 +92,47 @@ describe('Basic test of vue-condition-watcher', () => {
8592
})
8693
})
8794

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+
88136
// const tick = async (times: number) => {
89137
// for (let _ in [...Array(times).keys()]) {
90138
// await nextTick()

0 commit comments

Comments
 (0)