Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Dec 13, 2024
1 parent 7d3fccc commit bc36411
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/react-query/src/__tests__/useQuery.promise.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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])
}
})

Expand Down Expand Up @@ -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<string> }) {
useTrackRenders()
const data = React.use(props.promise)
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions packages/react-query/src/__tests__/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ export function setIsServer(isServer: boolean) {
}

export const doNotExecute = (_func: () => void) => true


export function createDeferred<TValue>() {
let resolve: (value: TValue) => void;
let reject: (error: unknown) => void;
const promise = new Promise<TValue>((res, rej) => {
resolve = res;
reject = rej;
});

return { promise, resolve: resolve!, reject: reject! };
}
export type Deferred<TValue> = ReturnType<typeof createDeferred<TValue>>;

0 comments on commit bc36411

Please sign in to comment.