Skip to content

Commit bc72910

Browse files
committed
chore: playground tests
1 parent 3882994 commit bc72910

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

playground/src/App.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ import type {
55
RouteLocationResolved,
66
RouteLocation,
77
} from 'vue-router'
8-
import { ref } from 'vue'
8+
import { ref, watch } from 'vue'
99
import { routes } from 'vue-router/auto-routes'
10+
import { useMutationState, useQueryClient } from '@tanstack/vue-query'
1011
1112
console.log(`We have ${routes.length} routes.`)
1213
14+
const queryClient = useQueryClient()
15+
const states = useMutationState({ filters: { mutationKey: ['hey'] } })
16+
watch(states, () => {
17+
window.ss = states
18+
})
19+
function clearMutationsCache() {
20+
queryClient.getMutationCache().clear()
21+
}
22+
1323
const router = useRouter()
1424
const route = useRoute()
1525
@@ -43,6 +53,8 @@ function _test() {
4353

4454
<template>
4555
<header>
56+
<pre>{{ states.length }}</pre>
57+
<button @click="clearMutationsCache()">Clear mutations</button>
4658
<div class="wrapper">
4759
<nav>
4860
<ul>

playground/src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ app.use(VueQueryPlugin, {
1717
// debugger
1818
mutation
1919
},
20+
onMutate(variables, mutation) {
21+
return { wow: 'wah' }
22+
},
2023
async onSettled(...args) {
2124
await new Promise((r) => setTimeout(r, 1000))
2225
console.log('global onSettled', ...args)

playground/src/pages/users/tq-query.[id].vue

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ const route = useRoute('/users/[id]')
1919
const simulateError = ref(false)
2020
2121
const enabled = ref(true)
22+
const queryClient = useQueryClient()
23+
// queryClient.prefetchQuery({
24+
// queryKey: ['user-id', computed(() => route.params.id)],
25+
// // queryFn: async () => {
26+
// // await delay(500)
27+
// // return {
28+
// // id: route.params.id,
29+
// // name: 'Edu',
30+
// // when: new Date().toUTCString(),
31+
// // }
32+
// // },
33+
// }).then((val) => {})
2234
2335
// const tt = useQueries({
2436
// queries: [
@@ -43,12 +55,17 @@ const {
4355
error: tqError,
4456
refetch,
4557
} = useQuery({
46-
async queryFn() {
58+
async queryFn({ signal }) {
4759
console.log('[TQ]useUserData', route.fullPath)
60+
signal.addEventListener('abort', (ev) => {
61+
// console.log('[TQ]useUserData aborted', ev)
62+
})
4863
await delay(500)
4964
if (simulateError.value) {
5065
throw new Error('Simulated Error')
5166
}
67+
signal.throwIfAborted()
68+
console.log('✅ returning data')
5269
const user = {
5370
id: route.params.id,
5471
// @ts-expect-error: no param "name"!
@@ -58,29 +75,51 @@ const {
5875
return user
5976
},
6077
queryKey: ['user-id', computed(() => route.params.id)],
61-
staleTime: 5000,
78+
staleTime: 0,
6279
retry: false,
63-
refetchOnMount: false,
80+
refetchOnMount: true,
81+
refetchOnWindowFocus(query) {
82+
console.log('[TQ]refetchOnWindowFocus', query)
83+
return true
84+
},
6485
enabled,
6586
})
6687
88+
let _id = 0
89+
function testRefetch() {
90+
const id = ++_id
91+
console.log(id + ' refetch started')
92+
refetch({ cancelRefetch: true, throwOnError: true }).then(res => {
93+
console.log(id + ' refetch finished', res)
94+
}).catch(err => {
95+
console.log(id + ' refetch error', err)
96+
}).finally(() => {
97+
console.log(id + ' refetch finally')
98+
})
99+
}
100+
101+
67102
const {
68103
data,
69104
error,
70105
mutate,
71106
status: mutSate,
72107
} = useMutation({
73-
mutationKey: ['hey'],
108+
// mutationKey: ['hey'],
109+
networkMode: 'always',
110+
onMutate(vars) {
111+
112+
},
74113
mutationFn: async (id: number) => {
75-
await delay(500)
114+
await delay(5000)
76115
return 'hey'
77116
},
78117
})
79118
</script>
80119

81120
<template>
82121
<main>
83-
<h1>defineQueryLoader()</h1>
122+
<h1>TanStack Query</h1>
84123
<pre>User: {{ route.params.id }}</pre>
85124

86125
<label>
@@ -104,16 +143,13 @@ const {
104143
<input type="checkbox" v-model="simulateError" /> Throw on Fetch
105144
</label>
106145
<br />
107-
<button @click="refetch()">Refresh</button>
146+
<button @click="refetch({ cancelRefetch: false })">Refresh</button>
147+
<button @click="testRefetch()">Refresh 2</button>
108148
</fieldset>
109149

110-
<RouterLink :to="{ params: { id: Number(route.params.id) - 1 } }"
111-
>Previous</RouterLink
112-
>
150+
<RouterLink :to="{ params: { id: Number(route.params.id) - 1 } }">Previous</RouterLink>
113151
|
114-
<RouterLink :to="{ params: { id: Number(route.params.id) + 1 } }"
115-
>Next</RouterLink
116-
>
152+
<RouterLink :to="{ params: { id: Number(route.params.id) + 1 } }">Next</RouterLink>
117153

118154
<h2>TQ</h2>
119155

0 commit comments

Comments
 (0)