UNPKG

axios-hooks-mock

Version:

A library that simplifies mocking for axios-hooks, especially when multiple hooks are used together.

78 lines (77 loc) 3.69 kB
import { ResponseValues, RefetchOptions } from 'axios-hooks'; import { AxiosRequestConfig, AxiosPromise } from 'axios'; import AxiosHooksTuple from './AxiosHooksTuple'; import AxiosHooksArray from './AxiosHooksArray'; /** * The main class for the library. * 1. Instantiate: new AxiosHooksMock(implementations) * 2. Add more implementations (if needed): .get('url', implementation) * 3. Implement (completes the chain, creates mock implementation): .implement(); * * This class is chainable, and aside from implement(), always returns itself. */ export default class AxiosHooksMock { private implementations; private defaultImplementation; /** * Standard constructor for the class * @param mockItems Optional, @see AxiosHooksMockItem */ constructor(mockItems?: { config: AxiosRequestConfig | string; implementation: AxiosHooksTuple | AxiosHooksArray; }[], defaultImplementation?: AxiosHooksTuple); /** Alternate constructor for those who hate the 'new' keyword. */ static construct(mockItems?: { config: AxiosRequestConfig | string; implementation: AxiosHooksTuple | AxiosHooksArray; }[], defaultImplementation?: AxiosHooksTuple): AxiosHooksMock; private detectUrlAndMethod; private buildCompositeKey; /** * The only method of adding mock implementations to the greater mock class. * All constructors and convenience methods call this method, but this can also be called externally. * @param config @see AxiosRequestConfig or string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ addImplementation(config: AxiosRequestConfig | string, implementation: AxiosHooksTuple): AxiosHooksMock; /** * Convenience method to add a get implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ get(url: string, implementation: AxiosHooksTuple): AxiosHooksMock; /** * Convenience method to add a post implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ post(url: string, implementation: AxiosHooksTuple): AxiosHooksMock; /** * Convenience method to add a patch implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ put(url: string, implementation: AxiosHooksTuple): AxiosHooksMock; /** * Convenience method to add a patch implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ patch(url: string, implementation: AxiosHooksTuple): AxiosHooksMock; /** * Convenience method to add a delete implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ delete(url: string, implementation: AxiosHooksTuple): AxiosHooksMock; /** * If no other implementation matches, this implementation is returned as a backup. * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ default(implementation: AxiosHooksTuple): AxiosHooksMock; /** * Calling this completes the chain, and returns a function that can be used as a mock implementation in jest (or elsewhere). */ implement(): (config: AxiosRequestConfig | string) => [ResponseValues<unknown>, (config?: string | AxiosRequestConfig, options?: RefetchOptions) => AxiosPromise<unknown>]; }