Skip to content

Commit 6e2ce45

Browse files
committed
Add types
1 parent d1ab9ff commit 6e2ce45

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

types/index.d.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// TypeScript Version: 3.8
2+
3+
import Vue, {ComponentOptions} from 'vue'
4+
import {ThisTypedMountOptions, VueClass} from '@vue/test-utils'
5+
import {Store, StoreOptions} from 'vuex'
6+
import Router, {RouteConfig} from 'vue-router'
7+
import {OptionsReceived as PrettyFormatOptions} from 'pretty-format'
8+
import {queries, EventType, BoundFunctions} from '@testing-library/dom'
9+
10+
// NOTE: fireEvent is overridden below
11+
export * from '@testing-library/dom'
12+
13+
export interface RenderResult extends BoundFunctions<typeof queries> {
14+
container: HTMLElement
15+
baseElement: HTMLElement
16+
debug: (
17+
baseElement?:
18+
| HTMLElement
19+
| DocumentFragment
20+
| Array<HTMLElement | DocumentFragment>,
21+
maxLength?: number,
22+
options?: PrettyFormatOptions,
23+
) => void
24+
unmount(): void
25+
isUnmounted(): boolean
26+
html(): string
27+
emitted(): {[name: string]: any[][]}
28+
updateProps(props: object): Promise<void>
29+
}
30+
31+
export interface RenderOptions<V extends Vue, S = {}>
32+
// The props and store options special-cased by Vue Testing Library and NOT passed to mount().
33+
extends Omit<ThisTypedMountOptions<V>, 'store' | 'props'> {
34+
props?: object
35+
store?: StoreOptions<S>
36+
routes?: RouteConfig[]
37+
container?: HTMLElement
38+
baseElement?: HTMLElement
39+
}
40+
41+
export type ConfigurationCallback<V extends Vue> = (
42+
localVue: typeof Vue,
43+
store: Store<any>,
44+
router: Router,
45+
) => Partial<ThisTypedMountOptions<V>> | void
46+
47+
export function render<V extends Vue>(
48+
TestComponent: VueClass<V> | ComponentOptions<V>,
49+
options?: RenderOptions<V>,
50+
configure?: ConfigurationCallback<V>,
51+
): RenderResult
52+
53+
export type AsyncFireObject = {
54+
[K in EventType]: (
55+
element: Document | Element | Window,
56+
options?: {},
57+
) => Promise<void>
58+
}
59+
60+
export interface VueFireEventObject extends AsyncFireObject {
61+
(element: Document | Element | Window, event: Event): Promise<void>
62+
touch(element: Document | Element | Window): Promise<void>
63+
update(element: HTMLOptionElement): Promise<void>
64+
update(
65+
element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement,
66+
value: string,
67+
): Promise<void>
68+
update(element: HTMLElement, value?: string): Promise<void>
69+
}
70+
71+
export const fireEvent: VueFireEventObject

types/test.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import Vue from 'vue'
2+
import {render, fireEvent, screen, waitFor} from '@testing-library/vue'
3+
4+
declare const elem: HTMLElement
5+
6+
const SomeComponent = Vue.extend({
7+
name: 'SomeComponent',
8+
props: {
9+
foo: Number,
10+
bar: String,
11+
},
12+
})
13+
14+
async function testRender() {
15+
const page = render({template: '<div />'})
16+
17+
// single queries
18+
page.getByText('foo')
19+
page.queryByText('foo')
20+
await page.findByText('foo')
21+
22+
// multiple queries
23+
page.getAllByText('bar')
24+
page.queryAllByText('bar')
25+
await page.findAllByText('bar')
26+
27+
// helpers
28+
const {container, unmount, debug} = page
29+
30+
debug(elem) // $ExpectType void
31+
debug([elem, elem], 100, {highlight: false}) // $ExpectType void
32+
}
33+
34+
async function testRenderOptions() {
35+
const container = document.createElement('div')
36+
const options = {container}
37+
render({template: 'div'}, options)
38+
}
39+
40+
async function testFireEvent() {
41+
const {container} = render({template: 'button'})
42+
await fireEvent.click(container)
43+
}
44+
45+
async function testDebug() {
46+
const {debug, getAllByTestId} = render({
47+
render(h) {
48+
return h('div', [
49+
h('h1', {attrs: {'data-testId': 'testid'}}, 'hello world'),
50+
h('h2', {attrs: {'data-testId': 'testid'}}, 'hello world'),
51+
])
52+
},
53+
})
54+
55+
debug(getAllByTestId('testid'))
56+
}
57+
58+
async function testScreen() {
59+
render({template: 'button'})
60+
61+
await screen.findByRole('button')
62+
}
63+
64+
async function testWaitFor() {
65+
const {container} = render({template: 'button'})
66+
fireEvent.click(container)
67+
await waitFor(() => {})
68+
}
69+
70+
async function testOptions() {
71+
render(SomeComponent, {
72+
// options for new Vue()
73+
name: 'SomeComponent',
74+
methods: {
75+
glorb() {
76+
return 42
77+
},
78+
},
79+
// options for vue-test-utils mount()
80+
slots: {
81+
quux: '<p>Baz</p>',
82+
},
83+
mocks: {
84+
isThisFake() {
85+
return true
86+
},
87+
},
88+
// options for Vue Testing Library render()
89+
container: elem,
90+
baseElement: elem,
91+
props: {
92+
foo: 9,
93+
bar: 'x',
94+
},
95+
store: {
96+
state: {
97+
foos: [4, 5],
98+
bars: ['a', 'b'],
99+
},
100+
getters: {
101+
fooCount() {
102+
return this.foos.length
103+
},
104+
},
105+
},
106+
routes: [
107+
{path: '/', name: 'home', component: SomeComponent},
108+
{
109+
path: '/about',
110+
name: 'about',
111+
component: () => Promise.resolve(SomeComponent),
112+
},
113+
],
114+
})
115+
}
116+
117+
function testConfigCallback() {
118+
const ExamplePlugin: Vue.PluginFunction<never> = () => {}
119+
render(SomeComponent, {}, (localVue, store, router) => {
120+
localVue.use(ExamplePlugin)
121+
store.replaceState({foo: 'bar'})
122+
router.onError(error => console.log(error.message))
123+
})
124+
}

types/tsconfig.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// this additional tsconfig is required by dtslint
2+
// see: https://github.com/Microsoft/dtslint#typestsconfigjson
3+
{
4+
"compilerOptions": {
5+
"module": "commonjs",
6+
"lib": ["es6", "dom"],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictNullChecks": true,
10+
"strictFunctionTypes": true,
11+
"noEmit": true,
12+
"baseUrl": ".",
13+
"paths": {
14+
"@testing-library/vue": ["."]
15+
}
16+
}
17+
}

types/tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": ["dtslint/dtslint.json"],
3+
"rules": {
4+
"semicolon": false,
5+
"whitespace": false
6+
}
7+
}

0 commit comments

Comments
 (0)