mirror of
https://github.com/reduxjs/redux-devtools.git
synced 2024-11-13 05:06:50 +03:00
8682d05b0b
* Update Redux packages * Fix instrument build * Fix some test type errors * Fix redux-devtools build * Fix rtk-query-monitor build * Fix redux-devtools-app build * Fix redux-devtools-extension build * Fix redux-devtools-remote build * Fix extension build * slider-monitor-example * test-tab-demo * inspector-monitor-demo * rtk-query-monitor-demo * counter-example * todomvc-example * Fix lint * Fix instrument test types * Fix core tests * Fix rtk-query-monitor tests * Updates
87 lines
2.2 KiB
TypeScript
87 lines
2.2 KiB
TypeScript
import {
|
|
combineReducers,
|
|
configureStore,
|
|
EnhancedStore,
|
|
Middleware,
|
|
Tuple,
|
|
} from '@reduxjs/toolkit';
|
|
import { createApi } from '@reduxjs/toolkit/query/react';
|
|
import type { BaseQueryFn, FetchArgs } from '@reduxjs/toolkit/query';
|
|
import type { ReduxDevTools } from './devtools.mocks';
|
|
|
|
export type MockBaseQuery<
|
|
Result,
|
|
Args = string | FetchArgs,
|
|
Meta = { status?: number },
|
|
> = BaseQueryFn<Args, Result, unknown, Meta>;
|
|
|
|
export type BaseQueryJestMockFunction<Result> = jest.Mock<
|
|
ReturnType<MockBaseQuery<Result>>,
|
|
Parameters<MockBaseQuery<Result>>
|
|
>;
|
|
|
|
export function createMockBaseQuery<Result>(
|
|
jestMockFn: BaseQueryJestMockFunction<Result>,
|
|
): MockBaseQuery<Result> {
|
|
return async function mockBaseQuery(param, api, extra) {
|
|
try {
|
|
const output = await jestMockFn(param, api, extra);
|
|
|
|
return output;
|
|
} catch (error) {
|
|
return {
|
|
error,
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
export function createPokemonApi(
|
|
jestMockFn: BaseQueryJestMockFunction<Record<string, any>>,
|
|
) {
|
|
return createApi({
|
|
reducerPath: 'pokemonApi',
|
|
keepUnusedDataFor: 9999,
|
|
baseQuery: createMockBaseQuery(jestMockFn),
|
|
tagTypes: ['pokemon'],
|
|
endpoints: (builder) => ({
|
|
getPokemonByName: builder.query<Record<string, any>, string>({
|
|
query: (name: string) => `pokemon/${name}`,
|
|
providesTags: (result, error, name: string) => [
|
|
{ type: 'pokemon' },
|
|
{ type: 'pokemon', id: name },
|
|
],
|
|
}),
|
|
}),
|
|
});
|
|
}
|
|
|
|
export function setupStore(
|
|
jestMockFn: BaseQueryJestMockFunction<Record<string, any>>,
|
|
devTools: typeof ReduxDevTools,
|
|
) {
|
|
const pokemonApi = createPokemonApi(jestMockFn);
|
|
|
|
const reducer = combineReducers({
|
|
[pokemonApi.reducerPath]: pokemonApi.reducer,
|
|
});
|
|
|
|
const store: EnhancedStore<ReturnType<typeof reducer>> = configureStore({
|
|
reducer,
|
|
devTools: false,
|
|
// adding the api middleware enables caching, invalidation, polling and other features of `rtk-query`
|
|
middleware: (getDefaultMiddleware) =>
|
|
getDefaultMiddleware().concat(pokemonApi.middleware),
|
|
enhancers: (getDefaultEnhancers) =>
|
|
getDefaultEnhancers().concat(devTools.instrument()),
|
|
});
|
|
|
|
return {
|
|
jestMockFn,
|
|
devTools,
|
|
store,
|
|
reducer,
|
|
pokemonApi,
|
|
};
|
|
}
|