Skip to content

Commit 8fd211b

Browse files
committed
test: wip pass trpc as fixture & with vue context
1 parent d3e7f07 commit 8fd211b

File tree

5 files changed

+71
-22
lines changed

5 files changed

+71
-22
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.DS_Store
2+
13
node_modules/
24
dist/
3-
.eslintcache
5+
.eslintcache

test/basic.test.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
import { VueQueryPlugin } from '@tanstack/vue-query'
2-
import { expect, test } from 'vitest'
3-
import { createApp } from 'vue'
1+
/* eslint-disable no-empty-pattern */
2+
import { keepPreviousData, useQuery } from '@tanstack/vue-query'
3+
import { test as base, expect } from 'vitest'
44

5-
import { trpc } from './trpc/client'
5+
import { app, useTRPC } from './vue-app'
66

7-
const app = createApp({})
8-
app.use(VueQueryPlugin)
7+
const test = base.extend<{
8+
trpc: ReturnType<typeof useTRPC>
9+
}>({
10+
trpc: [
11+
async ({}, use) => {
12+
await app.runWithContext(async () => {
13+
const trpc = useTRPC()
14+
await use(trpc)
15+
})
16+
},
17+
{ auto: true },
18+
],
19+
})
20+
21+
test('useQuery()', async ({ trpc }) => {
22+
const _trpc = useTRPC()
923

10-
test('useQuery()', async () => {
11-
await app.runWithContext(async () => {
12-
const pong = trpc.ping.useQuery()
13-
await pong.suspense()
14-
expect(pong.data.value).toEqual('Pong!')
24+
const pong = trpc.ping.useQuery(undefined, {
25+
placeholderData: keepPreviousData,
26+
})
27+
const query = useQuery({
28+
queryKey: ['ping'],
29+
queryFn: () => 2,
30+
placeholderData: keepPreviousData,
1531
})
32+
query.data
33+
pong.data
34+
await pong.suspense()
35+
expect(pong.data.value).toEqual('Pong!')
1636
})

test/trpc/client.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/vue-app.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { VueQueryPlugin, useQueryClient } from '@tanstack/vue-query'
2+
import { httpBatchLink } from '@trpc/client'
3+
import { createApp, inject } from 'vue'
4+
5+
import { createTRPCVueQueryClient } from '../src/index'
6+
7+
import type { AppRouter } from './trpc/index'
8+
import type { InjectionKey } from 'vue'
9+
10+
const trpcKey = Symbol('trpc') as InjectionKey<
11+
ReturnType<typeof createTRPCVueQueryClient<AppRouter>>
12+
>
13+
14+
export const app = createApp({})
15+
16+
app.use(VueQueryPlugin)
17+
app.use({
18+
install() {
19+
const queryClient = app.runWithContext(useQueryClient)
20+
const trpc = createTRPCVueQueryClient<AppRouter>({
21+
queryClient,
22+
trpc: {
23+
links: [httpBatchLink({ url: 'http://localhost:3000' })],
24+
},
25+
})
26+
27+
app.provide(trpcKey, trpc)
28+
},
29+
})
30+
31+
export function useTRPC() {
32+
const trpc = inject(trpcKey)
33+
if (!trpc) throw new Error('tRPC client is not available.')
34+
35+
return trpc
36+
}

vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig } from 'vitest/config'
22

33
export default defineConfig({
44
test: {
5-
setupFiles: ['./test/setup.ts'],
5+
// setupFiles: ['./test/setup.ts'],
66
// environment: 'happy-dom',
77
typecheck: {
88
enabled: true,

0 commit comments

Comments
 (0)