UNPKG

vue-router-mock

Version:

Easily test your components by mocking the router

226 lines (224 loc) 8.55 kB
import * as vue_router0 from "vue-router"; import { LocationQueryRaw, RouteLocationRaw, RouteParamsRaw, Router, RouterOptions } from "vue-router"; import { VueWrapper } from "@vue/test-utils"; import * as vue1 from "vue"; import { Ref } from "vue"; import * as _vue_reactivity0 from "@vue/reactivity"; //#region src/autoSpy.d.ts /** * Options passed to the `spy` option of the `createRouterMock` function */ interface RouterMockSpyOptions { /** * Creates a spy (for example, `create: fn => vi.fn(fn)` with vitest) */ create: (...args: any[]) => any; /** * Resets a spy but keeps it active. */ reset: (spy: _InferSpyType) => void; } /** * Define your own Spy to adapt to your testing framework (jest, peeky, sinon, vitest, etc) * @beta: still trying out, could change in the future * * @example * ```ts * import 'vue-router-mock' // Only needed on external d.ts files * * declare module 'vue-router-mock' { * export interface RouterMockSpy<Fn> { * spy: Sinon.Spy<Parameters<Fn>, ReturnType<Fn>> * } * } * ``` */ interface RouterMockSpy<Fn extends (...args: any[]) => any = (...args: any[]) => any> {} /** * @internal */ type _InferSpyType<Fn extends (...args: any[]) => any = (...args: any[]) => any> = keyof RouterMockSpy<Fn> extends 'spy' ? RouterMockSpy<Fn>['spy'] : Fn; //#endregion //#region src/router.d.ts declare const EmptyView: vue1.DefineComponent<{}, {}, {}, {}, {}, vue1.ComponentOptionsMixin, vue1.ComponentOptionsMixin, {}, string, vue1.PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, vue1.ComponentProvideOptions, true, {}, any>; /** * Router Mock instance */ interface RouterMock extends Router { /** * Current depth of the router view. This index is used to find the component * to display in the array `router.currentRoute.value.matched`. */ depth: Ref<number>; /** * Set a value to be returned on a navigation guard for the next navigation. * * @param returnValue - value that will be returned on a simulated navigation * guard */ setNextGuardReturn(returnValue: Error | boolean | RouteLocationRaw | undefined): void; /** * Returns a Promise of the pending navigation. Resolves right away if there * isn't any. */ getPendingNavigation(): ReturnType<Router['push']>; /** * Sets the params of the current route without triggering a navigation. Can * be awaited to wait for Vue to render again. * * @param params - params to set in the current route */ setParams(params: RouteParamsRaw): Promise<void>; /** * Sets the query of the current route without triggering a navigation. Can * be awaited to wait for Vue to render again. * * @param query - query to set in the current route */ setQuery(query: LocationQueryRaw): Promise<void>; /** * Sets the hash of the current route without triggering a navigation. Can * be awaited to wait for Vue to render again. * * @param hash - hash to set in the current route */ setHash(hash: string): Promise<void>; /** * Clear all the mocks and reset the location of the router. This is useful to be called in a `beforeEach()` test hook * to reset the router state before each test. */ reset(): void; push: _InferSpyType<Router['push']>; replace: _InferSpyType<Router['replace']>; } /** * Options passed to `createRouterMock()`. */ interface RouterMockOptions extends Partial<RouterOptions> { /** * Override the starting location before each test. Defaults to * START_LOCATION. */ initialLocation?: RouteLocationRaw; /** * Run in-component guards. Defaults to false. Setting this to `true` will also run global guards as if * `useRealNavigation` was set to `true`. */ runInComponentGuards?: boolean; /** * Runs all navigation through a `push()` or `replace()` to effectively run any global. */ useRealNavigation?: boolean; /** * Run per-route guards. Defaults to false. * @deprecated use `removePerRouteGuards` instead */ runPerRouteGuards?: boolean; /** * Removes `beforeEnter` guards to any route added. Defaults to `true`. */ removePerRouteGuards?: boolean; /** * By default the mock will allow you to push to locations without adding all * the necessary routes so you can still check if `router.push()` was called * in a specific scenario * (https://github.com/posva/vue-router-mock/issues/41). Set this to `true` to * disable that behavior and throw when `router.push()` fails. */ noUndeclaredRoutes?: boolean; /** * By default the mock will use sinon, jest, or vitest support to create and reset spies. This option allows to use * any testing library with its own spies, by providing a method to create spies, and one to reset them them. Check * the `RouterMockSpy` type to add your own type. * * @example * * For example, with vitest with `globals: false`: * * ```ts * const router = createRouterMock({ * spy: { * create: fn => vi.fn(fn), * reset: spy => spy.mockClear() * } * }); * ``` */ spy?: RouterMockSpyOptions; } /** * Creates a router mock instance * * @param options - options to initialize the router */ declare function createRouterMock(options?: RouterMockOptions): RouterMock; //#endregion //#region src/injections.d.ts /** * Inject global variables, overriding any previously inject router mock * * @param router - router mock to inject */ declare function injectRouterMock(router?: RouterMock): { router: RouterMock; route: vue_router0.RouteLocationNormalizedLoadedGeneric; }; /** * Creates an object of properties to be provided at your application level to * mock what is injected by vue-router * * @param router - router mock instance */ declare function createProvide(router: RouterMock): { [x: number]: RouterMock | vue_router0.RouteLocationNormalizedLoadedGeneric | vue1.Ref<vue_router0.RouteLocationNormalizedLoadedGeneric, vue_router0.RouteLocationNormalizedLoadedGeneric> | vue1.ComputedRef<vue_router0.RouteLocationMatched>; }; //#endregion //#region src/plugin.d.ts declare function plugin(wrapper: VueWrapper): VueWrapper<unknown, { $: vue1.ComponentInternalInstance; $data: {}; $props: {}; $attrs: { [x: string]: unknown; }; $refs: { [x: string]: unknown; }; $slots: Readonly<{ [name: string]: vue1.Slot<any> | undefined; }>; $root: vue1.ComponentPublicInstance | null; $parent: vue1.ComponentPublicInstance | null; $host: Element | null; $emit: (event: string, ...args: any[]) => void; $el: any; $options: vue1.ComponentOptionsBase<any, any, any, any, any, any, any, any, any, {}, {}, string, {}, {}, {}, string, vue1.ComponentProvideOptions> & { beforeCreate?: (() => void) | (() => void)[]; created?: (() => void) | (() => void)[]; beforeMount?: (() => void) | (() => void)[]; mounted?: (() => void) | (() => void)[]; beforeUpdate?: (() => void) | (() => void)[]; updated?: (() => void) | (() => void)[]; activated?: (() => void) | (() => void)[]; deactivated?: (() => void) | (() => void)[]; beforeDestroy?: (() => void) | (() => void)[]; beforeUnmount?: (() => void) | (() => void)[]; destroyed?: (() => void) | (() => void)[]; unmounted?: (() => void) | (() => void)[]; renderTracked?: ((e: vue1.DebuggerEvent) => void) | ((e: vue1.DebuggerEvent) => void)[]; renderTriggered?: ((e: vue1.DebuggerEvent) => void) | ((e: vue1.DebuggerEvent) => void)[]; errorCaptured?: ((err: unknown, instance: vue1.ComponentPublicInstance | null, info: string) => boolean | void) | ((err: unknown, instance: vue1.ComponentPublicInstance | null, info: string) => boolean | void)[]; }; $forceUpdate: () => void; $nextTick: typeof vue1.nextTick; $watch<T extends string | ((...args: any) => any)>(source: T, cb: T extends ((...args: any) => infer R) ? (...args: [R, R, _vue_reactivity0.OnCleanup]) => any : (...args: [any, any, _vue_reactivity0.OnCleanup]) => any, options?: vue1.WatchOptions): vue1.WatchStopHandle; } & Readonly<{}> & Omit<{}, never> & vue1.ShallowUnwrapRef<{}> & {} & vue1.ComponentCustomProperties & {}>; declare function getRouter(): RouterMock; declare module '@vue/test-utils' { interface VueWrapper<VM, T> { router: RouterMock; } } //#endregion export { EmptyView, type RouterMock, type RouterMockOptions, type RouterMockSpy, type RouterMockSpyOptions, plugin as VueRouterMock, createProvide, createRouterMock, getRouter, injectRouterMock }; //# sourceMappingURL=index.d.ts.map