From 309a2f39573c3606815a0e0d46edfd45628e460e Mon Sep 17 00:00:00 2001 From: mrcwbr Date: Wed, 5 Oct 2022 14:09:46 +0200 Subject: [PATCH] added typescript documentation to plugins.md --- docs/guide/extending-vtu/plugins.md | 19 ++++++++++++++++--- src/baseWrapper.ts | 2 +- src/vueWrapper.ts | 10 ++++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/docs/guide/extending-vtu/plugins.md b/docs/guide/extending-vtu/plugins.md index 921ea30dc..8e908f07f 100644 --- a/docs/guide/extending-vtu/plugins.md +++ b/docs/guide/extending-vtu/plugins.md @@ -6,9 +6,9 @@ functionality. Some use cases for plugins: -1. Aliasing existing public methods -1. Attaching matchers to the Wrapper instance -1. Attaching functionality to the Wrapper +1. Aliasing existing public methods +2. Attaching matchers to the Wrapper instance +3. Attaching functionality to the Wrapper ## Wrapper Plugin @@ -168,6 +168,19 @@ const wrapper = mount(Component, { }) ``` +## Using the plugin with TypeScript + +To use your custom wrapper plugin with [TypeScript](https://www.typescriptlang.org/) you have to declare your custom wrapper function. Therefore, add a file named `vue-test-utils.d.ts` with the following content: +```typescript +import { DOMWrapper } from '@vue/test-utils'; + +declare module '@vue/test-utils' { + export class VueWrapper { + findByTestId(testId: string): DOMWrapper[]; + } +} +``` + ## Featuring Your Plugin If you're missing functionality, consider writing a plugin to extend Vue Test diff --git a/src/baseWrapper.ts b/src/baseWrapper.ts index 662941fe2..03150e34e 100644 --- a/src/baseWrapper.ts +++ b/src/baseWrapper.ts @@ -41,7 +41,7 @@ export default abstract class BaseWrapper return this.wrapperElement } - constructor(element: ElementType) { + protected constructor(element: ElementType) { this.wrapperElement = element } diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index a20356e3f..01ce15ef6 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -31,10 +31,12 @@ export class VueWrapper< $emit: (event: any, ...args: any[]) => void } & ComponentCustomProperties = ComponentPublicInstance > extends BaseWrapper { - private componentVM: T - private rootVM: ComponentPublicInstance | undefined | null - private __app: App | null - private __setProps: ((props: Record) => void) | undefined + private readonly componentVM: T + private readonly rootVM: ComponentPublicInstance | undefined | null + private readonly __app: App | null + private readonly __setProps: + | ((props: Record) => void) + | undefined private cleanUpCallbacks: Array<() => void> = [] constructor(