From 27bb1cdc08d1b301ffd30bc68ac46d53f87d6d33 Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Mon, 16 Sep 2024 18:25:20 +1000 Subject: [PATCH] fix: [#1538] Always return Promise from ClipboardItem.getType() method --- 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');