first commit

This commit is contained in:
编码猿
2024-09-27 00:57:27 +08:00
commit 18df0f5e4b
2548 changed files with 253280 additions and 0 deletions

1
vant/test/.pydio Normal file
View File

@@ -0,0 +1 @@
a5e3e227-6b24-4ba5-8b81-3248f0f234e8

36
vant/test/demo.ts Normal file
View File

@@ -0,0 +1,36 @@
import Vue, { CreateElement } from 'vue';
import '../docs/site/mobile';
import Locale from '../src/locale';
import { mount, later } from '.';
const Empty = {
render(h: CreateElement): ReturnType<CreateElement> {
return h('div', [(this as any).$slots.default]);
},
inheritAttrs: false,
};
Vue.component('demo-block', Empty);
Vue.component('demo-section', Empty);
export function snapshotDemo(Demo: any, option: any = {}) {
test('renders demo correctly', async () => {
if (option.beforeTest) {
option.beforeTest();
}
if (Demo.i18n) {
Locale.add(Demo.i18n);
}
const wrapper = mount(Demo);
await later();
expect(wrapper).toMatchSnapshot();
if (option.afterTest) {
option.afterTest();
}
});
}

55
vant/test/dom.ts Normal file
View File

@@ -0,0 +1,55 @@
import { trigger } from './event';
function mockHTMLElementOffset() {
Object.defineProperties(HTMLElement.prototype, {
offsetParent: {
get() {
return this.parentNode || {};
},
},
offsetLeft: {
get() {
return parseFloat(window.getComputedStyle(this).marginLeft) || 0;
},
},
offsetTop: {
get() {
return parseFloat(window.getComputedStyle(this).marginTop) || 0;
},
},
offsetHeight: {
get() {
return parseFloat(window.getComputedStyle(this).height) || 0;
},
},
offsetWidth: {
get() {
return parseFloat(window.getComputedStyle(this).width) || 0;
},
},
});
}
function mockScrollIntoView() {
Element.prototype.scrollIntoView = function() {};
}
mockScrollIntoView();
mockHTMLElementOffset();
export function mockGetBoundingClientRect(
rect: ClientRect | DOMRect
): Function {
const originMethod = Element.prototype.getBoundingClientRect;
Element.prototype.getBoundingClientRect = <any>jest.fn(() => rect);
return function() {
Element.prototype.getBoundingClientRect = originMethod;
};
}
export function mockScrollTop(value: number) {
Object.defineProperty(window, 'scrollTop', { value, writable: true });
trigger(window, 'scroll');
}

60
vant/test/event.ts Normal file
View File

@@ -0,0 +1,60 @@
import Vue from 'vue';
import { Wrapper } from '@vue/test-utils';
function getTouch(el: HTMLElement | Window, x: number, y: number) {
return {
identifier: Date.now(),
target: el,
pageX: x,
pageY: y,
clientX: x,
clientY: y,
radiusX: 2.5,
radiusY: 2.5,
rotationAngle: 10,
force: 0.5,
};
}
// Trigger pointer/touch event
export function trigger(
wrapper: Wrapper<Vue> | HTMLElement | Window,
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) {
touchList.push(getTouch(el, options.x, options.y));
}
const event = document.createEvent('CustomEvent');
event.initCustomEvent(eventName, true, true, {});
Object.assign(event, {
clientX: x,
clientY: y,
touches: touchList,
targetTouches: touchList,
changedTouches: touchList,
});
el.dispatchEvent(event);
}
// simulate drag gesture
export function triggerDrag(
el: Wrapper<Vue> | 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);
trigger(el, 'touchmove', x / 2, y / 2);
trigger(el, 'touchmove', x, y);
trigger(el, 'touchend', x, y);
}

25
vant/test/index.ts Normal file
View File

@@ -0,0 +1,25 @@
import Vue from 'vue';
import { mount, TransitionStub } from '@vue/test-utils';
import { trigger, triggerDrag } from './event';
import { mockScrollTop, mockGetBoundingClientRect } from './dom';
// prevent vue warning log
Vue.config.silent = true;
// stub transition
Vue.component('transition', TransitionStub as any);
// promisify setTimeout
export function later(delay: number = 0): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
}
export {
mount,
trigger,
triggerDrag,
mockScrollTop,
mockGetBoundingClientRect,
};