Last active
April 9, 2022 14:36
-
-
Save FaberVitale/9dc0bcf2abca98fdec7a374f50ec4d3f to your computer and use it in GitHub Desktop.
Simple rtk-query `fetchBaseQuery` mock using jest
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
import { | |
combineReducers, | |
configureStore, | |
EnhancedStore, | |
Middleware, | |
} from '@reduxjs/toolkit'; | |
import type { BaseQueryFn, FetchArgs } from '@reduxjs/toolkit/query'; | |
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, | |
}; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment