[improvement] test utils ts (#3024)

This commit is contained in:
neverland 2019-03-21 11:00:50 +08:00 committed by GitHub
parent f03b200b37
commit 5c0ae2dc06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 17 deletions

View File

@ -75,6 +75,7 @@
"@babel/polyfill": "^7.2.5", "@babel/polyfill": "^7.2.5",
"@babel/preset-env": "^7.3.4", "@babel/preset-env": "^7.3.4",
"@babel/preset-typescript": "^7.3.3", "@babel/preset-typescript": "^7.3.3",
"@types/jest": "^24.0.11",
"@vant/doc": "^1.0.23", "@vant/doc": "^1.0.23",
"@vant/eslint-config": "^1.1.2", "@vant/eslint-config": "^1.1.2",
"@vant/markdown-loader": "^1.0.3", "@vant/markdown-loader": "^1.0.3",

View File

@ -7,10 +7,11 @@ const empty = {
template: '<div><slot></slot></div>', template: '<div><slot></slot></div>',
inheritAttrs: false inheritAttrs: false
}; };
Vue.component('demo-block', empty); Vue.component('demo-block', empty);
Vue.component('demo-section', empty); Vue.component('demo-section', empty);
export default function (Demo) { export default function (Demo: any) {
test('renders demo correctly', () => { test('renders demo correctly', () => {
if (Demo.i18n) { if (Demo.i18n) {
Locale.add(Demo.i18n); Locale.add(Demo.i18n);

View File

@ -1,14 +1,12 @@
import Vue from 'vue'; import Vue from 'vue';
import { mount, TransitionStub } from '@vue/test-utils'; import { mount, TransitionStub, Wrapper } from '@vue/test-utils';
// prevent vue warning log // prevent vue warning log
Vue.config.silent = true; Vue.config.silent = true;
export { export { mount };
mount
};
function getTouch(el, x, y) { function getTouch(el: HTMLElement, x: number, y: number) {
return { return {
identifier: Date.now(), identifier: Date.now(),
target: el, target: el,
@ -24,8 +22,14 @@ function getTouch(el, x, y) {
} }
// Trigger pointer/touch event // Trigger pointer/touch event
export function trigger(wrapper, eventName, x = 0, y = 0, options = {}) { export function trigger(
const el = wrapper.element ? wrapper.element : wrapper; wrapper: Wrapper<Vue> | 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)]; const touchList = options.touchList || [getTouch(el, x, y)];
if (options.x || options.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'); const event = document.createEvent('CustomEvent');
event.initCustomEvent(eventName, true, true, {}); event.initCustomEvent(eventName, true, true, {});
event.touches = touchList;
event.targetTouches = touchList; Object.assign(event, {
event.changedTouches = touchList; clientX: x,
event.clientX = x; clientY: y,
event.clientY = y; touches: touchList,
targetTouches: touchList,
changedTouches: touchList
});
el.dispatchEvent(event); el.dispatchEvent(event);
} }
// simulate drag gesture // simulate drag gesture
export function triggerDrag(el, x = 0, y = 0) { export function triggerDrag(el: Wrapper<Vue> | HTMLElement, x = 0, y = 0): void {
trigger(el, 'touchstart', 0, 0); trigger(el, 'touchstart', 0, 0);
trigger(el, 'touchmove', x / 4, y / 4); trigger(el, 'touchmove', x / 4, y / 4);
trigger(el, 'touchmove', x / 3, y / 3); trigger(el, 'touchmove', x / 3, y / 3);
@ -54,12 +61,12 @@ export function triggerDrag(el, x = 0, y = 0) {
} }
// promisify setTimeout // promisify setTimeout
export function later(delay) { export function later(delay: number = 0): Promise<void> {
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(resolve, delay); setTimeout(resolve, delay);
}); });
} }
export function transitionStub() { export function transitionStub(): void {
Vue.component('transition', TransitionStub); Vue.component('transition', TransitionStub as any);
} }

View File

@ -910,6 +910,18 @@
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-1.1.0.tgz#2cc2ca41051498382b43157c8227fea60363f94a" resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-1.1.0.tgz#2cc2ca41051498382b43157c8227fea60363f94a"
integrity sha512-ohkhb9LehJy+PA40rDtGAji61NCgdtKLAlFoYp4cnuuQEswwdK3vz9SOIkkyc3wrk8dzjphQApNs56yyXLStaQ== 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@*": "@types/minimatch@*":
version "3.0.3" version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"