|
1 | 1 | import { QueryClient, useQuery, useQueryClient, VueQueryPlugin } from '@tanstack/vue-query-v4' |
2 | 2 | import { waitFor } from '@testing-library/vue' |
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import { inject } from 'vue-demi' |
4 | 5 | import { cleanUp, mountSetup } from '../../test/vue-mount' |
5 | 6 | import { useQueryCallbacks } from './index' |
6 | 7 |
|
@@ -144,6 +145,38 @@ describe('vue (v4)', () => { |
144 | 145 | expect(onSuccess).toBeCalledTimes(1) |
145 | 146 | expect(onSuccess).toBeCalledWith('bar') |
146 | 147 | }) |
| 148 | + |
| 149 | + it('should call onSuccess with `queryClientKey`', async () => { |
| 150 | + const onSuccess = vi.fn() |
| 151 | + const QUERY_KEY = ['foo'] |
| 152 | + const queryClientKey = '_vue_query_' |
| 153 | + |
| 154 | + const { result: query } = mountSetup(() => { |
| 155 | + const queryClient = inject(queryClientKey) as QueryClient |
| 156 | + const result = useQuery({ |
| 157 | + queryKey: QUERY_KEY, |
| 158 | + queryFn: () => Promise.resolve('bar'), |
| 159 | + queryClient, |
| 160 | + }) |
| 161 | + |
| 162 | + useQueryCallbacks({ |
| 163 | + queryKey: QUERY_KEY, |
| 164 | + queryClientKey, |
| 165 | + onSuccess, |
| 166 | + }) |
| 167 | + |
| 168 | + return result |
| 169 | + }, (app) => { |
| 170 | + app.provide(queryClientKey, new QueryClient()) |
| 171 | + }) |
| 172 | + |
| 173 | + expect(query.data.value).toBeUndefined() |
| 174 | + await waitFor(() => expect(query.data.value).not.toBeUndefined()) |
| 175 | + |
| 176 | + expect(query.data.value).toBe('bar') |
| 177 | + expect(onSuccess).toBeCalledTimes(1) |
| 178 | + expect(onSuccess).toBeCalledWith('bar') |
| 179 | + }) |
147 | 180 | }) |
148 | 181 |
|
149 | 182 | function useQueryClientSetup<T>( |
|
0 commit comments