|
1 |
| -import {COMMON_MIME_TYPES, toFileWithPath} from './file'; |
| 1 | +import {clone, COMMON_MIME_TYPES, toFileWithPath} from './file'; |
| 2 | + |
| 3 | +class MockFile { |
| 4 | + public lastModified: number = 0; |
| 5 | + public name: string = ''; |
| 6 | + public size: number = 0; |
| 7 | + public type: string = ''; |
| 8 | + public blob: Blob; |
| 9 | + |
| 10 | + constructor(fileBits: BlobPart[], fileName: string, options: FilePropertyBag) { |
| 11 | + this.lastModified = options.lastModified || 0; |
| 12 | + this.name = fileName; |
| 13 | + this.type = options.type || ''; |
| 14 | + this.blob = new Blob(fileBits); |
| 15 | + this.size = this.blob.size; |
| 16 | + throw new Error('Error!'); |
| 17 | + } |
| 18 | + |
| 19 | + public slice() { |
| 20 | + return this.blob; |
| 21 | + } |
| 22 | +} |
2 | 23 |
|
3 | 24 | describe('toFile()', () => {
|
4 | 25 | it('should be an instance of a File', () => {
|
@@ -56,6 +77,27 @@ describe('toFile()', () => {
|
56 | 77 | expect(fwp.lastModified).toEqual(file.lastModified);
|
57 | 78 | });
|
58 | 79 |
|
| 80 | + it('clones the File as a blob', () => { |
| 81 | + const g = global as any; |
| 82 | + const opts: FilePropertyBag = { |
| 83 | + type: 'plain/text', |
| 84 | + lastModified: 1234567 |
| 85 | + }; |
| 86 | + const data = JSON.stringify({ping: true}); |
| 87 | + const file = new File([data], 'test.txt', opts); |
| 88 | + g.OriginalFile = File; |
| 89 | + g.File = MockFile; |
| 90 | + const clonedFile = clone(file); |
| 91 | + g.File = g.OriginalFile; |
| 92 | + |
| 93 | + expect(clonedFile).toBeInstanceOf(Blob); |
| 94 | + expect(clonedFile === file).toBe(false); |
| 95 | + expect(clonedFile.name).toEqual(file.name); |
| 96 | + expect(clonedFile.type).toEqual(file.type); |
| 97 | + expect(clonedFile.size).toEqual(file.size); |
| 98 | + expect(clonedFile.lastModified).toEqual(file.lastModified); |
| 99 | + }); |
| 100 | + |
59 | 101 | it('should behave like a File', done => {
|
60 | 102 | const data = {ping: true};
|
61 | 103 | const json = JSON.stringify(data);
|
|
0 commit comments