|
| 1 | +import { of } from '../../../thingies'; |
| 2 | +import { memfs } from '../../../'; |
| 3 | + |
| 4 | +describe('.openAsBlob()', () => { |
| 5 | + it('can read a text file as blob', async () => { |
| 6 | + const { fs } = memfs({ '/dir/test.txt': 'Hello, World!' }); |
| 7 | + const blob = await fs.openAsBlob('/dir/test.txt'); |
| 8 | + expect(blob).toBeInstanceOf(Blob); |
| 9 | + expect(blob.size).toBe(13); |
| 10 | + expect(blob.type).toBe(''); |
| 11 | + |
| 12 | + const text = await blob.text(); |
| 13 | + expect(text).toBe('Hello, World!'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('can read a binary file as blob', async () => { |
| 17 | + const binaryData = Buffer.from([0x89, 0x50, 0x4e, 0x47]); // PNG header |
| 18 | + const { fs } = memfs({ '/image.png': binaryData }); |
| 19 | + const blob = await fs.openAsBlob('/image.png'); |
| 20 | + expect(blob).toBeInstanceOf(Blob); |
| 21 | + expect(blob.size).toBe(4); |
| 22 | + |
| 23 | + const arrayBuffer = await blob.arrayBuffer(); |
| 24 | + const view = new Uint8Array(arrayBuffer); |
| 25 | + expect(Array.from(view)).toEqual([0x89, 0x50, 0x4e, 0x47]); |
| 26 | + }); |
| 27 | + |
| 28 | + it('can specify a mime type', async () => { |
| 29 | + const { fs } = memfs({ '/data.json': '{"key": "value"}' }); |
| 30 | + const blob = await fs.openAsBlob('/data.json', { type: 'application/json' }); |
| 31 | + expect(blob).toBeInstanceOf(Blob); |
| 32 | + expect(blob.type).toBe('application/json'); |
| 33 | + |
| 34 | + const text = await blob.text(); |
| 35 | + expect(text).toBe('{"key": "value"}'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('handles empty files', async () => { |
| 39 | + const { fs } = memfs({ '/empty.txt': '' }); |
| 40 | + const blob = await fs.openAsBlob('/empty.txt'); |
| 41 | + expect(blob).toBeInstanceOf(Blob); |
| 42 | + expect(blob.size).toBe(0); |
| 43 | + |
| 44 | + const text = await blob.text(); |
| 45 | + expect(text).toBe(''); |
| 46 | + }); |
| 47 | + |
| 48 | + it('throws if file does not exist', async () => { |
| 49 | + const { fs } = memfs({ '/dir/test.txt': 'content' }); |
| 50 | + const [, err] = await of(fs.openAsBlob('/dir/test-NOT-FOUND.txt')); |
| 51 | + expect(err).toBeInstanceOf(Error); |
| 52 | + expect((<any>err).code).toBe('ENOENT'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('throws EISDIR if path is a directory', async () => { |
| 56 | + const { fs } = memfs({ '/dir/test.txt': 'content' }); |
| 57 | + const [, err] = await of(fs.openAsBlob('/dir')); |
| 58 | + expect(err).toBeInstanceOf(Error); |
| 59 | + expect((<any>err).code).toBe('EISDIR'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('works with Buffer paths', async () => { |
| 63 | + const { fs } = memfs({ '/test.txt': 'buffer path test' }); |
| 64 | + const pathBuffer = Buffer.from('/test.txt'); |
| 65 | + const blob = await fs.openAsBlob(pathBuffer); |
| 66 | + expect(blob).toBeInstanceOf(Blob); |
| 67 | + |
| 68 | + const text = await blob.text(); |
| 69 | + expect(text).toBe('buffer path test'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('works with different path formats', async () => { |
| 73 | + const { fs } = memfs({ '/path-test.txt': 'path format test' }); |
| 74 | + const blob = await fs.openAsBlob('/path-test.txt'); |
| 75 | + expect(blob).toBeInstanceOf(Blob); |
| 76 | + |
| 77 | + const text = await blob.text(); |
| 78 | + expect(text).toBe('path format test'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('handles large files', async () => { |
| 82 | + const largeContent = 'x'.repeat(10000); |
| 83 | + const { fs } = memfs({ '/large.txt': largeContent }); |
| 84 | + const blob = await fs.openAsBlob('/large.txt'); |
| 85 | + expect(blob).toBeInstanceOf(Blob); |
| 86 | + expect(blob.size).toBe(10000); |
| 87 | + |
| 88 | + const text = await blob.text(); |
| 89 | + expect(text).toBe(largeContent); |
| 90 | + }); |
| 91 | + |
| 92 | + it('can read file through symlink', async () => { |
| 93 | + const { fs } = memfs({ '/original.txt': 'symlink test' }); |
| 94 | + fs.symlinkSync('/original.txt', '/link.txt'); |
| 95 | + |
| 96 | + const blob = await fs.openAsBlob('/link.txt'); |
| 97 | + expect(blob).toBeInstanceOf(Blob); |
| 98 | + |
| 99 | + const text = await blob.text(); |
| 100 | + expect(text).toBe('symlink test'); |
| 101 | + }); |
| 102 | +}); |
0 commit comments