|
| 1 | +import { createApi } from '@reduxjs/toolkit/query' |
| 2 | +import { setupApiStore, waitMs } from './helpers' |
| 3 | + |
| 4 | +const mockBaseQuery = jest |
| 5 | + .fn() |
| 6 | + .mockImplementation((args: any) => ({ data: args })) |
| 7 | + |
| 8 | +const api = createApi({ |
| 9 | + baseQuery: mockBaseQuery, |
| 10 | + tagTypes: ['Posts'], |
| 11 | + endpoints: (build) => ({ |
| 12 | + getPosts: build.query<unknown, number>({ |
| 13 | + query(pageNumber) { |
| 14 | + return { url: 'posts', params: pageNumber } |
| 15 | + }, |
| 16 | + providesTags: ['Posts'], |
| 17 | + }), |
| 18 | + }), |
| 19 | +}) |
| 20 | +const { getPosts } = api.endpoints |
| 21 | + |
| 22 | +const storeRef = setupApiStore(api) |
| 23 | + |
| 24 | +const getSubscribersForQueryCacheKey = (queryCacheKey: string) => |
| 25 | + storeRef.store.getState()[api.reducerPath].subscriptions[queryCacheKey] || {} |
| 26 | +const createSubscriptionGetter = (queryCacheKey: string) => () => |
| 27 | + getSubscribersForQueryCacheKey(queryCacheKey) |
| 28 | + |
| 29 | +describe('polling tests', () => { |
| 30 | + it('clears intervals when seeing a resetApiState action', async () => { |
| 31 | + await storeRef.store.dispatch( |
| 32 | + getPosts.initiate(1, { |
| 33 | + subscriptionOptions: { pollingInterval: 10 }, |
| 34 | + subscribe: true, |
| 35 | + }) |
| 36 | + ) |
| 37 | + |
| 38 | + expect(mockBaseQuery).toHaveBeenCalledTimes(1) |
| 39 | + |
| 40 | + storeRef.store.dispatch(api.util.resetApiState()) |
| 41 | + |
| 42 | + await waitMs(30) |
| 43 | + |
| 44 | + expect(mockBaseQuery).toHaveBeenCalledTimes(1) |
| 45 | + }) |
| 46 | + |
| 47 | + it('replaces polling interval when the subscription options are updated', async () => { |
| 48 | + const { requestId, queryCacheKey, ...subscription } = |
| 49 | + storeRef.store.dispatch( |
| 50 | + getPosts.initiate(1, { |
| 51 | + subscriptionOptions: { pollingInterval: 10 }, |
| 52 | + subscribe: true, |
| 53 | + }) |
| 54 | + ) |
| 55 | + |
| 56 | + const getSubs = createSubscriptionGetter(queryCacheKey) |
| 57 | + |
| 58 | + expect(Object.keys(getSubs())).toHaveLength(1) |
| 59 | + expect(getSubs()[requestId].pollingInterval).toBe(10) |
| 60 | + |
| 61 | + subscription.updateSubscriptionOptions({ pollingInterval: 20 }) |
| 62 | + |
| 63 | + expect(Object.keys(getSubs())).toHaveLength(1) |
| 64 | + expect(getSubs()[requestId].pollingInterval).toBe(20) |
| 65 | + }) |
| 66 | + |
| 67 | + it(`doesn't replace the interval when removing a shared query instance with a poll `, async () => { |
| 68 | + const subscriptionOne = storeRef.store.dispatch( |
| 69 | + getPosts.initiate(1, { |
| 70 | + subscriptionOptions: { pollingInterval: 10 }, |
| 71 | + subscribe: true, |
| 72 | + }) |
| 73 | + ) |
| 74 | + |
| 75 | + storeRef.store.dispatch( |
| 76 | + getPosts.initiate(1, { |
| 77 | + subscriptionOptions: { pollingInterval: 10 }, |
| 78 | + subscribe: true, |
| 79 | + }) |
| 80 | + ) |
| 81 | + |
| 82 | + const getSubs = createSubscriptionGetter(subscriptionOne.queryCacheKey) |
| 83 | + |
| 84 | + expect(Object.keys(getSubs())).toHaveLength(2) |
| 85 | + |
| 86 | + subscriptionOne.unsubscribe() |
| 87 | + |
| 88 | + expect(Object.keys(getSubs())).toHaveLength(1) |
| 89 | + }) |
| 90 | + |
| 91 | + it('uses lowest specified interval when two components are mounted', async () => { |
| 92 | + storeRef.store.dispatch( |
| 93 | + getPosts.initiate(1, { |
| 94 | + subscriptionOptions: { pollingInterval: 30000 }, |
| 95 | + subscribe: true, |
| 96 | + }) |
| 97 | + ) |
| 98 | + |
| 99 | + storeRef.store.dispatch( |
| 100 | + getPosts.initiate(1, { |
| 101 | + subscriptionOptions: { pollingInterval: 10 }, |
| 102 | + subscribe: true, |
| 103 | + }) |
| 104 | + ) |
| 105 | + |
| 106 | + await waitMs(20) |
| 107 | + |
| 108 | + expect(mockBaseQuery.mock.calls.length).toBeGreaterThanOrEqual(2) |
| 109 | + }) |
| 110 | +}) |
0 commit comments