Skip to content

Commit

Permalink
Harden optimisticUpserts test
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Nov 28, 2024
1 parent ef84607 commit 23a1c3f
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions packages/toolkit/src/query/tests/optimisticUpserts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,27 +399,34 @@ describe('upsertQueryEntries', () => {
storeRef.store.dispatch(entriesAction)

// Tricky timing. The cache data promises will be resolved
// in microtasks, so we just need any async delay here.
await delay(10)
// in microtasks. We need to wait for them. Best to do this
// in a loop just to avoid a hardcoded delay, but also this
// needs to complete before `keepUnusedDataFor` expires them.
await waitFor(
() => {
const state = storeRef.store.getState()

// onCacheEntryAdded should have run for each post,
// including cache data being resolved
for (const post of posts) {
const matchingSideEffectAction = state.actions.find(
(action) =>
postAddedAction.match(action) && action.payload === post.id,
)
expect(matchingSideEffectAction).toBeTruthy()
}

const state = storeRef.store.getState()
const selectedData =
api.endpoints.postWithSideEffect.select('1')(state).data

// onCacheEntryAdded should have run for each post,
// including cache data being resolved
for (const post of posts) {
const matchingSideEffectAction = state.actions.find(
(action) => postAddedAction.match(action) && action.payload === post.id,
)
expect(matchingSideEffectAction).toBeTruthy()
}

expect(api.endpoints.postWithSideEffect.select('1')(state).data).toBe(
posts[0],
expect(selectedData).toBe(posts[0])
},
{ timeout: 50, interval: 5 },
)

// The cache data should be removed after the keepUnusedDataFor time,
// so wait longer than that
await delay(20)
await delay(100)

const stateAfter = storeRef.store.getState()

Expand Down

0 comments on commit 23a1c3f

Please sign in to comment.