|
| 1 | +import './setup.ts' |
| 2 | +import { assertEquals, assertInstanceOf } from 'std/assert/mod.ts' |
| 3 | +import { assertSpyCall, spy } from 'std/testing/mock.ts' |
| 4 | +import { readAs } from '../src/readAs.ts' |
| 5 | + |
| 6 | +Deno.test('read blob', async (t) => { |
| 7 | + const blob = new Blob(['foo'], { type: 'text/plain' }) |
| 8 | + |
| 9 | + await t.step('as `ArrayBuffer`', async () => { |
| 10 | + const reader = new FileReader() |
| 11 | + const readAsArrayBufferSpy = spy(reader, 'readAsArrayBuffer') |
| 12 | + const result = await readAs(reader, blob, 'ArrayBuffer') |
| 13 | + |
| 14 | + assertEquals(result, reader.result) |
| 15 | + assertInstanceOf(result, ArrayBuffer) |
| 16 | + assertSpyCall(readAsArrayBufferSpy, 0, { |
| 17 | + self: reader, |
| 18 | + args: [blob, undefined], |
| 19 | + }) |
| 20 | + }) |
| 21 | + |
| 22 | + await t.step('as `BinaryString`', async () => { |
| 23 | + const reader = new FileReader() |
| 24 | + const readAsBinaryStringSpy = spy(reader, 'readAsBinaryString') |
| 25 | + const result = await readAs(reader, blob, 'BinaryString') |
| 26 | + |
| 27 | + assertEquals(result, reader.result) |
| 28 | + assertEquals(result, 'foo') |
| 29 | + assertSpyCall(readAsBinaryStringSpy, 0, { |
| 30 | + self: reader, |
| 31 | + args: [blob, undefined], |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + await t.step('as `DataURL`', async () => { |
| 36 | + const reader = new FileReader() |
| 37 | + const readAsDataURLSpy = spy(reader, 'readAsDataURL') |
| 38 | + const result = await readAs(reader, blob, 'DataURL') |
| 39 | + |
| 40 | + assertEquals(result, reader.result) |
| 41 | + assertEquals(result, 'data:text/plain;base64,Zm9v') |
| 42 | + assertSpyCall(readAsDataURLSpy, 0, { |
| 43 | + self: reader, |
| 44 | + args: [blob, undefined], |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + await t.step('as `Text` with encoding utf-8', async () => { |
| 49 | + const reader = new FileReader() |
| 50 | + const readAsTextSpy = spy(reader, 'readAsText') |
| 51 | + const result = await readAs(reader, blob, 'Text', 'utf-8') |
| 52 | + |
| 53 | + assertEquals(result, reader.result) |
| 54 | + assertEquals(typeof result, 'string') |
| 55 | + assertSpyCall(readAsTextSpy, 0, { |
| 56 | + self: reader, |
| 57 | + args: [blob, 'utf-8'], |
| 58 | + }) |
| 59 | + }) |
| 60 | +}) |
0 commit comments