Skip to content

Commit

Permalink
test: 更新工具函数单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyh2001 committed May 6, 2024
1 parent 488ba58 commit 6b50518
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 4 deletions.
156 changes: 156 additions & 0 deletions packages/fighting-design/_utils/is/__test__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { expect, test } from 'vitest'
import { isDate, isString, isNumber, isBoolean, isObject, isFunction, isArray, isTrue } from '..'

test('isDate', () => {
expect(isDate(1)).toBe(false)
expect(isDate('2024')).toBe(false)
expect(isDate(true)).toBe(false)
expect(isDate(null)).toBe(false)
expect(isDate(void 0)).toBe(false)
expect(isDate({})).toBe(false)
expect(isDate([])).toBe(false)
expect(isDate(Symbol)).toBe(false)
expect(isDate(new Set())).toBe(false)
expect(isDate(Date())).toBe(false)
expect(isDate(new Date())).toBe(true)
expect(isDate(new Date)).toBe(true)
expect(isDate(Date)).toBe(false)
})

test('isString', () => {
expect(isString(1)).toBe(false)
expect(isString('2024')).toBe(true)
expect(isString(`2024`)).toBe(true)
expect(isString(true)).toBe(false)
expect(isString(null)).toBe(false)
expect(isString(void 0)).toBe(false)
expect(isString({})).toBe(false)
expect(isString([])).toBe(false)
expect(isString(new Set())).toBe(false)
expect(isString(new Date)).toBe(false)
expect(isString(Date)).toBe(false)
})

test('isNumber', () => {
expect(isNumber(1)).toBe(true)
expect(isNumber(1.111)).toBe(true)
expect(isNumber(NaN)).toBe(false)
expect(isNumber('2024')).toBe(false)
expect(isNumber(`2024`)).toBe(false)
expect(isNumber(false)).toBe(false)
expect(isNumber(null)).toBe(false)
expect(isNumber(void 0)).toBe(false)
expect(isNumber({})).toBe(false)
expect(isNumber([])).toBe(false)
expect(isNumber(new Set())).toBe(false)
expect(isNumber(new Date)).toBe(false)
expect(isNumber(Date)).toBe(false)
})

test('isBoolean', () => {
expect(isBoolean(1)).toBe(false)
expect(isBoolean(1.111)).toBe(false)
expect(isBoolean(NaN)).toBe(false)
expect(isBoolean('2024')).toBe(false)
expect(isBoolean(`2024`)).toBe(false)
expect(isBoolean(false)).toBe(true)
expect(isBoolean(true)).toBe(true)
expect(isBoolean(null)).toBe(false)
expect(isBoolean(!null)).toBe(true)
expect(isBoolean(void 0)).toBe(false)
expect(isBoolean({})).toBe(false)
expect(isBoolean([])).toBe(false)
expect(isBoolean(new Set())).toBe(false)
expect(isBoolean(new Date)).toBe(false)
expect(isBoolean(Date)).toBe(false)
})

class User { }

test('isObject', () => {
expect(isObject(1)).toBe(false)
expect(isObject(1.111)).toBe(false)
expect(isObject(NaN)).toBe(false)
expect(isObject('2024')).toBe(false)
expect(isObject(`2024`)).toBe(false)
expect(isObject(false)).toBe(false)
expect(isObject(true)).toBe(false)
expect(isObject(null)).toBe(false)
expect(isObject(!null)).toBe(false)
expect(isObject(void 0)).toBe(false)
expect(isObject({})).toBe(true)
expect(isObject(new User)).toBe(true)
expect(isObject([])).toBe(false)
expect(isObject(new Set())).toBe(false)
expect(isObject(new Date)).toBe(false)
expect(isObject(Date)).toBe(false)
})

test('isFunction', () => {
expect(isFunction(1)).toBe(false)
expect(isFunction(1.111)).toBe(false)
expect(isFunction(NaN)).toBe(false)
expect(isFunction('2024')).toBe(false)
expect(isFunction(`2024`)).toBe(false)
expect(isFunction(false)).toBe(false)
expect(isFunction(true)).toBe(false)
expect(isFunction(null)).toBe(false)
expect(isFunction(!null)).toBe(false)
expect(isFunction(void 0)).toBe(false)
expect(isFunction({})).toBe(false)
expect(isFunction(new User)).toBe(false)
expect(isFunction([])).toBe(false)
expect(isFunction(new Set())).toBe(false)
expect(isFunction(new Date)).toBe(false)
expect(isFunction(Date)).toBe(true)
expect(isFunction(new Function())).toBe(true)
expect(isFunction(new Function)).toBe(true)
expect(isFunction(() => { })).toBe(true)
expect(isFunction(function () { })).toBe(true)
})

test('isArray', () => {
expect(isArray(1)).toBe(false)
expect(isArray(1.111)).toBe(false)
expect(isArray(NaN)).toBe(false)
expect(isArray('2024')).toBe(false)
expect(isArray(`2024`)).toBe(false)
expect(isArray(false)).toBe(false)
expect(isArray(true)).toBe(false)
expect(isArray(null)).toBe(false)
expect(isArray(!null)).toBe(false)
expect(isArray(void 0)).toBe(false)
expect(isArray({})).toBe(false)
expect(isArray(new User)).toBe(false)
expect(isArray([])).toBe(true)
expect(isArray(new Array)).toBe(true)
expect(isArray(new Array)).toBe(true)
expect(isArray(new Set())).toBe(false)
expect(isArray(new Date)).toBe(false)
expect(isArray(Date)).toBe(false)
expect(isArray(() => { })).toBe(false)
expect(isArray(function () { })).toBe(false)
})

test('isTrue', () => {
expect(isTrue(1)).toBe(true)
expect(isTrue(1.111)).toBe(true)
expect(isTrue(NaN)).toBe(false)
expect(isTrue('2024')).toBe(true)
expect(isTrue(`2024`)).toBe(true)
expect(isTrue(false)).toBe(true)
expect(isTrue(true)).toBe(true)
expect(isTrue(null)).toBe(false)
expect(isTrue(!null)).toBe(true)
expect(isTrue(void 0)).toBe(false)
expect(isTrue({})).toBe(true)
expect(isTrue(new User)).toBe(true)
expect(isTrue([])).toBe(true)
expect(isTrue(new Array)).toBe(true)
expect(isTrue(new Array)).toBe(true)
expect(isTrue(new Set())).toBe(true)
expect(isTrue(new Date)).toBe(true)
expect(isTrue(Date)).toBe(true)
expect(isTrue(() => { })).toBe(true)
expect(isTrue(function () { })).toBe(true)
})
19 changes: 16 additions & 3 deletions packages/fighting-design/_utils/is/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ export const isString = (value: any): value is string => typeof value === 'strin

/**
* 检测一个数据是否为 number 类型
*
* 千万不要使用 isNaN
*
* isNaN({}) -> true
* Number.isNaN({}) -> false
*
* @param { * } value 要检测的数据
* @returns { boolean }
* @see isNaN() https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/isNaN
* @see Number.isNaN() https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
*/
export const isNumber = (value: any): value is number =>
typeof value === 'number' && !isNaN(value)
typeof value === 'number' && !Number.isNaN(value)

/**
* 检测一个数据是否为 boolean 类型
Expand Down Expand Up @@ -66,12 +71,20 @@ export const isBrowser: boolean = typeof window !== 'undefined'

/**
* 是否为真值
*
* 这里的规则只是按断是非不为 NaN、undefined、null 的值都为真
*
* 千万不要使用 isNaN
*
* isNaN({}) -> true
* Number.isNaN({}) -> false
*
* @see Number.isNaN() https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
* @param { * } value 检测的值
* @returns { boolean }
*/
export const isTrue = (value: any): boolean => {
if (isNaN(value) || value === void 0 || value === null) {
if (Number.isNaN(value) || value === void 0 || value === null) {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion packages/fighting-design/avatar/src/avatar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/** 获取到父组件注入的依赖项 */
const parentInject: AvatarGroupProps | undefined = inject(
AVATAR_GROUP_PROPS_KEY,
undefined
void 0
)
const { isSuccess, isShowNode } = useLoadImg(
Expand Down

0 comments on commit 6b50518

Please sign in to comment.