Skip to content

Commit

Permalink
feat: 添加测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
lao-jiawei committed Jun 18, 2024
1 parent b3e768b commit 1df95fa
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ yarn-error.log*

dist/

*.test.js
*.DS_store
9 changes: 9 additions & 0 deletions test/Format.test.js
Original file line number Diff line number Diff line change
@@ -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');
})
11 changes: 11 additions & 0 deletions test/arrayIsEqual.test.js
Original file line number Diff line number Diff line change
@@ -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);
})
11 changes: 11 additions & 0 deletions test/curry.test.js
Original file line number Diff line number Diff line change
@@ -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);
})
13 changes: 13 additions & 0 deletions test/debounce.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
8 changes: 8 additions & 0 deletions test/filterObj.test.js
Original file line number Diff line number Diff line change
@@ -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 });
})
7 changes: 7 additions & 0 deletions test/groupByValue.test.js
Original file line number Diff line number Diff line change
@@ -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'] });
})
12 changes: 12 additions & 0 deletions test/isEmpty.test.js
Original file line number Diff line number Diff line change
@@ -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);
})
41 changes: 41 additions & 0 deletions test/nestByKey.test.js
Original file line number Diff line number Diff line change
@@ -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: []
}
]
}
]);
})
15 changes: 15 additions & 0 deletions test/objToPath.test.js
Original file line number Diff line number Diff line change
@@ -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')
})
15 changes: 15 additions & 0 deletions test/pathToObj.test.js
Original file line number Diff line number Diff line change
@@ -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',
})
})

0 comments on commit 1df95fa

Please sign in to comment.