Spaces:
Running
Running
File size: 3,497 Bytes
c2b7eb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import { noop } from '@internal/listenerMiddleware/utils'
import { configureStore } from '@internal/configureStore'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
endpoints: () => ({}),
})
describe('injectEndpoints', () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
afterEach(() => {
vi.clearAllMocks()
vi.unstubAllEnvs()
})
afterAll(() => {
vi.restoreAllMocks()
vi.unstubAllEnvs()
})
test("query: overriding with `overrideEndpoints`='throw' throws an error", async () => {
const extended = api.injectEndpoints({
endpoints: (build) => ({
injected: build.query<unknown, string>({
query: () => '/success',
}),
}),
})
expect(() => {
extended.injectEndpoints({
overrideExisting: 'throw',
endpoints: (build) => ({
injected: build.query<unknown, string>({
query: () => '/success',
}),
}),
})
}).toThrowError(
new Error(
`called \`injectEndpoints\` to override already-existing endpointName injected without specifying \`overrideExisting: true\``,
),
)
})
test('query: overriding an endpoint with `overrideEndpoints`=false does nothing in production', async () => {
vi.stubEnv('NODE_ENV', 'development')
const extended = api.injectEndpoints({
endpoints: (build) => ({
injected: build.query<unknown, string>({
query: () => '/success',
}),
}),
})
extended.injectEndpoints({
overrideExisting: false,
endpoints: (build) => ({
injected: build.query<unknown, string>({
query: () => '/success',
}),
}),
})
expect(consoleErrorSpy).toHaveBeenCalledWith(
`called \`injectEndpoints\` to override already-existing endpointName injected without specifying \`overrideExisting: true\``,
)
})
test('query: overriding with `overrideEndpoints`=false logs an error in development', async () => {
vi.stubEnv('NODE_ENV', 'production')
const extended = api.injectEndpoints({
endpoints: (build) => ({
injected: build.query<unknown, string>({
query: () => '/success',
}),
}),
})
extended.injectEndpoints({
overrideExisting: false,
endpoints: (build) => ({
injected: build.query<unknown, string>({
query: () => '/success',
}),
}),
})
expect(consoleErrorSpy).not.toHaveBeenCalled()
})
test('adding the same middleware to the store twice throws an error', () => {
// Strictly speaking this is a duplicate of the tests in configureStore.test.ts,
// but this helps confirm that we throw the error for adding
// the same API middleware twice.
const extendedApi = api.injectEndpoints({
endpoints: (build) => ({
injected: build.query<unknown, string>({
query: () => '/success',
}),
}),
})
const makeStore = () =>
configureStore({
reducer: {
api: api.reducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(api.middleware, extendedApi.middleware),
})
expect(makeStore).toThrowError(
'Duplicate middleware references found when creating the store. Ensure that each middleware is only included once.',
)
})
})
|