diff --git a/packages/react-query/src/__tests__/useQuery.promise.test.tsx b/packages/react-query/src/__tests__/useQuery.promise.test.tsx index ec07d2d2b2..5efc62473a 100644 --- a/packages/react-query/src/__tests__/useQuery.promise.test.tsx +++ b/packages/react-query/src/__tests__/useQuery.promise.test.tsx @@ -12,7 +12,9 @@ import { useQuery, } from '..' import { QueryCache } from '../index' -import { createQueryClient, queryKey, sleep } from './utils' +import { createDeferred, createQueryClient, queryKey, sleep } from './utils' + + describe('useQuery().promise', () => { const queryCache = new QueryCache() @@ -75,11 +77,11 @@ describe('useQuery().promise', () => { withinDOM().getByText('loading..') expect(renderedComponents).toEqual([Page, Loading]) } - + { const { renderedComponents, withinDOM } = await renderStream.takeRender() withinDOM().getByText('test') - expect(renderedComponents).toEqual([Page, MyComponent]) + expect(renderedComponents).toEqual([MyComponent]) } }) @@ -1035,10 +1037,11 @@ describe('useQuery().promise', () => { expect(queryFn).toHaveBeenCalledTimes(0) }) - it('should show correct data when switching between cache entries without re-fetches', async () => { + it.only('should show correct data when switching between cache entries without re-fetches', async () => { const key = queryKey() const renderStream = createRenderStream({ snapshotDOM: true }) + function MyComponent(props: { promise: Promise }) { useTrackRenders() const data = React.use(props.promise) @@ -1091,7 +1094,15 @@ describe('useQuery().promise', () => { expect(renderedComponents).toEqual([MyComponent]) } - rendered.getByText('inc').click() + { + rendered.getByText('inc').click() + + const { renderedComponents, withinDOM } = await renderStream.takeRender() + withinDOM().getByText('test0') + console.log({renderedComponents}) + expect(renderedComponents).toEqual([Page, MyComponent]) + + } { const { renderedComponents, withinDOM } = await renderStream.takeRender() diff --git a/packages/react-query/src/__tests__/utils.tsx b/packages/react-query/src/__tests__/utils.tsx index 7e25177768..29105208b6 100644 --- a/packages/react-query/src/__tests__/utils.tsx +++ b/packages/react-query/src/__tests__/utils.tsx @@ -94,3 +94,16 @@ export function setIsServer(isServer: boolean) { } export const doNotExecute = (_func: () => void) => true + + +export function createDeferred() { + let resolve: (value: TValue) => void; + let reject: (error: unknown) => void; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + + return { promise, resolve: resolve!, reject: reject! }; +} +export type Deferred = ReturnType>; \ No newline at end of file