diff --git a/package.json b/package.json index ae25d1dc2..7ddd3cac7 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "@babel/polyfill": "^7.2.5", "@babel/preset-env": "^7.3.4", "@babel/preset-typescript": "^7.3.3", + "@types/jest": "^24.0.11", "@vant/doc": "^1.0.23", "@vant/eslint-config": "^1.1.2", "@vant/markdown-loader": "^1.0.3", diff --git a/test/demo-test.js b/test/demo-test.ts similarity index 92% rename from test/demo-test.js rename to test/demo-test.ts index 348e3bb9f..480049c42 100644 --- a/test/demo-test.js +++ b/test/demo-test.ts @@ -7,10 +7,11 @@ const empty = { template: '
', inheritAttrs: false }; + Vue.component('demo-block', empty); Vue.component('demo-section', empty); -export default function (Demo) { +export default function (Demo: any) { test('renders demo correctly', () => { if (Demo.i18n) { Locale.add(Demo.i18n); diff --git a/test/utils.js b/test/utils.ts similarity index 56% rename from test/utils.js rename to test/utils.ts index e5d0d6850..a779403ab 100644 --- a/test/utils.js +++ b/test/utils.ts @@ -1,14 +1,12 @@ import Vue from 'vue'; -import { mount, TransitionStub } from '@vue/test-utils'; +import { mount, TransitionStub, Wrapper } from '@vue/test-utils'; // prevent vue warning log Vue.config.silent = true; -export { - mount -}; +export { mount }; -function getTouch(el, x, y) { +function getTouch(el: HTMLElement, x: number, y: number) { return { identifier: Date.now(), target: el, @@ -24,8 +22,14 @@ function getTouch(el, x, y) { } // Trigger pointer/touch event -export function trigger(wrapper, eventName, x = 0, y = 0, options = {}) { - const el = wrapper.element ? wrapper.element : wrapper; +export function trigger( + wrapper: Wrapper | HTMLElement, + eventName: string, + x: number = 0, + y: number = 0, + options: any = {} +) { + const el = 'element' in wrapper ? wrapper.element : wrapper; const touchList = options.touchList || [getTouch(el, x, y)]; if (options.x || options.y) { @@ -34,17 +38,20 @@ export function trigger(wrapper, eventName, x = 0, y = 0, options = {}) { const event = document.createEvent('CustomEvent'); event.initCustomEvent(eventName, true, true, {}); - event.touches = touchList; - event.targetTouches = touchList; - event.changedTouches = touchList; - event.clientX = x; - event.clientY = y; + + Object.assign(event, { + clientX: x, + clientY: y, + touches: touchList, + targetTouches: touchList, + changedTouches: touchList + }); el.dispatchEvent(event); } // simulate drag gesture -export function triggerDrag(el, x = 0, y = 0) { +export function triggerDrag(el: Wrapper | HTMLElement, x = 0, y = 0): void { trigger(el, 'touchstart', 0, 0); trigger(el, 'touchmove', x / 4, y / 4); trigger(el, 'touchmove', x / 3, y / 3); @@ -54,12 +61,12 @@ export function triggerDrag(el, x = 0, y = 0) { } // promisify setTimeout -export function later(delay) { +export function later(delay: number = 0): Promise { return new Promise(resolve => { setTimeout(resolve, delay); }); } -export function transitionStub() { - Vue.component('transition', TransitionStub); +export function transitionStub(): void { + Vue.component('transition', TransitionStub as any); } diff --git a/yarn.lock b/yarn.lock index 18bbf77d6..9906b8d79 100644 --- a/yarn.lock +++ b/yarn.lock @@ -910,6 +910,18 @@ resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-1.1.0.tgz#2cc2ca41051498382b43157c8227fea60363f94a" integrity sha512-ohkhb9LehJy+PA40rDtGAji61NCgdtKLAlFoYp4cnuuQEswwdK3vz9SOIkkyc3wrk8dzjphQApNs56yyXLStaQ== +"@types/jest-diff@*": + version "20.0.1" + resolved "https://registry.yarnpkg.com/@types/jest-diff/-/jest-diff-20.0.1.tgz#35cc15b9c4f30a18ef21852e255fdb02f6d59b89" + integrity sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA== + +"@types/jest@^24.0.11": + version "24.0.11" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.11.tgz#1f099bea332c228ea6505a88159bfa86a5858340" + integrity sha512-2kLuPC5FDnWIDvaJBzsGTBQaBbnDweznicvK7UGYzlIJP4RJR2a4A/ByLUXEyEgag6jz8eHdlWExGDtH3EYUXQ== + dependencies: + "@types/jest-diff" "*" + "@types/minimatch@*": version "3.0.3" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"