mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-05 19:41:42 +08:00
test: update test cases of utils
This commit is contained in:
parent
4ca6dc4dbe
commit
75c296f0a8
10
packages/vant-use/src/test/utils.spec.ts
Normal file
10
packages/vant-use/src/test/utils.spec.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { raf, cancelRaf } from '../utils';
|
||||
|
||||
test('raf', async () => {
|
||||
const spy = jest.fn();
|
||||
raf(spy);
|
||||
|
||||
// await later(50);
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
cancelRaf(1);
|
||||
});
|
@ -2,14 +2,15 @@ import { isPromise, noop } from '.';
|
||||
|
||||
export function callInterceptor(options: {
|
||||
interceptor?: (...args: any[]) => Promise<boolean> | boolean;
|
||||
args: any[];
|
||||
args?: any[];
|
||||
done: () => void;
|
||||
canceled?: () => void;
|
||||
}) {
|
||||
const { interceptor, args, done, canceled } = options;
|
||||
|
||||
if (interceptor) {
|
||||
const returnVal = interceptor(...args);
|
||||
// eslint-disable-next-line prefer-spread
|
||||
const returnVal = interceptor.apply(null, args || []);
|
||||
|
||||
if (isPromise(returnVal)) {
|
||||
returnVal
|
||||
|
@ -1,8 +1,6 @@
|
||||
import { deepClone } from '../deep-clone';
|
||||
import { deepAssign } from '../deep-assign';
|
||||
import { isDef, get, noop } from '..';
|
||||
import { raf, cancelRaf } from '../dom/raf';
|
||||
import { later } from '../../../test';
|
||||
import { isEmail } from '../validate/email';
|
||||
import { isMobile } from '../validate/mobile';
|
||||
import { isNumeric } from '../validate/number';
|
||||
@ -20,7 +18,6 @@ test('deepClone', () => {
|
||||
expect(deepClone(noop)).toEqual(noop);
|
||||
expect(deepClone(arr)).toEqual(arr);
|
||||
expect(deepClone(undefined)).toEqual(undefined);
|
||||
expect(deepClone(1)).toEqual(1);
|
||||
});
|
||||
|
||||
test('deepAssign', () => {
|
||||
@ -66,15 +63,6 @@ test('isAndroid', () => {
|
||||
expect(isAndroid()).toBeFalsy();
|
||||
});
|
||||
|
||||
test('raf', async () => {
|
||||
const spy = jest.fn();
|
||||
raf(spy);
|
||||
|
||||
await later(50);
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
cancelRaf(1);
|
||||
});
|
||||
|
||||
test('isEmail', () => {
|
||||
expect(isEmail('abc@gmail.com')).toBeTruthy();
|
||||
expect(isEmail('abc@@gmail.com')).toBeFalsy();
|
||||
@ -132,9 +120,9 @@ test('addUnit', () => {
|
||||
test('unitToPx', () => {
|
||||
const originGetComputedStyle = window.getComputedStyle;
|
||||
|
||||
window.innerWidth = 100;
|
||||
window.innerHeight = 200;
|
||||
window.getComputedStyle = () => ({ fontSize: '16px' });
|
||||
Object.defineProperty(window, 'innerWidth', { value: 100 });
|
||||
Object.defineProperty(window, 'innerHeight', { value: 200 });
|
||||
window.getComputedStyle = () => ({ fontSize: '16px' } as CSSStyleDeclaration);
|
||||
|
||||
expect(unitToPx(0)).toEqual(0);
|
||||
expect(unitToPx(10)).toEqual(10);
|
@ -1,7 +1,7 @@
|
||||
import { later } from '../../../test';
|
||||
import { callInterceptor } from '../interceptor';
|
||||
|
||||
test('#callInterceptor', async () => {
|
||||
test('callInterceptor', async () => {
|
||||
const done = jest.fn();
|
||||
callInterceptor({ done });
|
||||
expect(done).toHaveBeenCalledTimes(1);
|
||||
@ -42,9 +42,11 @@ test('#callInterceptor', async () => {
|
||||
callInterceptor({
|
||||
interceptor: (...args) => {
|
||||
expect(args).toEqual(['foo']);
|
||||
return false;
|
||||
},
|
||||
args: ['foo'],
|
||||
done,
|
||||
});
|
||||
|
||||
expect(done).toHaveBeenCalledTimes(3);
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user