Skip to content

Commit

Permalink
Merge pull request #4364 from smacpherson64/master
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored May 9, 2024
2 parents 5ec40d8 + e5a9962 commit 5d77624
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
// isFetching = true any time a request is in flight
const isFetching = currentState.isLoading
// isLoading = true only when loading while no data is present yet (initial load with no data in the cache)
const isLoading = !hasData && isFetching
const isLoading = (!lastResult || lastResult.isLoading || lastResult.isUninitialized) && !hasData && isFetching
// isSuccess = true when data is present
const isSuccess = currentState.isSuccess || (isFetching && hasData)

Expand Down
51 changes: 51 additions & 0 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,57 @@ describe('hooks tests', () => {
}
})

test('Hook subscription failures do not reset isLoading state', async () => {
const states: boolean[] = []

function Parent() {
const { isLoading } = api.endpoints.getUserAndForceError.useQuery(1)

// Collect loading states to verify that it does not revert back to true.
states.push(isLoading)

// Parent conditionally renders child when loading.
if (isLoading) return null

return <Child />
}

function Child() {
// Using the same args as the parent
api.endpoints.getUserAndForceError.useQuery(1)

return null
}

render(<Parent />, { wrapper: storeRef.wrapper })

// Allow at least three state effects to hit.
// Trying to see if any [true, false, true] occurs.
await act(async () => {
await waitMs(1)
})

await act(async () => {
await waitMs(1)
})

await act(async () => {
await waitMs(1)
})

// Find if at any time the isLoading state has reverted
// E.G.: `[..., true, false, ..., true]`
// ^^^^ ^^^^^ ^^^^
const firstTrue = states.indexOf(true)
const firstFalse = states.slice(firstTrue).indexOf(false)
const revertedState = states.slice(firstFalse).indexOf(true)

expect(
revertedState,
`Expected isLoading state to never revert back to true but did after ${revertedState} renders...`,
).toBe(-1)
})

describe('Hook middleware requirements', () => {
let mock: MockInstance

Expand Down

0 comments on commit 5d77624

Please sign in to comment.