From f8ce7ac96156eac7c84d2e3085a756b14f654dba Mon Sep 17 00:00:00 2001 From: haoziqaq <357229046@qq.com> Date: Sun, 1 Aug 2021 02:32:26 +0800 Subject: [PATCH] test(ui/uploader): fix mock fileReader to class affects: @varlet/ui --- .../src/uploader/__tests__/index.spec.js | 1 + packages/varlet-ui/src/utils/jest.ts | 21 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/varlet-ui/src/uploader/__tests__/index.spec.js b/packages/varlet-ui/src/uploader/__tests__/index.spec.js index aa0c76d63a0..c2919a2146b 100644 --- a/packages/varlet-ui/src/uploader/__tests__/index.spec.js +++ b/packages/varlet-ui/src/uploader/__tests__/index.spec.js @@ -72,6 +72,7 @@ test('test uploader preview', async () => { }) await wrapper.vm.handleChange(createEvent('cat.jpg')) + await delay(16) await wrapper.find('.var-uploader__file').trigger('click') await delay(16) diff --git a/packages/varlet-ui/src/utils/jest.ts b/packages/varlet-ui/src/utils/jest.ts index fd7475a3371..7d5b37cd0ba 100644 --- a/packages/varlet-ui/src/utils/jest.ts +++ b/packages/varlet-ui/src/utils/jest.ts @@ -180,16 +180,19 @@ interface MockFileReaderCtx { export function mockFileReader(url: string) { const originMethod = window.FileReader - Object.assign(window, { - FileReader(this: MockFileReaderCtx) { - this.onload = function () {} - this.result = '' + class FileReader { + result = '' - this.readAsDataURL = function () { - this.result = url - this.onload() - } - }, + onload() {} + + readAsDataURL() { + this.result = url + this.onload() + } + } + + Object.assign(window, { + FileReader, }) return {