-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
245 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/tests/server/regression/issue-2996-defined-data.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { getServerAndReactClient } from '../react/__reactHelpers'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { render, waitFor } from '@testing-library/react'; | ||
import { initTRPC } from '@trpc/server'; | ||
import { expectTypeOf } from 'expect-type'; | ||
import { konn } from 'konn'; | ||
import React from 'react'; | ||
|
||
const posts = [ | ||
{ id: 1, title: 'foo' }, | ||
{ id: 2, title: 'bar' }, | ||
]; | ||
type Post = typeof posts[number]; | ||
|
||
const fetchPosts = async () => posts; | ||
|
||
const ctx = konn() | ||
.beforeEach(() => { | ||
const t = initTRPC.create(); | ||
|
||
const appRouter = t.router({ | ||
posts: t.procedure.query(fetchPosts), | ||
}); | ||
|
||
return getServerAndReactClient(appRouter); | ||
}) | ||
.afterEach(async (ctx) => { | ||
await ctx?.close?.(); | ||
}) | ||
.done(); | ||
|
||
test('destructuring data', async () => { | ||
const { App, proxy } = ctx; | ||
|
||
function MyComponent() { | ||
const { data: trpcData = [] } = proxy.posts.useQuery(); | ||
expectTypeOf<typeof trpcData>().toEqualTypeOf<Post[]>(); | ||
|
||
// verify tanstack returns the same | ||
const { data: rqData = [] } = useQuery(['key'], fetchPosts); | ||
expectTypeOf<typeof rqData>().toEqualTypeOf<Post[]>(); | ||
|
||
if (!trpcData) throw new Error('should not happen'); | ||
|
||
if (trpcData.length === 0) return <div>No posts</div>; | ||
return <div>{trpcData.map((post) => post.title).join(', ')}</div>; | ||
} | ||
|
||
const utils = render( | ||
<App> | ||
<MyComponent /> | ||
</App>, | ||
); | ||
|
||
expect(utils.container).toHaveTextContent('No posts'); | ||
await waitFor(() => { | ||
expect(utils.container).toHaveTextContent('foo, bar'); | ||
}); | ||
}); | ||
|
||
test('using `initialData`', async () => { | ||
const { App, proxy } = ctx; | ||
|
||
function MyComponent() { | ||
const { data: trpcData } = proxy.posts.useQuery(undefined, { | ||
initialData: [], | ||
}); | ||
expectTypeOf<typeof trpcData>().toEqualTypeOf<Post[]>(); | ||
|
||
// verify tanstack returns the same | ||
const { data: rqData } = useQuery(['key'], fetchPosts, { | ||
initialData: [], | ||
}); | ||
expectTypeOf<typeof rqData>().toEqualTypeOf<Post[]>(); | ||
|
||
if (!trpcData) throw new Error('should not happen'); | ||
|
||
if (trpcData.length === 0) return <div>No posts</div>; | ||
return <div>{trpcData.map((post) => post.title).join(', ')}</div>; | ||
} | ||
|
||
const utils = render( | ||
<App> | ||
<MyComponent /> | ||
</App>, | ||
); | ||
|
||
expect(utils.container).toHaveTextContent('No posts'); | ||
await waitFor(() => { | ||
expect(utils.container).toHaveTextContent('foo, bar'); | ||
}); | ||
}); | ||
|
||
test('using `placeholderData`', async () => { | ||
const { App, proxy } = ctx; | ||
|
||
function MyComponent() { | ||
const { data: trpcData } = proxy.posts.useQuery(undefined, { | ||
placeholderData: [], | ||
}); | ||
expectTypeOf<typeof trpcData>().toEqualTypeOf<Post[] | undefined>(); | ||
|
||
// verify tanstack returns the same | ||
const { data: rqData } = useQuery(['key'], fetchPosts, { | ||
placeholderData: [], | ||
}); | ||
expectTypeOf<typeof rqData>().toEqualTypeOf<Post[] | undefined>(); | ||
|
||
if (!trpcData) throw new Error('should not happen'); | ||
|
||
if (trpcData.length === 0) return <div>No posts</div>; | ||
return <div>{trpcData.map((post) => post.title).join(', ')}</div>; | ||
} | ||
|
||
const utils = render( | ||
<App> | ||
<MyComponent /> | ||
</App>, | ||
); | ||
|
||
expect(utils.container).toHaveTextContent('No posts'); | ||
await waitFor(() => { | ||
expect(utils.container).toHaveTextContent('foo, bar'); | ||
}); | ||
}); |