| 1 |
import { setupApiStore } from '@internal/tests/utils/helpers' |
| 2 |
import { createApi } from '../core' |
| 3 |
import type { SubscriptionSelectors } from '../core/buildMiddleware/types' |
| 4 |
import { fakeBaseQuery } from '../fakeBaseQuery' |
| 5 |
|
| 6 |
let calls = 0 |
| 7 |
const api = createApi({ |
| 8 |
baseQuery: fakeBaseQuery(), |
| 9 |
endpoints: (build) => ({ |
| 10 |
increment: build.query<number, void>({ |
| 11 |
async queryFn() { |
| 12 |
const data = calls++ |
| 13 |
await Promise.resolve() |
| 14 |
return { data } |
| 15 |
}, |
| 16 |
}), |
| 17 |
failing: build.query<void, void>({ |
| 18 |
async queryFn() { |
| 19 |
await Promise.resolve() |
| 20 |
return { error: { status: 500, data: 'error' } } |
| 21 |
}, |
| 22 |
}), |
| 23 |
}), |
| 24 |
}) |
| 25 |
|
| 26 |
const storeRef = setupApiStore(api) |
| 27 |
|
| 28 |
let getSubscriptions: SubscriptionSelectors['getSubscriptions'] |
| 29 |
let isRequestSubscribed: SubscriptionSelectors['isRequestSubscribed'] |
| 30 |
|
| 31 |
beforeEach(() => { |
| 32 |
;({ getSubscriptions, isRequestSubscribed } = storeRef.store.dispatch( |
| 33 |
api.internalActions.internal_getRTKQSubscriptions(), |
| 34 |
) as unknown as SubscriptionSelectors) |
| 35 |
}) |
| 36 |
|
| 37 |
test('multiple synchonrous initiate calls with pre-existing cache entry', async () => { |
| 38 |
const { store, api } = storeRef |
| 39 |
// seed the store |
| 40 |
const firstValue = await store.dispatch(api.endpoints.increment.initiate()) |
| 41 |
|
| 42 |
expect(firstValue).toMatchObject({ data: 0, status: 'fulfilled' }) |
| 43 |
|
| 44 |
// dispatch another increment |
| 45 |
const secondValuePromise = store.dispatch(api.endpoints.increment.initiate()) |
| 46 |
// and one with a forced refresh |
| 47 |
const thirdValuePromise = store.dispatch( |
| 48 |
api.endpoints.increment.initiate(undefined, { forceRefetch: true }), |
| 49 |
) |
| 50 |
// and another increment |
| 51 |
const fourthValuePromise = store.dispatch(api.endpoints.increment.initiate()) |
| 52 |
|
| 53 |
const secondValue = await secondValuePromise |
| 54 |
const thirdValue = await thirdValuePromise |
| 55 |
const fourthValue = await fourthValuePromise |
| 56 |
|
| 57 |
expect(secondValue).toMatchObject({ |
| 58 |
data: firstValue.data, |
| 59 |
status: 'fulfilled', |
| 60 |
requestId: firstValue.requestId, |
| 61 |
}) |
| 62 |
|
| 63 |
expect(thirdValue).toMatchObject({ data: 1, status: 'fulfilled' }) |
| 64 |
expect(thirdValue.requestId).not.toBe(firstValue.requestId) |
| 65 |
expect(fourthValue).toMatchObject({ |
| 66 |
data: thirdValue.data, |
| 67 |
status: 'fulfilled', |
| 68 |
requestId: thirdValue.requestId, |
| 69 |
}) |
| 70 |
}) |
| 71 |
|
| 72 |
describe('calling initiate without a cache entry, with subscribe: false still returns correct values', () => { |
| 73 |
test('successful query', async () => { |
| 74 |
const { store, api } = storeRef |
| 75 |
calls = 0 |
| 76 |
const promise = store.dispatch( |
| 77 |
api.endpoints.increment.initiate(undefined, { subscribe: false }), |
| 78 |
) |
| 79 |
expect(isRequestSubscribed('increment(undefined)', promise.requestId)).toBe( |
| 80 |
false, |
| 81 |
) |
| 82 |
expect( |
| 83 |
isRequestSubscribed( |
| 84 |
'increment(undefined)', |
| 85 |
`${promise.requestId}_running`, |
| 86 |
), |
| 87 |
).toBe(true) |
| 88 |
|
| 89 |
await expect(promise).resolves.toMatchObject({ |
| 90 |
data: 0, |
| 91 |
status: 'fulfilled', |
| 92 |
}) |
| 93 |
expect( |
| 94 |
isRequestSubscribed( |
| 95 |
'increment(undefined)', |
| 96 |
`${promise.requestId}_running`, |
| 97 |
), |
| 98 |
).toBe(false) |
| 99 |
}) |
| 100 |
|
| 101 |
test('rejected query', async () => { |
| 102 |
const { store, api } = storeRef |
| 103 |
calls = 0 |
| 104 |
const promise = store.dispatch( |
| 105 |
api.endpoints.failing.initiate(undefined, { subscribe: false }), |
| 106 |
) |
| 107 |
expect(isRequestSubscribed('failing(undefined)', promise.requestId)).toBe( |
| 108 |
false, |
| 109 |
) |
| 110 |
expect( |
| 111 |
isRequestSubscribed('failing(undefined)', `${promise.requestId}_running`), |
| 112 |
).toBe(true) |
| 113 |
|
| 114 |
await expect(promise).resolves.toMatchObject({ |
| 115 |
status: 'rejected', |
| 116 |
}) |
| 117 |
expect( |
| 118 |
isRequestSubscribed('failing(undefined)', `${promise.requestId}_running`), |
| 119 |
).toBe(false) |
| 120 |
}) |
| 121 |
}) |
| 122 |
|
| 123 |
describe('calling initiate should have resulting queryCacheKey match baseQuery queryCacheKey', () => { |
| 124 |
const baseQuery = vi.fn(() => ({ data: 'success' })) |
| 125 |
function getNewApi() { |
| 126 |
return createApi({ |
| 127 |
baseQuery, |
| 128 |
endpoints: (build) => ({ |
| 129 |
query: build.query<void, { arg1: string; arg2: string }>({ |
| 130 |
query: (args) => `queryUrl/${args.arg1}/${args.arg2}`, |
| 131 |
}), |
| 132 |
mutation: build.mutation<void, { arg1: string; arg2: string }>({ |
| 133 |
query: () => 'mutationUrl', |
| 134 |
}), |
| 135 |
}), |
| 136 |
}) |
| 137 |
} |
| 138 |
let api = getNewApi() |
| 139 |
beforeEach(() => { |
| 140 |
baseQuery.mockClear() |
| 141 |
api = getNewApi() |
| 142 |
}) |
| 143 |
|
| 144 |
test('should be a string and matching on queries', () => { |
| 145 |
const { store: storeApi } = setupApiStore(api, undefined, { |
| 146 |
withoutTestLifecycles: true, |
| 147 |
}) |
| 148 |
const promise = storeApi.dispatch( |
| 149 |
api.endpoints.query.initiate({ arg2: 'secondArg', arg1: 'firstArg' }), |
| 150 |
) |
| 151 |
expect(baseQuery).toHaveBeenCalledWith( |
| 152 |
expect.any(String), |
| 153 |
expect.objectContaining({ |
| 154 |
queryCacheKey: promise.queryCacheKey, |
| 155 |
}), |
| 156 |
undefined, |
| 157 |
) |
| 158 |
}) |
| 159 |
|
| 160 |
test('should be undefined and matching on mutations', () => { |
| 161 |
const { store: storeApi } = setupApiStore(api, undefined, { |
| 162 |
withoutTestLifecycles: true, |
| 163 |
}) |
| 164 |
storeApi.dispatch( |
| 165 |
api.endpoints.mutation.initiate({ arg2: 'secondArg', arg1: 'firstArg' }), |
| 166 |
) |
| 167 |
expect(baseQuery).toHaveBeenCalledWith( |
| 168 |
expect.any(String), |
| 169 |
expect.objectContaining({ |
| 170 |
queryCacheKey: undefined, |
| 171 |
}), |
| 172 |
undefined, |
| 173 |
) |
| 174 |
}) |
| 175 |
}) |