-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4189 from ffluk3/throw-on-override
Provide 'throw' option for `overrideExisting`
- Loading branch information
Showing
5 changed files
with
111 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query' | ||
import { vi } from 'vitest' | ||
|
||
const api = createApi({ | ||
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }), | ||
endpoints: () => ({}), | ||
}) | ||
|
||
describe('injectEndpoints', () => { | ||
test("query: overridding 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: overridding an endpoint with `overrideEndpoints`=false does nothing in production', async () => { | ||
const consoleMock = vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
|
||
process.env.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(consoleMock).toHaveBeenCalledWith( | ||
`called \`injectEndpoints\` to override already-existing endpointName injected without specifying \`overrideExisting: true\``, | ||
) | ||
}) | ||
|
||
test('query: overridding with `overrideEndpoints`=false logs an error in development', async () => { | ||
const consoleMock = vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
|
||
process.env.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(consoleMock).not.toHaveBeenCalled() | ||
}) | ||
}) |