Skip to content

Commit

Permalink
Add tests for 'skipPollOnFocusLost' option
Browse files Browse the repository at this point in the history
New test cases have been added for the 'skipPollOnFocusLost' option in query polling. Specifically, these tests verify the correct behaviour when this option is toggled, when it's reset on mount, and when it's changed via subscription options update.
  • Loading branch information
riqts committed Jan 24, 2024
1 parent d1b9971 commit e7fce61
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/toolkit/src/query/tests/polling.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,85 @@ describe('polling tests', () => {

expect(mockBaseQuery.mock.calls.length).toBeGreaterThanOrEqual(2)
})

it('respects skipPollOnFocusLost', async () => {
storeRef.store.dispatch(
getPosts.initiate(1, {
subscriptionOptions: { pollingInterval: 10, skipPollOnFocusLost: true },
subscribe: true,
})
)

await delay(20)
const callsWithSkip = mockBaseQuery.mock.calls.length

storeRef.store.dispatch(
getPosts.initiate(1, {
subscriptionOptions: {
pollingInterval: 10,
skipPollOnFocusLost: false,
},
subscribe: true,
})
)

await delay(30)
const callsWithoutSkip = mockBaseQuery.mock.calls.length

expect(callsWithSkip).toBe(1)
expect(callsWithoutSkip).toBeGreaterThan(2)
})

it('replaces skipPollOnFocusLost with most recent mount', async () => {
storeRef.store.dispatch(
getPosts.initiate(1, {
subscriptionOptions: {
pollingInterval: 10,
skipPollOnFocusLost: false,
},
subscribe: true,
})
)

await delay(50)
const callsWithSkip = mockBaseQuery.mock.calls.length

storeRef.store.dispatch(
getPosts.initiate(1, {
subscriptionOptions: { pollingInterval: 15, skipPollOnFocusLost: true },
subscribe: true,
})
)

await delay(50)
const callsWithoutSkip = mockBaseQuery.mock.calls.length

expect(callsWithSkip).toBeGreaterThan(2)
expect(callsWithoutSkip).toBe(callsWithSkip + 1)
})

it('replaces skipPollOnFocusLost when the subscription options are updated', async () => {
const { requestId, queryCacheKey, ...subscription } =
storeRef.store.dispatch(
getPosts.initiate(1, {
subscriptionOptions: { pollingInterval: 10 },
subscribe: true,
})
)

const getSubs = createSubscriptionGetter(queryCacheKey)

await delay(1)
expect(Object.keys(getSubs())).toHaveLength(1)
expect(getSubs()[requestId].skipPollOnFocusLost).toBe(false)

subscription.updateSubscriptionOptions({
pollingInterval: 20,
skipPollOnFocusLost: true,
})

await delay(1)
expect(Object.keys(getSubs())).toHaveLength(1)
expect(getSubs()[requestId].skipPollOnFocusLost).toBe(true)
})
})

0 comments on commit e7fce61

Please sign in to comment.