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