Skip to content

Move types from DefinitelyTyped to this repository #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
"version": "0.0.0-semantically-released",
"description": "Simple and complete Vue DOM testing utilities that encourage good testing practices.",
"main": "dist/vue-testing-library.js",
"types": "types/index.d.ts",
"scripts": {
"format": "kcd-scripts format",
"build": "kcd-scripts build",
"lint": "kcd-scripts lint",
"test": "kcd-scripts test",
"test:update": "npm test -- --updateSnapshot --coverage",
"validate": "kcd-scripts validate",
"typecheck": "dtslint ./types/",
"setup": "npm install && npm run validate -s"
},
"engines": {
"node": ">10.18"
},
"files": [
"types",
"dist",
"cleanup-after-each.js"
],
Expand All @@ -42,7 +45,6 @@
"dependencies": {
"@babel/runtime": "^7.11.2",
"@testing-library/dom": "^7.24.3",
"@types/testing-library__vue": "^5.0.0",
"@vue/test-utils": "^1.1.0"
},
"devDependencies": {
Expand All @@ -51,6 +53,7 @@
"apollo-boost": "^0.4.9",
"apollo-cache-inmemory": "^1.6.6",
"axios": "^0.20.0",
"dtslint": "^4.0.4",
"eslint-plugin-vue": "^6.2.2",
"graphql": "^15.3.0",
"graphql-tag": "^2.11.0",
Expand All @@ -60,6 +63,7 @@
"lodash.merge": "^4.6.2",
"msw": "^0.21.2",
"portal-vue": "^2.1.7",
"typescript": "^4.0.5",
"vee-validate": "^2.2.15",
"vue": "^2.6.12",
"vue-apollo": "^3.0.4",
Expand Down
71 changes: 71 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// TypeScript Version: 3.8

import Vue, {ComponentOptions} from 'vue'
import {ThisTypedMountOptions, VueClass} from '@vue/test-utils'
import {Store, StoreOptions} from 'vuex'
import Router, {RouteConfig} from 'vue-router'
import {OptionsReceived as PrettyFormatOptions} from 'pretty-format'
import {queries, EventType, BoundFunctions} from '@testing-library/dom'

// NOTE: fireEvent is overridden below
export * from '@testing-library/dom'

export interface RenderResult extends BoundFunctions<typeof queries> {
container: HTMLElement
baseElement: HTMLElement
debug: (
baseElement?:
| HTMLElement
| DocumentFragment
| Array<HTMLElement | DocumentFragment>,
maxLength?: number,
options?: PrettyFormatOptions,
) => void
unmount(): void
isUnmounted(): boolean
html(): string
emitted(): {[name: string]: any[][]}
updateProps(props: object): Promise<void>
}

export interface RenderOptions<V extends Vue, S = {}>
// The props and store options special-cased by Vue Testing Library and NOT passed to mount().
extends Omit<ThisTypedMountOptions<V>, 'store' | 'props'> {
props?: object
store?: StoreOptions<S>
routes?: RouteConfig[]
container?: HTMLElement
baseElement?: HTMLElement
}

export type ConfigurationCallback<V extends Vue> = (
localVue: typeof Vue,
store: Store<any>,
router: Router,
) => Partial<ThisTypedMountOptions<V>> | void
Copy link
Member Author

@afontcu afontcu Nov 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line productes the following warning with latest config:

void is only valid as a return type or generic type variable
@typescript-eslint/no-invalid-void-type

not entirely sure I should be worried about this?


export function render<V extends Vue>(
TestComponent: VueClass<V> | ComponentOptions<V>,
options?: RenderOptions<V>,
configure?: ConfigurationCallback<V>,
): RenderResult

export type AsyncFireObject = {
[K in EventType]: (
element: Document | Element | Window,
options?: {},
) => Promise<void>
}

export interface VueFireEventObject extends AsyncFireObject {
(element: Document | Element | Window, event: Event): Promise<void>
touch(element: Document | Element | Window): Promise<void>
update(element: HTMLOptionElement): Promise<void>
update(
element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement,
value: string,
): Promise<void>
update(element: HTMLElement, value?: string): Promise<void>
}

export const fireEvent: VueFireEventObject
124 changes: 124 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import Vue from 'vue'
import {render, fireEvent, screen, waitFor} from '@testing-library/vue'

declare const elem: HTMLElement

const SomeComponent = Vue.extend({
name: 'SomeComponent',
props: {
foo: Number,
bar: String,
},
})

async function testRender() {
const page = render({template: '<div />'})

// single queries
page.getByText('foo')
page.queryByText('foo')
await page.findByText('foo')

// multiple queries
page.getAllByText('bar')
page.queryAllByText('bar')
await page.findAllByText('bar')

// helpers
const {container, unmount, debug} = page

debug(elem) // $ExpectType void
debug([elem, elem], 100, {highlight: false}) // $ExpectType void
}

async function testRenderOptions() {
const container = document.createElement('div')
const options = {container}
render({template: 'div'}, options)
}

async function testFireEvent() {
const {container} = render({template: 'button'})
await fireEvent.click(container)
}

async function testDebug() {
const {debug, getAllByTestId} = render({
render(h) {
return h('div', [
h('h1', {attrs: {'data-testId': 'testid'}}, 'hello world'),
h('h2', {attrs: {'data-testId': 'testid'}}, 'hello world'),
])
},
})

debug(getAllByTestId('testid'))
}

async function testScreen() {
render({template: 'button'})

await screen.findByRole('button')
}

async function testWaitFor() {
const {container} = render({template: 'button'})
fireEvent.click(container)
await waitFor(() => {})
}

async function testOptions() {
render(SomeComponent, {
// options for new Vue()
name: 'SomeComponent',
methods: {
glorb() {
return 42
},
},
// options for vue-test-utils mount()
slots: {
quux: '<p>Baz</p>',
},
mocks: {
isThisFake() {
return true
},
},
// options for Vue Testing Library render()
container: elem,
baseElement: elem,
props: {
foo: 9,
bar: 'x',
},
store: {
state: {
foos: [4, 5],
bars: ['a', 'b'],
},
getters: {
fooCount() {
return this.foos.length
},
},
},
routes: [
{path: '/', name: 'home', component: SomeComponent},
{
path: '/about',
name: 'about',
component: () => Promise.resolve(SomeComponent),
},
],
})
}

function testConfigCallback() {
const ExamplePlugin: Vue.PluginFunction<never> = () => {}
render(SomeComponent, {}, (localVue, store, router) => {
localVue.use(ExamplePlugin)
store.replaceState({foo: 'bar'})
router.onError(error => console.log(error.message))
})
}
17 changes: 17 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// this additional tsconfig is required by dtslint
// see: https://github.com/Microsoft/dtslint#typestsconfigjson
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "dom"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noEmit": true,
"baseUrl": ".",
"paths": {
"@testing-library/vue": ["."]
}
}
}
7 changes: 7 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["dtslint/dtslint.json"],
"rules": {
"semicolon": false,
"whitespace": false
}
}