From 759b4fb60e0ad0d0bc6743a79ae25076313f36fa Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 4 Nov 2024 07:01:26 +1000 Subject: [PATCH] fix: [#1538] Always return Promise from ClipboardItem.getType() (#1539) Co-authored-by: David Ortner --- packages/happy-dom/src/clipboard/ClipboardItem.ts | 7 +++++-- packages/happy-dom/test/clipboard/Clipboard.test.ts | 8 +++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/happy-dom/src/clipboard/ClipboardItem.ts b/packages/happy-dom/src/clipboard/ClipboardItem.ts index d7d54b2b8..5d8a678c7 100644 --- a/packages/happy-dom/src/clipboard/ClipboardItem.ts +++ b/packages/happy-dom/src/clipboard/ClipboardItem.ts @@ -43,12 +43,15 @@ export default class ClipboardItem { * @param type Type. * @returns Data. */ - public async getType(type: string): Promise { + public async getType(type: string): Promise { if (!this.#data[type]) { throw new DOMException( `Failed to execute 'getType' on 'ClipboardItem': The type '${type}' was not found` ); } - return this.#data[type]; + if (this.#data[type] instanceof Blob) { + return this.#data[type]; + } + return new Blob([await this.#data[type]], { type }); } } diff --git a/packages/happy-dom/test/clipboard/Clipboard.test.ts b/packages/happy-dom/test/clipboard/Clipboard.test.ts index c52c1ab9f..c5eb61cd3 100644 --- a/packages/happy-dom/test/clipboard/Clipboard.test.ts +++ b/packages/happy-dom/test/clipboard/Clipboard.test.ts @@ -37,11 +37,9 @@ describe('Clipboard', () => { for (const item of data) { const data = await item.getType(item.types[0]); - if (typeof data === 'string') { - text += data; - } else { - text += await data.text(); - } + expect(data).toBeInstanceOf(Blob); + + text += await data.text(); } expect(text).toBe('test-atest-btest-ctest-dtest-e');