Skip to content

Commit

Permalink
refactor: add unit test configuration file and unit test code
Browse files Browse the repository at this point in the history
  • Loading branch information
bdwenxi committed Nov 9, 2020
1 parent 11abcc6 commit ad9bee0
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ Thumbs.db
es/
cjs/
packages/doc/.docz
dist
16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('path');

module.exports = {
moduleDirectories: ['src', 'node_modules'],
moduleFileExtensions: ['js', 'ts', 'jsx', 'tsx', 'd.ts'],
transform: {
'^.+\\.(js|jsx|ts|tsx)$': path.resolve(
'./node_modules/reskript/dist/config/jest/transformer'
)
},
coverageReporters: ['json-summary', 'lcov', 'text', 'clover'],
testMatch: ['**/__tests__/**/*.test.{js,jsx,ts,tsx}'],
collectCoverageFrom: ['<rootDir>/src/**/*.{js,jsx,ts,tsx}'],
coveragePathIgnorePatterns: ['/node_modules/', '/__tests__/'],
testEnvironment: 'jsdom'
};
51 changes: 27 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,53 @@
"name": "file2md5",
"version": "1.0.0",
"description": "file2md5 is a browser-side library based on SparkMD5, which supports typescript friendly.",
"main": "lib/index.js",
"module": "es/index.js",
"types": "es/index.d.ts",
"repository": "[email protected]:bdwenxi/file2md5.git",
"homepage": "https://github.com/bdwenxi/file2md5",
"repository": "[email protected]:bdwenxi/file2md5.git",
"license": "MIT",
"author": "bdwenxi <[email protected]>",
"contributors": [
{
"name": "bdwenxi",
"email": "[email protected]"
}
],
"scripts": {
"build": "rm -rf es cjs && tsc & tsc --module esnext --outDir ./es",
"build:check": "tsc",
"test": "skr test --coverage"
},
"main": "lib/index.js",
"module": "es/index.js",
"types": "es/index.d.ts",
"files": [
"src",
"cjs",
"es"
],
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
"scripts": {
"build": "rm -rf es cjs && tsc & tsc --module esnext --outDir ./es",
"build:check": "tsc",
"test": "skr test --coverage"
},
"husky": {
"hooks": {
"prepare-commit-msg": "exec < /dev/tty && git cz --hook || true"
}
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"lodash.noop": "^3.0.1",
"spark-md5": "^3.0.1"
},
"license": "MIT",
"devDependencies": {
"@types/jest": "^26.0.15",
"@types/lodash.noop": "^3.0.6",
"@types/spark-md5": "^3.0.2",
"cz-conventional-changelog": "3.3.0",
"husky": "^4.3.0",
"reskript": "^0.26.7",
"typescript": "^4.0.5"
},
"dependencies": {
"spark-md5": "^3.0.1"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"husky": {
"hooks": {
"prepare-commit-msg": "exec < /dev/tty && git cz --hook || true"
}
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
}
}
55 changes: 55 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import file2md5 from '../index';

jest.setTimeout(300000);

describe('file2md5', () => {
test('test basic usage', async () => {
const file = new window.File([new ArrayBuffer(1024)], 'test.txt');

const md5 = await file2md5(file, {chunkSize: 3 * 1024 * 1024});
expect(file).not.toBeNull();
expect(typeof md5).toBe('string');
expect(md5.length).toBeGreaterThan(0);
});

test('test "onProgress" callback', async () => {
const file = new window.File([new ArrayBuffer(1024)], 'test.txt');
const onProgress = jest.fn();

await file2md5(file, {chunkSize: 3 * 1024 * 1024, onProgress});

expect(onProgress).toHaveBeenCalled();
});

test('test "raw" property', async () => {
const file = new window.File([new ArrayBuffer(1024)], 'test.txt');
const md5 = await file2md5(file, {chunkSize: 3 * 1024 * 1024, raw: true});

expect(typeof md5).toBe('string');
expect(md5.length).toBeGreaterThan(0);
});

test('test huge file', async () => {
const file = new window.File([new ArrayBuffer(300 * 1024 * 1024)], 'test.txt');
const md5 = await file2md5(file, {chunkSize: 3 * 1024 * 1024});

expect(typeof md5).toBe('string');
expect(md5.length).toBeGreaterThan(0);
});

test('test abort method', async () => {
const file = new window.File([new ArrayBuffer(700 * 1024 * 1024)], 'test.txt');
const abort = jest.fn().mockImplementation(() => file2md5.abort());

setTimeout(
() => abort(),
1500
);

const md5 = await file2md5(file, {chunkSize: 3 * 1024 * 1024});

expect(abort).toHaveBeenCalled();
expect(typeof md5).toBe('string');
expect(md5).toBe('');
});
});
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import SparkMD5 from 'spark-md5';
import noop from 'lodash.noop';

export interface IOptions {
chunkSize?: number;
raw?: boolean;
onProgress?: (progress: number) => unknown;
}

type IResolvedResult = [md5: string, abort: () => void];
export interface IFile2Md5 {
(file: File, options: IOptions): Promise<string>;
abort(): void;
}

const file2md5 = function (file: File, options: IOptions = {}): Promise<IResolvedResult> {
const file2md5: IFile2Md5 = function (file: File, options: IOptions = {}): Promise<string> {
const {chunkSize = 2 * 1024 * 1024, raw = false, onProgress} = options;
const spark = new SparkMD5.ArrayBuffer();
const fileReader = new FileReader();
Expand All @@ -28,7 +32,7 @@ const file2md5 = function (file: File, options: IOptions = {}): Promise<IResolve
fileReader.abort();
};

const execute = function (resolve: (...IResolvedResult) => void, reject: (err: DOMException | null) => void): void {
const execute = function (resolve: (md5: string) => void, reject: (err: DOMException | null) => void): void {
fileReader.addEventListener(
'load',
e => {
Expand All @@ -43,7 +47,7 @@ const file2md5 = function (file: File, options: IOptions = {}): Promise<IResolve
}

// If raw is true, the result as a binary string will be returned instead
resolve([spark.end(raw), abort]);
resolve(spark.end(raw));
}
);

Expand All @@ -52,6 +56,7 @@ const file2md5 = function (file: File, options: IOptions = {}): Promise<IResolve
() => {
// Resets the internal state of the computation
spark.reset();
resolve('');
}
);

Expand All @@ -67,7 +72,11 @@ const file2md5 = function (file: File, options: IOptions = {}): Promise<IResolve
loadNext();
};

return new Promise<IResolvedResult>(execute);
file2md5.abort = abort;

return new Promise<string>(execute);
};

file2md5.abort = noop;

export default file2md5;
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
"moduleResolution": "node",
"noUnusedParameters": true,
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"typeRoots": [
"./node_modules/@types",
"./types"
]
},
"include": ["src"]
}
78 changes: 78 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,17 @@
"@types/yargs" "^15.0.0"
chalk "^3.0.0"

"@jest/types@^26.6.2":
version "26.6.2"
resolved "http://registry.npm.baidu-int.com/@jest%2ftypes/-/types-26.6.2.tgz#bef5a532030e1d88a2f5a6d933f84e97226ed48e"
integrity sha1-vvWlMgMOHYii9abZM/hOlyJu1I4=
dependencies:
"@types/istanbul-lib-coverage" "^2.0.0"
"@types/istanbul-reports" "^3.0.0"
"@types/node" "*"
"@types/yargs" "^15.0.0"
chalk "^4.0.0"

"@nodelib/[email protected]":
version "2.1.3"
resolved "http://registry.npm.baidu-int.com/@nodelib%2ffs.scandir/-/fs.scandir-2.1.3.tgz#3a582bdb53804c6ba6d146579c46e52130cf4a3b"
Expand Down Expand Up @@ -1568,11 +1579,38 @@
"@types/istanbul-lib-coverage" "*"
"@types/istanbul-lib-report" "*"

"@types/istanbul-reports@^3.0.0":
version "3.0.0"
resolved "http://registry.npm.baidu-int.com/@types%2fistanbul-reports/-/istanbul-reports-3.0.0.tgz#508b13aa344fa4976234e75dddcc34925737d821"
integrity sha1-UIsTqjRPpJdiNOdd3cw0klc32CE=
dependencies:
"@types/istanbul-lib-report" "*"

"@types/jest@^26.0.15":
version "26.0.15"
resolved "http://registry.npm.baidu-int.com/@types%2fjest/-/jest-26.0.15.tgz#12e02c0372ad0548e07b9f4e19132b834cb1effe"
integrity sha1-EuAsA3KtBUjge59OGRMrg0yx7/4=
dependencies:
jest-diff "^26.0.0"
pretty-format "^26.0.0"

"@types/json-schema@^7.0.3", "@types/json-schema@^7.0.5", "@types/json-schema@^7.0.6":
version "7.0.6"
resolved "http://registry.npm.baidu-int.com/@types%2fjson-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0"
integrity sha1-9MfsQ+gbMZqYFRFQMXCfJph4kfA=

"@types/lodash.noop@^3.0.6":
version "3.0.6"
resolved "http://registry.npm.baidu-int.com/@types%2flodash.noop/-/lodash.noop-3.0.6.tgz#2be7ceeabe21af841c76b57b6ff12bb04b7ad21f"
integrity sha1-K+fO6r4hr4QcdrV7b/ErsEt60h8=
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.165"
resolved "http://registry.npm.baidu-int.com/@types%2flodash/-/lodash-4.14.165.tgz#74d55d947452e2de0742bad65270433b63a8c30f"
integrity sha1-dNVdlHRS4t4HQrrWUnBDO2Ooww8=

"@types/minimatch@*":
version "3.0.3"
resolved "http://registry.npm.baidu-int.com/@types%2fminimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down Expand Up @@ -3943,6 +3981,11 @@ diff-sequences@^25.2.6:
resolved "http://registry.npm.baidu-int.com/diff-sequences/-/diff-sequences-25.2.6.tgz#5f467c00edd35352b7bca46d7927d60e687a76dd"
integrity sha1-X0Z8AO3TU1K3vKRteSfWDmh6dt0=

diff-sequences@^26.6.2:
version "26.6.2"
resolved "http://registry.npm.baidu-int.com/diff-sequences/-/diff-sequences-26.6.2.tgz#48ba99157de1923412eed41db6b6d4aa9ca7c0b1"
integrity sha1-SLqZFX3hkjQS7tQdtrbUqpynwLE=

dir-glob@^2.0.0:
version "2.2.2"
resolved "http://registry.npm.baidu-int.com/dir-glob/-/dir-glob-2.2.2.tgz#fa09f0694153c8918b18ba0deafae94769fc50c4"
Expand Down Expand Up @@ -6833,6 +6876,16 @@ jest-diff@^25.5.0:
jest-get-type "^25.2.6"
pretty-format "^25.5.0"

jest-diff@^26.0.0:
version "26.6.2"
resolved "http://registry.npm.baidu-int.com/jest-diff/-/jest-diff-26.6.2.tgz#1aa7468b52c3a68d7d5c5fdcdfcd5e49bd164394"
integrity sha1-GqdGi1LDpo19XF/c381eSb0WQ5Q=
dependencies:
chalk "^4.0.0"
diff-sequences "^26.6.2"
jest-get-type "^26.3.0"
pretty-format "^26.6.2"

jest-docblock@^25.3.0:
version "25.3.0"
resolved "http://registry.npm.baidu-int.com/jest-docblock/-/jest-docblock-25.3.0.tgz#8b777a27e3477cd77a168c05290c471a575623ef"
Expand Down Expand Up @@ -6880,6 +6933,11 @@ jest-get-type@^25.2.6:
resolved "http://registry.npm.baidu-int.com/jest-get-type/-/jest-get-type-25.2.6.tgz#0b0a32fab8908b44d508be81681487dbabb8d877"
integrity sha1-Cwoy+riQi0TVCL6BaBSH26u42Hc=

jest-get-type@^26.3.0:
version "26.3.0"
resolved "http://registry.npm.baidu-int.com/jest-get-type/-/jest-get-type-26.3.0.tgz#e97dc3c3f53c2b406ca7afaed4493b1d099199e0"
integrity sha1-6X3Dw/U8K0Bsp6+u1Ek7HQmRmeA=

jest-haste-map@^25.5.1:
version "25.5.1"
resolved "http://registry.npm.baidu-int.com/jest-haste-map/-/jest-haste-map-25.5.1.tgz#1df10f716c1d94e60a1ebf7798c9fb3da2620943"
Expand Down Expand Up @@ -7554,6 +7612,11 @@ lodash.memoize@^4.1.2:
resolved "http://registry.npm.baidu-int.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=

lodash.noop@^3.0.1:
version "3.0.1"
resolved "http://registry.npm.baidu-int.com/lodash.noop/-/lodash.noop-3.0.1.tgz#38188f4d650a3a474258439b96ec45b32617133c"
integrity sha1-OBiPTWUKOkdCWEObluxFsyYXEzw=

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "http://registry.npm.baidu-int.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down Expand Up @@ -9471,6 +9534,16 @@ pretty-format@^25.5.0:
ansi-styles "^4.0.0"
react-is "^16.12.0"

pretty-format@^26.0.0, pretty-format@^26.6.2:
version "26.6.2"
resolved "http://registry.npm.baidu-int.com/pretty-format/-/pretty-format-26.6.2.tgz#e35c2705f14cb7fe2fe94fa078345b444120fc93"
integrity sha1-41wnBfFMt/4v6U+geDRbREEg/JM=
dependencies:
"@jest/types" "^26.6.2"
ansi-regex "^5.0.0"
ansi-styles "^4.0.0"
react-is "^17.0.1"

pretty-time@^1.1.0:
version "1.1.0"
resolved "http://registry.npm.baidu-int.com/pretty-time/-/pretty-time-1.1.0.tgz#ffb7429afabb8535c346a34e41873adf3d74dd0e"
Expand Down Expand Up @@ -9707,6 +9780,11 @@ react-is@^16.12.0, react-is@^16.13.1, react-is@^16.8.1, react-is@^16.8.6:
resolved "http://registry.npm.baidu-int.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha1-eJcppNw23imZ3BVt1sHZwYzqVqQ=

react-is@^17.0.1:
version "17.0.1"
resolved "http://registry.npm.baidu-int.com/react-is/-/react-is-17.0.1.tgz#5b3531bd76a645a4c9fb6e693ed36419e3301339"
integrity sha1-WzUxvXamRaTJ+25pPtNkGeMwEzk=

react-monaco-editor@^0.36.0:
version "0.36.0"
resolved "http://registry.npm.baidu-int.com/react-monaco-editor/-/react-monaco-editor-0.36.0.tgz#ac085c14f25fb072514c925596f6a06a711ee078"
Expand Down

0 comments on commit ad9bee0

Please sign in to comment.