Skip to content

Commit

Permalink
Removed redundant createListenerMiddleware from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
riqts committed Jan 24, 2024
1 parent b9d37c3 commit e676ee8
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions packages/toolkit/src/query/tests/polling.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createApi } from '@reduxjs/toolkit/query'
import { delay } from 'msw'
import { setupApiStore } from './helpers'
import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
import { createListenerMiddleware } from '@reduxjs/toolkit'

const mockBaseQuery = vi
.fn()
Expand Down Expand Up @@ -126,26 +125,18 @@ describe('polling tests', () => {

it('respects skipPollOnFocusLost', async () => {
mockBaseQuery.mockClear()
const listenerMiddleware = createListenerMiddleware()
const storeListenerRef = setupApiStore(api, undefined, {
middleware: {
concat: [listenerMiddleware.middleware],
},
withoutTestLifecycles: true,
})

storeListenerRef.store.dispatch(
storeRef.store.dispatch(
getPosts.initiate(2, {
subscriptionOptions: { pollingInterval: 10, skipPollOnFocusLost: true },
subscribe: true,
})
)
storeListenerRef.store.dispatch(api.internalActions?.onFocusLost())
storeRef.store.dispatch(api.internalActions?.onFocusLost())

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

storeListenerRef.store.dispatch(
storeRef.store.dispatch(
getPosts.initiate(2, {
subscriptionOptions: {
pollingInterval: 10,
Expand All @@ -155,27 +146,19 @@ describe('polling tests', () => {
})
)

storeListenerRef.store.dispatch(api.internalActions?.onFocus())
storeRef.store.dispatch(api.internalActions?.onFocus())

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

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

storeListenerRef.store.dispatch(api.util.resetApiState())
storeRef.store.dispatch(api.util.resetApiState())
})

it('respects skipPollOnFocusLost of the most recent mounted subscription', async () => {
const listenerMiddleware = createListenerMiddleware()
const storeListenerRef = setupApiStore(api, undefined, {
middleware: {
concat: [listenerMiddleware.middleware],
},
withoutTestLifecycles: true,
})

storeListenerRef.store.dispatch(
storeRef.store.dispatch(
getPosts.initiate(3, {
subscriptionOptions: {
pollingInterval: 10,
Expand All @@ -188,14 +171,14 @@ describe('polling tests', () => {
await delay(50)
const callsWithSkip = mockBaseQuery.mock.calls.length

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

storeListenerRef.store.dispatch(api.internalActions?.onFocusLost())
storeRef.store.dispatch(api.internalActions?.onFocusLost())

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

0 comments on commit e676ee8

Please sign in to comment.