优化
This commit is contained in:
14
client/shared/utils/__tests__/array-helper.spec.ts
Normal file
14
client/shared/utils/__tests__/array-helper.spec.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { joinArray } from '../array-helper';
|
||||
|
||||
describe('array-helper', () => {
|
||||
test('joinArray', () => {
|
||||
expect(joinArray([1, 2, 3], '5')).toMatchObject([1, '5', 2, '5', 3]);
|
||||
expect(joinArray([{ a: 1 }, { a: 2 }, { a: 3 }], '5')).toMatchObject([
|
||||
{ a: 1 },
|
||||
'5',
|
||||
{ a: 2 },
|
||||
'5',
|
||||
{ a: 3 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
14
client/shared/utils/__tests__/color-scheme-helper.spec.ts
Normal file
14
client/shared/utils/__tests__/color-scheme-helper.spec.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { parseColorScheme } from '../color-scheme-helper';
|
||||
|
||||
describe('parseColorScheme', () => {
|
||||
test.each([
|
||||
['dark', { isDarkMode: true, extraSchemeName: null }],
|
||||
['light', { isDarkMode: false, extraSchemeName: null }],
|
||||
['auto', { isDarkMode: true, extraSchemeName: null }],
|
||||
['dark+miku', { isDarkMode: true, extraSchemeName: 'theme-miku' }],
|
||||
['light+miku', { isDarkMode: false, extraSchemeName: 'theme-miku' }],
|
||||
['miku', { isDarkMode: true, extraSchemeName: 'theme-miku' }],
|
||||
])('%s', (input, output) => {
|
||||
expect(parseColorScheme(input)).toEqual(output);
|
||||
});
|
||||
});
|
||||
21
client/shared/utils/__tests__/date-helper.spec.ts
Normal file
21
client/shared/utils/__tests__/date-helper.spec.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { isToday, getMessageTimeDiff } from '../date-helper';
|
||||
|
||||
describe('isToday', () => {
|
||||
test.each([
|
||||
[new Date(), true],
|
||||
[new Date(new Date().setDate(new Date().getDate() - 1)), false],
|
||||
])('%s => %s', (input, should) => {
|
||||
expect(isToday(input)).toBe(should);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getMessageTimeDiff', () => {
|
||||
test.each([
|
||||
[new Date(), '几秒前'],
|
||||
[new Date(new Date().setMinutes(new Date().getMinutes() - 1)), '1 分钟前'],
|
||||
[new Date(new Date().setHours(new Date().getHours() - 1)), '1 小时前'],
|
||||
[new Date('2020-01-01T00:00:00Z'), '2020-01-01 08:00:00'],
|
||||
])('%s => %s', (input, should) => {
|
||||
expect(getMessageTimeDiff(input)).toBe(should);
|
||||
});
|
||||
});
|
||||
16
client/shared/utils/__tests__/is-promise.spec.ts
Normal file
16
client/shared/utils/__tests__/is-promise.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { isPromise } from '../is-promise';
|
||||
|
||||
describe('isPromise', () => {
|
||||
test.each([
|
||||
[Promise.resolve(), true],
|
||||
['str', false],
|
||||
[123, false],
|
||||
[[], false],
|
||||
[{}, false],
|
||||
[Symbol('sym'), false],
|
||||
[undefined, false],
|
||||
[null, false],
|
||||
])('%s => %s', (input, should) => {
|
||||
expect(isPromise(input)).toBe(should);
|
||||
});
|
||||
});
|
||||
18
client/shared/utils/__tests__/json-helper.spec.ts
Normal file
18
client/shared/utils/__tests__/json-helper.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { isValidJson } from '../json-helper';
|
||||
|
||||
describe('isValidJson', () => {
|
||||
test.each([
|
||||
['foo', false],
|
||||
['[]', true],
|
||||
['{}', true],
|
||||
['{"foo": []}', true],
|
||||
['{"foo": [}', false],
|
||||
['{foo: bar}', false],
|
||||
['{"foo": "bar"}', true],
|
||||
[[], false],
|
||||
[null, false],
|
||||
[undefined, false],
|
||||
])('%s => %s', (input: any, should) => {
|
||||
expect(isValidJson(input)).toBe(should);
|
||||
});
|
||||
});
|
||||
41
client/shared/utils/__tests__/string-helper.spec.ts
Normal file
41
client/shared/utils/__tests__/string-helper.spec.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { isAvailableString, isObjectId, isUrl } from '../string-helper';
|
||||
|
||||
describe('string-helper', () => {
|
||||
describe('isAvailableString', () => {
|
||||
test.each<[any, boolean]>([
|
||||
['any string', true],
|
||||
['', false],
|
||||
[1, false],
|
||||
[() => {}, false],
|
||||
[{}, false],
|
||||
[[], false],
|
||||
[undefined, false],
|
||||
[null, false],
|
||||
])('%p => %p', (url, res) => {
|
||||
expect(isAvailableString(url)).toBe(res);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isUrl', () => {
|
||||
test.each<[string, boolean]>([
|
||||
['http://baidu.com', true],
|
||||
['https://baidu.com', true],
|
||||
['ws://baidu.com', true],
|
||||
['wss://baidu.com', true],
|
||||
['baidu.com', false],
|
||||
['baidu', false],
|
||||
])('%s => %p', (url, res) => {
|
||||
expect(isUrl(url)).toBe(res);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isObjectId', () => {
|
||||
test.each<[string, boolean]>([
|
||||
['1', false],
|
||||
['unknown', false],
|
||||
['64b4a473a44c273805b25da5', true],
|
||||
])('%s => %p', (input, res) => {
|
||||
expect(isObjectId(input)).toBe(res);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user