diff --git a/.gitignore b/.gitignore index 1d90dc1..d3b390d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,4 @@ yarn-error.log* dist/ -*.test.js *.DS_store \ No newline at end of file diff --git a/test/Format.test.js b/test/Format.test.js new file mode 100644 index 0000000..7bb6047 --- /dev/null +++ b/test/Format.test.js @@ -0,0 +1,9 @@ +import { Format } from "../src"; + +test('测试Form.formatMoney', () => { + expect(Format.formatMoney(12341234.246)).toBe('12,341,234.25'); + + expect(Format.formatMoney(12341234.246, '$')).toBe('$12,341,234.25'); + + expect(Format.formatMoney(12341234.246, '$', 1)).toBe('$12,341,234.2'); +}) \ No newline at end of file diff --git a/test/arrayIsEqual.test.js b/test/arrayIsEqual.test.js new file mode 100644 index 0000000..57fc9c9 --- /dev/null +++ b/test/arrayIsEqual.test.js @@ -0,0 +1,11 @@ +import arrayIsEqual from "../src/arrayIsEqual"; + +test('判断数组是否相等', () => { + expect(arrayIsEqual([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])).toBe(true); + + expect(arrayIsEqual([1, 2, 3, 4, '5'], [1, 2, 3, 4, '5'])).toBe(true); + + expect(arrayIsEqual([1, 2, 3, 4, '5'], [1, 2, 3, 4, 5])).toBe(false); + + expect(arrayIsEqual([null, undefined, NaN], [null, undefined, NaN])).toBe(true); +}) diff --git a/test/curry.test.js b/test/curry.test.js new file mode 100644 index 0000000..f16aee8 --- /dev/null +++ b/test/curry.test.js @@ -0,0 +1,11 @@ +import curry from "../src/curry"; + +test('柯里化加法', () => { + function add(x, y, z, m) { + return x + y + z + m + } + + expect(curry(add, 1)(2)(3)(4)).toBe(10); + + expect(curry(add, 2)(2)(3)(3)).toBe(10); +}) diff --git a/test/debounce.test.js b/test/debounce.test.js new file mode 100644 index 0000000..efc0de5 --- /dev/null +++ b/test/debounce.test.js @@ -0,0 +1,13 @@ + +// jest.useFakeTimers(); +// jest.spyOn(global, 'setTimeout'); + +test('debounce--测试用例', () => { + const sum = (a, b) => (a + b); + expect(sum(1,2)).toBe(3); + + // debounce(sum, 1000); + + // expect(setTimeout).toHaveBeenCalledTimes(1); + // expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 1000); +}); diff --git a/test/filterObj.test.js b/test/filterObj.test.js new file mode 100644 index 0000000..95eda28 --- /dev/null +++ b/test/filterObj.test.js @@ -0,0 +1,8 @@ +import { filterObj } from "../src"; +test('测试filerOj', () => { + const obj = { a: 1, b: '2', c: 3 }; + + expect(filterObj.pick(obj, ['a', 'c'])).toEqual({ 'a': 1, 'c': 3 }); + + expect(filterObj.omit(obj, ['b'])).toEqual({ 'a': 1, 'c': 3 }); +}) \ No newline at end of file diff --git a/test/groupByValue.test.js b/test/groupByValue.test.js new file mode 100644 index 0000000..fbe9d4e --- /dev/null +++ b/test/groupByValue.test.js @@ -0,0 +1,7 @@ +import { groupByValue } from "../src"; +test('测试groupByValue', () => { + + expect(groupByValue({ a: 1, b: 2, c: 1 })).toEqual({ 1: ['a', 'c'], 2: ['b'] }); + + expect(groupByValue({ a: 1, b: 2, c: 1, d: 2 })).toEqual({ 1: ['a', 'c'], 2: ['b', 'd'] }); +}) \ No newline at end of file diff --git a/test/isEmpty.test.js b/test/isEmpty.test.js new file mode 100644 index 0000000..3ee1497 --- /dev/null +++ b/test/isEmpty.test.js @@ -0,0 +1,12 @@ +import { isEmpty } from "../src"; +test('测试isEmpty', () => { + expect(isEmpty([])).toBe(true); + expect(isEmpty({})).toBe(true); + expect(isEmpty('')).toBe(true); + expect(isEmpty(null)).toBe(true); + expect(isEmpty(undefined)).toBe(true); + + expect(isEmpty([1, 2])).toBe(false); + expect(isEmpty({ a: 1, b: 2 })).toBe(false); + expect(isEmpty('text')).toBe(false); +}) \ No newline at end of file diff --git a/test/nestByKey.test.js b/test/nestByKey.test.js new file mode 100644 index 0000000..8cf1950 --- /dev/null +++ b/test/nestByKey.test.js @@ -0,0 +1,41 @@ +import { nestByKey } from "../src"; + +test('测试nestByKey', () => { + const comments = [ + { id: 1, parentId: null }, + { id: 2, parentId: 1 }, + { id: 3, parentId: 1 }, + { id: 4, parentId: 2 }, + { id: 5, parentId: 4 } + ]; + expect(nestByKey(comments)).toEqual([ + { + id: 1, + parentId: null, + children: [ + { + id: 2, + parentId: 1, + children: [ + { + id: 4, + parentId: 2, + children: [ + { + id: 5, + parentId: 4, + children: [] + } + ] + } + ] + }, + { + id: 3, + parentId: 1, + children: [] + } + ] + } + ]); +}) \ No newline at end of file diff --git a/test/objToPath.test.js b/test/objToPath.test.js new file mode 100644 index 0000000..1d71a71 --- /dev/null +++ b/test/objToPath.test.js @@ -0,0 +1,15 @@ +import objToPath from "../src/objToPath"; + +test('objToPath--测试用例', () => { + // 单参数 + expect(objToPath({ + test1: 'test1', + })).toBe('test1=test1') + + // 多类型参数 + expect(objToPath({ + string: 'string', + number: 1, + boolean: true, + })).toBe('string=string&number=1&boolean=true') +}) \ No newline at end of file diff --git a/test/pathToObj.test.js b/test/pathToObj.test.js new file mode 100644 index 0000000..003d4de --- /dev/null +++ b/test/pathToObj.test.js @@ -0,0 +1,15 @@ +import pathToObj from "../src/pathToObj"; + +test('pathToObj--测试用例', () => { + // 单参数 + expect(pathToObj('test1=test1')).toEqual({ + test1: 'test1', + }) + + // 多类型参数 + expect(pathToObj('string=string&number=1&boolean=true')).toEqual({ + string: 'string', + number: '1', + boolean: 'true', + }) +}) \ No newline at end of file