diff --git a/packages/happy-dom/src/file/FileReader.ts b/packages/happy-dom/src/file/FileReader.ts index 521257ae0..1c9de3c72 100644 --- a/packages/happy-dom/src/file/FileReader.ts +++ b/packages/happy-dom/src/file/FileReader.ts @@ -49,6 +49,11 @@ export default class FileReader extends EventTarget { * @param blob Blob. */ public readAsArrayBuffer(blob: Blob): void { + if (!(blob instanceof Blob)) { + throw new this[PropertySymbol.window].TypeError( + `Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'.` + ); + } this.#readFile(blob, FileReaderFormatEnum.buffer); } @@ -58,6 +63,11 @@ export default class FileReader extends EventTarget { * @param blob Blob. */ public readAsBinaryString(blob: Blob): void { + if (!(blob instanceof Blob)) { + throw new this[PropertySymbol.window].TypeError( + `Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'.` + ); + } this.#readFile(blob, FileReaderFormatEnum.binaryString); } @@ -67,6 +77,11 @@ export default class FileReader extends EventTarget { * @param blob Blob. */ public readAsDataURL(blob: Blob): void { + if (!(blob instanceof Blob)) { + throw new this[PropertySymbol.window].TypeError( + `Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.` + ); + } this.#readFile(blob, FileReaderFormatEnum.dataURL); } @@ -77,6 +92,11 @@ export default class FileReader extends EventTarget { * @param [encoding] Encoding. */ public readAsText(blob: Blob, encoding: string | null = null): void { + if (!(blob instanceof Blob)) { + throw new this[PropertySymbol.window].TypeError( + `Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.` + ); + } this.#readFile(blob, FileReaderFormatEnum.text, encoding || 'UTF-8'); } diff --git a/packages/happy-dom/test/file/FileReader.test.ts b/packages/happy-dom/test/file/FileReader.test.ts index dbb8455e4..edcce55a0 100644 --- a/packages/happy-dom/test/file/FileReader.test.ts +++ b/packages/happy-dom/test/file/FileReader.test.ts @@ -25,5 +25,82 @@ describe('FileReader', () => { await window.happyDOM?.waitUntilComplete(); expect(result).toBe('data:text/plain;charset=utf-8;base64,VEVTVA=='); }); + + it('Reads Blob as data URL passing invalid parameter.', () => { + expect(() => { + fileReader.readAsDataURL('invalid'); + }).toThrow( + `Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.` + ); + }); + }); + + describe('readAsText()', () => { + it('Reads Blob as text.', async () => { + const blob = new Blob(['TEST'], { + type: 'text/plain;charset=utf-8' + }); + let result: string | null = null; + fileReader.addEventListener('load', () => { + result = fileReader.result; + }); + fileReader.readAsText(blob); + await window.happyDOM?.waitUntilComplete(); + expect(result).toBe('TEST'); + }); + + it('Reads Blob as text passing invalid parameter.', () => { + expect(() => { + fileReader.readAsText('invalid'); + }).toThrow( + `Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.` + ); + }); + }); + + describe('readAsArrayBuffer()', () => { + it('Reads Blob as array buffer.', async () => { + const blob = new Blob(['TEST'], { + type: 'text/plain;charset=utf-8' + }); + let result: ArrayBuffer | null = null; + fileReader.addEventListener('load', () => { + result = fileReader.result; + }); + fileReader.readAsArrayBuffer(blob); + await window.happyDOM?.waitUntilComplete(); + expect(result).toBeInstanceOf(ArrayBuffer); + }); + + it('Reads Blob as array buffer passing invalid parameter.', () => { + expect(() => { + fileReader.readAsArrayBuffer('invalid'); + }).toThrow( + `Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'.` + ); + }); + }); + + describe('readAsBinaryString()', () => { + it('Reads Blob as binary string.', async () => { + const blob = new Blob(['TEST'], { + type: 'text/plain;charset=utf-8' + }); + let result: string | null = null; + fileReader.addEventListener('load', () => { + result = fileReader.result; + }); + fileReader.readAsBinaryString(blob); + await window.happyDOM?.waitUntilComplete(); + expect(result).toBe('TEST'); + }); + + it('Reads Blob as binary string passing invalid parameter.', () => { + expect(() => { + fileReader.readAsBinaryString('invalid'); + }).toThrow( + `Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'.` + ); + }); }); });