| 1 |
import { noop } from '@internal/listenerMiddleware/utils' |
| 2 |
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query' |
| 3 |
|
| 4 |
const api = createApi({ |
| 5 |
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }), |
| 6 |
endpoints: () => ({}), |
| 7 |
}) |
| 8 |
|
| 9 |
describe('injectEndpoints', () => { |
| 10 |
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop) |
| 11 |
|
| 12 |
afterEach(() => { |
| 13 |
vi.clearAllMocks() |
| 14 |
vi.unstubAllEnvs() |
| 15 |
}) |
| 16 |
|
| 17 |
afterAll(() => { |
| 18 |
vi.restoreAllMocks() |
| 19 |
vi.unstubAllEnvs() |
| 20 |
}) |
| 21 |
|
| 22 |
test("query: overriding with `overrideEndpoints`='throw' throws an error", async () => { |
| 23 |
const extended = api.injectEndpoints({ |
| 24 |
endpoints: (build) => ({ |
| 25 |
injected: build.query<unknown, string>({ |
| 26 |
query: () => '/success', |
| 27 |
}), |
| 28 |
}), |
| 29 |
}) |
| 30 |
|
| 31 |
expect(() => { |
| 32 |
extended.injectEndpoints({ |
| 33 |
overrideExisting: 'throw', |
| 34 |
endpoints: (build) => ({ |
| 35 |
injected: build.query<unknown, string>({ |
| 36 |
query: () => '/success', |
| 37 |
}), |
| 38 |
}), |
| 39 |
}) |
| 40 |
}).toThrowError( |
| 41 |
new Error( |
| 42 |
`called \`injectEndpoints\` to override already-existing endpointName injected without specifying \`overrideExisting: true\``, |
| 43 |
), |
| 44 |
) |
| 45 |
}) |
| 46 |
|
| 47 |
test('query: overriding an endpoint with `overrideEndpoints`=false does nothing in production', async () => { |
| 48 |
vi.stubEnv('NODE_ENV', 'development') |
| 49 |
|
| 50 |
const extended = api.injectEndpoints({ |
| 51 |
endpoints: (build) => ({ |
| 52 |
injected: build.query<unknown, string>({ |
| 53 |
query: () => '/success', |
| 54 |
}), |
| 55 |
}), |
| 56 |
}) |
| 57 |
|
| 58 |
extended.injectEndpoints({ |
| 59 |
overrideExisting: false, |
| 60 |
endpoints: (build) => ({ |
| 61 |
injected: build.query<unknown, string>({ |
| 62 |
query: () => '/success', |
| 63 |
}), |
| 64 |
}), |
| 65 |
}) |
| 66 |
|
| 67 |
expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 68 |
`called \`injectEndpoints\` to override already-existing endpointName injected without specifying \`overrideExisting: true\``, |
| 69 |
) |
| 70 |
}) |
| 71 |
|
| 72 |
test('query: overriding with `overrideEndpoints`=false logs an error in development', async () => { |
| 73 |
vi.stubEnv('NODE_ENV', 'production') |
| 74 |
|
| 75 |
const extended = api.injectEndpoints({ |
| 76 |
endpoints: (build) => ({ |
| 77 |
injected: build.query<unknown, string>({ |
| 78 |
query: () => '/success', |
| 79 |
}), |
| 80 |
}), |
| 81 |
}) |
| 82 |
|
| 83 |
extended.injectEndpoints({ |
| 84 |
overrideExisting: false, |
| 85 |
endpoints: (build) => ({ |
| 86 |
injected: build.query<unknown, string>({ |
| 87 |
query: () => '/success', |
| 88 |
}), |
| 89 |
}), |
| 90 |
}) |
| 91 |
|
| 92 |
expect(consoleErrorSpy).not.toHaveBeenCalled() |
| 93 |
}) |
| 94 |
}) |