Skip to content

Commit b64d44b

Browse files
committed
test: refreshdeps auto
1 parent c69c86f commit b64d44b

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { sleep } from 'test-utils/sleep'
2+
import renderHook from 'test-utils/renderHook'
3+
import { reactive, ref } from 'vue'
4+
import useRequest from '../useRequest'
5+
6+
let text = ''
7+
let text1 = ''
8+
function getUsername({ id, storeId }: { id: number; storeId: number }): Promise<string> {
9+
return new Promise(resolve => {
10+
setTimeout(() => {
11+
text = `${String(Date.now())}; \t 参数id: ${id} \t; 参数storeId: ${storeId}`
12+
text1 = `${String(Date.now())}; \t 参数id: ${id} \t; 参数storeId: ${storeId}`
13+
resolve(`${String(Date.now())}; \t 参数id: ${id} \t; 参数storeId: ${storeId}`)
14+
}, 1000)
15+
})
16+
}
17+
18+
describe('useRequest/RefreshDeps', () => {
19+
const id = ref(1)
20+
const store = reactive({
21+
id: 1,
22+
})
23+
const [{ data }] = renderHook(() =>
24+
useRequest(() => getUsername({ id: id.value, storeId: store.id }), {
25+
refreshDeps: true,
26+
}),
27+
)
28+
29+
let prevDataText = ''
30+
31+
it('should init', () => {
32+
expect(id.value).toBe(1)
33+
expect(store.id).toBe(1)
34+
})
35+
36+
it('change id,id dependency unchanged will not request', async () => {
37+
await sleep(1000)
38+
await sleep(100)
39+
prevDataText = text
40+
expect(prevDataText === data?.value).toBeTruthy()
41+
id.value = 1
42+
await sleep(1000)
43+
expect(data?.value === prevDataText).toBeTruthy()
44+
})
45+
46+
it('change id', async () => {
47+
id.value = 2
48+
await sleep(1000)
49+
await sleep(100)
50+
expect(data?.value === prevDataText).toBeFalsy()
51+
prevDataText = data?.value ?? ''
52+
53+
id.value = 1
54+
await sleep(1000)
55+
await sleep(100)
56+
expect(data?.value === prevDataText).toBeFalsy()
57+
})
58+
59+
it('change store id,store id dependency unchanged will not request', async () => {
60+
prevDataText = data?.value ?? ''
61+
store.id = 1
62+
await sleep(1000)
63+
await sleep(100)
64+
expect(data?.value === prevDataText).toBeTruthy()
65+
})
66+
67+
it('change store id', async () => {
68+
prevDataText = data?.value ?? ''
69+
store.id = 2
70+
await sleep(1000)
71+
await sleep(100)
72+
expect(data?.value === prevDataText).toBeFalsy()
73+
prevDataText = data?.value ?? ''
74+
store.id = 1
75+
await sleep(1000)
76+
await sleep(100)
77+
expect(data?.value === prevDataText).toBeFalsy()
78+
})
79+
})
80+
81+
// describe('useRequest/RefreshDeps-auto', () => {
82+
// const id = ref(1)
83+
// const store = reactive({
84+
// id: 1,
85+
// })
86+
// const [{ data }] = renderHook(() =>
87+
// useRequest(() => getUsername({ id: id.value, storeId: store.id }), {
88+
// refreshDeps: true,
89+
// }),
90+
// )
91+
92+
// let prevDataText = ''
93+
94+
// it('should init', () => {
95+
// expect(id.value).toBe(1)
96+
// expect(store.id).toBe(1)
97+
// })
98+
99+
// it('change id,id dependency unchanged will not request', async () => {
100+
// await sleep(1000)
101+
// await sleep(100)
102+
// prevDataText = text1
103+
// expect(prevDataText === data?.value).toBeTruthy()
104+
// id.value = 1
105+
// await sleep(1000)
106+
// expect(data?.value === prevDataText).toBeTruthy()
107+
// })
108+
109+
// it('change id', async () => {
110+
// id.value = 2
111+
// await sleep(1000)
112+
// await sleep(100)
113+
// expect(data?.value === prevDataText).toBeFalsy()
114+
// prevDataText = data?.value ?? ''
115+
116+
// id.value = 1
117+
// await sleep(1000)
118+
// await sleep(100)
119+
// expect(data?.value === prevDataText).toBeFalsy()
120+
// })
121+
122+
// it('change store id,store id dependency unchanged will not request', async () => {
123+
// prevDataText = data?.value ?? ''
124+
// store.id = 1
125+
// await sleep(1000)
126+
// await sleep(100)
127+
// expect(data?.value === prevDataText).toBeTruthy()
128+
// })
129+
130+
// it('change store id', async () => {
131+
// prevDataText = data?.value ?? ''
132+
// store.id = 2
133+
// await sleep(1000)
134+
// await sleep(100)
135+
// expect(data?.value === prevDataText).toBeFalsy()
136+
// prevDataText = data?.value ?? ''
137+
// store.id = 1
138+
// await sleep(1000)
139+
// await sleep(100)
140+
// expect(data?.value === prevDataText).toBeFalsy()
141+
// })
142+
// })

packages/hooks/src/useRequest/__tests__/refreshDeps.spec.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { reactive, ref } from 'vue'
44
import useRequest from '../useRequest'
55

66
let text = ''
7+
let text1 = ''
78
function getUsername({ id, storeId }: { id: number; storeId: number }): Promise<string> {
89
return new Promise(resolve => {
910
setTimeout(() => {
1011
text = `${String(Date.now())}; \t 参数id: ${id} \t; 参数storeId: ${storeId}`
12+
text1 = `${String(Date.now())}; \t 参数id: ${id} \t; 参数storeId: ${storeId}`
1113
resolve(`${String(Date.now())}; \t 参数id: ${id} \t; 参数storeId: ${storeId}`)
1214
}, 1000)
1315
})
@@ -75,3 +77,66 @@ describe('useRequest/RefreshDeps', () => {
7577
expect(data?.value === prevDataText).toBeFalsy()
7678
})
7779
})
80+
81+
// describe('useRequest/RefreshDeps-auto', () => {
82+
// const id = ref(1)
83+
// const store = reactive({
84+
// id: 1,
85+
// })
86+
// const [{ data }] = renderHook(() =>
87+
// useRequest(() => getUsername({ id: id.value, storeId: store.id }), {
88+
// refreshDeps: true,
89+
// }),
90+
// )
91+
92+
// let prevDataText = ''
93+
94+
// it('should init', () => {
95+
// expect(id.value).toBe(1)
96+
// expect(store.id).toBe(1)
97+
// })
98+
99+
// it('change id,id dependency unchanged will not request', async () => {
100+
// await sleep(1000)
101+
// await sleep(100)
102+
// prevDataText = text1
103+
// expect(prevDataText === data?.value).toBeTruthy()
104+
// id.value = 1
105+
// await sleep(1000)
106+
// expect(data?.value === prevDataText).toBeTruthy()
107+
// })
108+
109+
// it('change id', async () => {
110+
// id.value = 2
111+
// await sleep(1000)
112+
// await sleep(100)
113+
// expect(data?.value === prevDataText).toBeFalsy()
114+
// prevDataText = data?.value ?? ''
115+
116+
// id.value = 1
117+
// await sleep(1000)
118+
// await sleep(100)
119+
// expect(data?.value === prevDataText).toBeFalsy()
120+
// })
121+
122+
// it('change store id,store id dependency unchanged will not request', async () => {
123+
// prevDataText = data?.value ?? ''
124+
// store.id = 1
125+
// await sleep(1000)
126+
// await sleep(100)
127+
// expect(data?.value === prevDataText).toBeTruthy()
128+
// })
129+
130+
// it('change store id', async () => {
131+
// prevDataText = data?.value ?? ''
132+
// store.id = 2
133+
// await sleep(1000)
134+
// await sleep(100)
135+
// expect(data?.value === prevDataText).toBeFalsy()
136+
// prevDataText = data?.value ?? ''
137+
// store.id = 1
138+
// await sleep(1000)
139+
// await sleep(100)
140+
// expect(data?.value === prevDataText).toBeFalsy()
141+
// })
142+
// })

0 commit comments

Comments
 (0)