test: improve mock spy (#8483)

This commit is contained in:
neverland 2021-04-09 09:53:25 +08:00 committed by GitHub
parent 63ace39d2a
commit 1217088d95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 29 deletions

View File

@ -7,7 +7,7 @@ exports[`delete preview image 1`] = `
<div class="van-image van-uploader__preview-image"
style="width: 30px; height: 30px;"
>
<img src="https://img01.yzcdn.cn/vant/cat.jpeg"
<img src="https://img.yzcdn.cn/vant/cat.jpeg"
class="van-image__img"
style="object-fit: cover;"
>
@ -98,7 +98,7 @@ exports[`image-fit prop 1`] = `
<div class="van-uploader__wrapper">
<div class="van-uploader__preview">
<div class="van-image van-uploader__preview-image">
<img src="https://img01.yzcdn.cn/vant/cat.jpeg"
<img src="https://img.yzcdn.cn/vant/cat.jpeg"
class="van-image__img"
style="object-fit: contain;"
>
@ -129,7 +129,7 @@ exports[`preview-cover slot 1`] = `
<div class="van-uploader__wrapper">
<div class="van-uploader__preview">
<div class="van-image van-uploader__preview-image">
<img src="https://img01.yzcdn.cn/vant/cat.jpeg"
<img src="https://img.yzcdn.cn/vant/cat.jpeg"
class="van-image__img"
style="object-fit: cover;"
>
@ -148,7 +148,7 @@ exports[`preview-cover slot 1`] = `
</div>
<div class="van-uploader__preview">
<div class="van-image van-uploader__preview-image">
<img src="https://img01.yzcdn.cn/vant/cat.jpeg"
<img src="https://img.yzcdn.cn/vant/cat.jpeg"
class="van-image__img"
style="object-fit: cover;"
>
@ -214,7 +214,7 @@ exports[`render preview image 1`] = `
<div class="van-uploader__wrapper">
<div class="van-uploader__preview">
<div class="van-image van-uploader__preview-image">
<img src="https://img01.yzcdn.cn/vant/cat.jpeg"
<img src="https://img.yzcdn.cn/vant/cat.jpeg"
class="van-image__img"
style="object-fit: cover;"
>
@ -233,7 +233,7 @@ exports[`render preview image 1`] = `
<i class="van-badge__wrapper van-icon van-icon-description van-uploader__file-icon">
</i>
<div class="van-uploader__file-name van-ellipsis">
https://img01.yzcdn.cn/vant/test.pdf
https://img.yzcdn.cn/vant/test.pdf
</div>
</div>
<div class="van-uploader__preview-delete">

View File

@ -1,23 +1,33 @@
import { nextTick } from 'vue';
import Uploader, { UploaderFileListItem } from '..';
import { mount, later, triggerDrag } from '../../../test';
import { nextTick } from 'vue';
const mockFileDataUrl = 'data:image/test';
const mockFile = new File([new ArrayBuffer(10000)], 'test.jpg');
const IMAGE = 'https://img01.yzcdn.cn/vant/cat.jpeg';
const PDF = 'https://img01.yzcdn.cn/vant/test.pdf';
const IMAGE = 'https://img.yzcdn.cn/vant/cat.jpeg';
const PDF = 'https://img.yzcdn.cn/vant/test.pdf';
(window.FileReader as any) = function (this: any) {
(this as any).readAsText = function () {
this.onload &&
function mockFileReader() {
function mockReadAsText(this: FileReader) {
if (this.onload) {
this.onload({
target: {
result: mockFileDataUrl,
},
} as ProgressEvent<FileReader>);
}
}
Object.defineProperty(window.FileReader.prototype, 'readAsText', {
value: mockReadAsText,
});
};
this.readAsDataURL = this.readAsText;
};
Object.defineProperty(window.FileReader.prototype, 'readAsDataURL', {
value: mockReadAsText,
});
}
mockFileReader();
test('disabled', async () => {
const afterRead = jest.fn();
@ -256,8 +266,8 @@ test('render preview image', async () => {
const wrapper = mount(Uploader, {
props: {
modelValue: [
{ url: 'https://img01.yzcdn.cn/vant/cat.jpeg' },
{ url: 'https://img01.yzcdn.cn/vant/test.pdf' },
{ url: 'https://img.yzcdn.cn/vant/cat.jpeg' },
{ url: 'https://img.yzcdn.cn/vant/test.pdf' },
{ file: mockFile },
],
},
@ -270,7 +280,7 @@ test('image-fit prop', () => {
const wrapper = mount(Uploader, {
props: {
imageFit: 'contain',
modelValue: [{ url: 'https://img01.yzcdn.cn/vant/cat.jpeg' }],
modelValue: [{ url: 'https://img.yzcdn.cn/vant/cat.jpeg' }],
},
});

View File

@ -109,11 +109,13 @@ test('addUnit', () => {
});
test('unitToPx', () => {
const originGetComputedStyle = window.getComputedStyle;
const mockedStyle = { fontSize: '16px' } as CSSStyleDeclaration;
const spy = jest
.spyOn(window, 'getComputedStyle')
.mockReturnValue(mockedStyle);
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);
@ -123,5 +125,5 @@ test('unitToPx', () => {
expect(unitToPx('10vw')).toEqual(10);
expect(unitToPx('10vh')).toEqual(20);
window.getComputedStyle = originGetComputedStyle;
spy.mockRestore();
});

View File

@ -38,13 +38,9 @@ export function mockScrollIntoView() {
}
export function mockGetBoundingClientRect(rect: Partial<DOMRect>): () => void {
const originMethod = Element.prototype.getBoundingClientRect;
Element.prototype.getBoundingClientRect = jest.fn(() => rect as DOMRect);
return function () {
Element.prototype.getBoundingClientRect = originMethod;
};
const spy = jest.spyOn(Element.prototype, 'getBoundingClientRect');
spy.mockReturnValue(rect as DOMRect);
return () => spy.mockRestore();
}
export async function mockScrollTop(value: number) {