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