Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
added a mime type for .pages files; added new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webschik committed Jan 19, 2019
1 parent fe33cd9 commit 7113d19
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 9 deletions.
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "file2html",
"version": "0.1.9",
"version": "0.2.0",
"description": "JS convertor of files to HTML and CSS code",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion src/mime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const mimeTypes: {[key: string]: string} = {
djvu: 'image/vnd.djvu',
djv: 'image/vnd.djvu',
zip: 'application/zip',
rtf: 'application/rtf'
rtf: 'application/rtf',
pages: 'application/vnd.apple.pages'
};

export function lookup (filename: string) {
Expand All @@ -34,6 +35,7 @@ export function lookup (filename: string) {
const preparedFilename: string = filename.toLowerCase();

for (const extension in mimeTypes) {
// istanbul ignore else
if (mimeTypes.hasOwnProperty(extension)) {
const {length} = extension;

Expand Down
1 change: 1 addition & 0 deletions src/text-encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export function decode (content: Uint8Array, encoding?: string, options?: TextEn
}

export function encode (text: string, encoding?: string, options?: TextEncoding.TextEncoderOptions): Uint8Array {
// @ts-ignore
return new TextEncoder(encoding, options).encode(text);
}
108 changes: 108 additions & 0 deletions test/unit/index.spec.ts
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'));
});
});
});
});
14 changes: 13 additions & 1 deletion test/unit/mime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import {lookup} from '../../src/mime';

describe('mime', () => {
describe('lookup()', () => {
it('fb2.zip', () => expect(lookup('file.fb2.zip')).toBe('application/x-zip-compressed-fb2'));
it('should check invalid or unknown extensions', () => {
expect(lookup(undefined)).toBeUndefined();
expect(lookup('')).toBeUndefined();
expect(lookup('file.unknown')).toBeUndefined();
});

it('should return a mime-type for compressed FB2 file', () => {
expect(lookup('file.fb2.zip')).toBe('application/x-zip-compressed-fb2');
});

it('should return a mime-type for Apple Pages file', () => {
expect(lookup('file.pages')).toBe('application/vnd.apple.pages');
});
});
});
52 changes: 47 additions & 5 deletions test/unit/text-encoding.spec.ts
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);
});
});
});

0 comments on commit 7113d19

Please sign in to comment.