This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a mime type for .pages files; added new tests
- Loading branch information
Showing
7 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import {config, FileTypes, read, Reader, File} from '../../src'; | ||
|
||
describe('index', () => { | ||
describe('File', () => { | ||
it('should create a File instance', () => { | ||
const file = new File({ | ||
meta: {mimeType: 'text/plain'} | ||
} as any); | ||
|
||
expect(file.getMeta()).toEqual({mimeType: 'text/plain'}); | ||
expect(file.getData()).toEqual({ | ||
meta: {mimeType: 'text/plain'} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Reader', () => { | ||
it('should return a false on file check by default', () => { | ||
expect(Reader.testFileMimeType('text/plain')).toBe(false); | ||
}); | ||
|
||
it('should return an error on file read by default', () => { | ||
const reader = new Reader(); | ||
|
||
expect.assertions(1); | ||
return reader.read({ | ||
fileInfo: { | ||
content: new Uint8Array([]), | ||
meta: {} | ||
} | ||
}).catch((error: Error) => { | ||
expect(error).toEqual(new Error('Invalid file')); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('FileTypes', () => { | ||
it('should export a file types map', () => { | ||
expect(FileTypes).toEqual({ | ||
document: 1, | ||
presentation: 2, | ||
spreadsheet: 3, | ||
drawing: 4, | ||
image: 5 | ||
}); | ||
}); | ||
}); | ||
|
||
describe('config(), read()', () => { | ||
const reader1 = { | ||
read: jest.fn() | ||
}; | ||
const ReaderConstructor1: any = jest.fn(() => reader1); | ||
|
||
ReaderConstructor1.testFileMimeType = jest.fn(); | ||
const reader2 = { | ||
read: jest.fn() | ||
}; | ||
const ReaderConstructor2: any = jest.fn(() => reader2); | ||
|
||
ReaderConstructor2.testFileMimeType = jest.fn(); | ||
|
||
beforeAll(() => { | ||
config({ | ||
readers: [ | ||
ReaderConstructor1 as any, | ||
ReaderConstructor2 as any | ||
] | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
reader1.read.mockClear(); | ||
reader2.read.mockClear(); | ||
ReaderConstructor1.mockClear(); | ||
ReaderConstructor1.testFileMimeType.mockClear(); | ||
ReaderConstructor2.mockClear(); | ||
ReaderConstructor2.testFileMimeType.mockClear(); | ||
}); | ||
|
||
it('should find an appropriate reader for the file', () => { | ||
ReaderConstructor2.testFileMimeType.mockImplementationOnce(() => true); | ||
reader2.read.mockImplementationOnce(() => Promise.resolve()); | ||
|
||
return read({fileBuffer: Buffer.from([]), meta: {mimeType: 'text/plain'}}).then(() => { | ||
expect(ReaderConstructor1).toHaveBeenCalledTimes(0); | ||
expect(reader1.read).toHaveBeenCalledTimes(0); | ||
expect(ReaderConstructor2.testFileMimeType).toHaveBeenCalledWith('text/plain'); | ||
expect(ReaderConstructor2).toHaveBeenCalledTimes(1); | ||
expect(reader2.read).toHaveBeenCalledWith({ | ||
fileInfo: { | ||
content: new Uint8Array([]), | ||
meta: {mimeType: 'text/plain'} | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
it('should return an error if there is no reader for the file', () => { | ||
expect.assertions(3); | ||
return read({fileBuffer: Buffer.from([]), meta: {}}).catch((error) => { | ||
expect(ReaderConstructor1).toHaveBeenCalledTimes(0); | ||
expect(ReaderConstructor2).toHaveBeenCalledTimes(0); | ||
expect(error).toEqual(new Error('file2html.errors.unsupportedFile')); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,56 @@ | ||
import {decode} from '../../src/text-encoding'; | ||
import {TextDecoder} from 'text-encoding'; | ||
import {decode, encode} from '../../src/text-encoding'; | ||
|
||
describe('text-encoding', () => { | ||
const {TextDecoder: OriginTextDecoder, TextEncoder: OriginTextEncoder} = global as any; | ||
const text = 'Hello'; | ||
const bytes = new Uint8Array([72, 101, 108, 108, 111]); | ||
const decoder = { | ||
decode: jest.fn(() => text) | ||
}; | ||
const TextDecoder = jest.fn(() => { | ||
return decoder; | ||
}); | ||
const encoder = { | ||
encode: jest.fn(() => bytes) | ||
}; | ||
const TextEncoder = jest.fn(() => { | ||
return encoder; | ||
}); | ||
|
||
beforeAll(() => { | ||
(window as any).TextDecoder = TextDecoder; | ||
(global as any).TextDecoder = TextDecoder; | ||
(global as any).TextEncoder = TextEncoder; | ||
}); | ||
|
||
afterEach(() => { | ||
decoder.decode.mockClear(); | ||
encoder.encode.mockClear(); | ||
TextDecoder.mockClear(); | ||
TextEncoder.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
(global as any).TextDecoder = OriginTextDecoder; | ||
(global as any).TextEncoder = OriginTextEncoder; | ||
}); | ||
|
||
describe('decode()', () => { | ||
it('should convert bytes array to string', () => { | ||
expect(decode(new Uint8Array([72, 101, 108, 108, 111]))).toBe('Hello'); | ||
it('should convert a bytes array to string', () => { | ||
const textResult = decode(bytes); | ||
|
||
expect(TextDecoder).toHaveBeenCalledWith(undefined, undefined); | ||
expect(decoder.decode).toHaveBeenCalledWith(bytes); | ||
expect(textResult).toBe(text); | ||
}); | ||
}); | ||
|
||
describe('encode()', () => { | ||
it('should convert a string to bytes array', () => { | ||
const bytesResult = encode(text); | ||
|
||
expect(TextEncoder).toHaveBeenCalledWith(undefined, undefined); | ||
expect(encoder.encode).toHaveBeenCalledWith(text); | ||
expect(bytesResult).toBe(bytes); | ||
}); | ||
}); | ||
}); |