From 6d396b7211ced3f51858b3c4097542d0abcd2a18 Mon Sep 17 00:00:00 2001 From: chouchouji <1305974212@qq.com> Date: Mon, 28 Oct 2024 13:16:35 +0800 Subject: [PATCH 1/3] test: add general test cases --- tests/array.spec.ts | 2 +- tests/general.spec.ts | 220 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 220 insertions(+), 2 deletions(-) diff --git a/tests/array.spec.ts b/tests/array.spec.ts index f96bc74..2bb78b5 100644 --- a/tests/array.spec.ts +++ b/tests/array.spec.ts @@ -12,7 +12,7 @@ import { shuffle, sum, sumBy, -} from '../src' +} from '../src/array' it('uniq', () => { const arr = uniq([1, 2, 2, 3, 4, 4]) diff --git a/tests/general.spec.ts b/tests/general.spec.ts index 1618159..1b7b636 100644 --- a/tests/general.spec.ts +++ b/tests/general.spec.ts @@ -1,7 +1,225 @@ import { it, expect } from 'vitest' -import { isNonEmptyArray } from '../src' +import { + isNonEmptyArray, + isString, + isBoolean, + isNumber, + toTypeString, + isDate, + isMap, + isSet, + isRegExp, + isSymbol, + isNumeric, + isPlainObject, + isObject, + isPromise, + isArray, + isEmpty, + isFunction, + isNullish, + isURL, + isWindow, + isTruthy, + toRawType, + hasOwn, +} from '../src/general' it('isNonEmptyArray', () => { expect(isNonEmptyArray([])).toBe(false) + expect(isNonEmptyArray([1, 2])).toBe(true) }) + +it('toTypeString', () => { + expect(toTypeString([])).toBe('[object Array]') + expect(toTypeString({})).toBe('[object Object]') + expect(toTypeString(1)).toBe('[object Number]') + expect(toTypeString('')).toBe('[object String]') + expect(toTypeString(false)).toBe('[object Boolean]') + expect(toTypeString(null)).toBe('[object Null]') + expect(toTypeString(undefined)).toBe('[object Undefined]') +}) + +it('toRawType', () => { + expect(toRawType([])).toBe('Array') + expect(toRawType({})).toBe('Object') + expect(toRawType(1)).toBe('Number') + expect(toRawType('')).toBe('String') + expect(toRawType(false)).toBe('Boolean') + expect(toRawType(null)).toBe('Null') + expect(toRawType(undefined)).toBe('Undefined') +}) + +it('isSymbol', () => { + expect(isSymbol(Symbol('test'))).toBe(true) +}) + +it('isDate', () => { + expect(isDate(new Date())).toBe(true) +}) + +it('isMap', () => { + expect(isMap(new Map())).toBe(true) +}) + +it('isSet', () => { + expect(isSet(new Set())).toBe(true) +}) + +it('isRegExp', () => { + expect(isRegExp(/test/g)).toBe(true) +}) + +it('isString', () => { + expect(isString([])).toBe(false) + expect(isString(false)).toBe(false) + expect(isString(1)).toBe(false) + expect(isString(null)).toBe(false) + expect(isString(undefined)).toBe(false) + expect(isString({})).toBe(false) + + expect(isString('')).toBe(true) +}) + +it('isBoolean', () => { + expect(isBoolean([])).toBe(false) + expect(isBoolean(1)).toBe(false) + expect(isBoolean(null)).toBe(false) + expect(isBoolean(undefined)).toBe(false) + expect(isBoolean({})).toBe(false) + expect(isBoolean('')).toBe(false) + + expect(isBoolean(false)).toBe(true) + expect(isBoolean(true)).toBe(true) +}) + +it('isNumber', () => { + expect(isNumber([])).toBe(false) + expect(isNumber(false)).toBe(false) + expect(isNumber(null)).toBe(false) + expect(isNumber(undefined)).toBe(false) + expect(isNumber({})).toBe(false) + expect(isNumber('')).toBe(false) + + expect(isNumber(1)).toBe(true) + expect(isNumber(NaN)).toBe(true) +}) + +it('isNumeric', () => { + expect(isNumeric(123)).toBe(true) + expect(isNumeric(-123)).toBe(true) + expect(isNumeric(0)).toBe(true) + + expect(isNumeric('123')).toBe(true) + expect(isNumeric('-123')).toBe(true) + expect(isNumeric('+123')).toBe(true) + + expect(isNumeric('123.45')).toBe(false) + expect(isNumeric('abc')).toBe(false) + expect(isNumeric(null)).toBe(false) + expect(isNumeric(undefined)).toBe(false) + expect(isNumeric([])).toBe(false) + expect(isNumeric({})).toBe(false) +}) + +it('isPlainObject', () => { + expect(isPlainObject([])).toBe(false) + expect(isPlainObject(null)).toBe(false) + expect(isPlainObject(undefined)).toBe(false) + expect(isPlainObject(123)).toBe(false) + expect(isPlainObject('123')).toBe(false) + expect(isPlainObject(() => {})).toBe(false) + expect(isPlainObject({})).toBe(true) + expect(isPlainObject({ key: 'value' })).toBe(true) +}) + +it('isObject', () => { + expect(isObject({})).toBe(true) + expect(isObject([])).toBe(true) + expect(isObject(null)).toBe(false) + + expect(isObject(123)).toBe(false) + expect(isObject('abc')).toBe(false) + expect(isObject(undefined)).toBe(false) + expect(isObject(() => {})).toBe(false) +}) + +it('isPromise', () => { + expect(isPromise(Promise.resolve())).toBe(true) + expect(isPromise({ then: () => {}, catch: () => {} })).toBe(true) + expect(isPromise({ then: () => {} })).toBe(false) + expect(isPromise({ catch: () => {} })).toBe(false) + expect(isPromise(null)).toBe(false) + expect(isPromise({})).toBe(false) + expect(isPromise([])).toBe(false) + expect(isPromise('Promise')).toBe(false) + expect(isPromise(123)).toBe(false) +}) + +it('isFunction', () => { + expect(isFunction(() => {})).toBe(true) + expect(isFunction(123)).toBe(false) + expect(isFunction('function')).toBe(false) + expect(isFunction({})).toBe(false) +}) + +it('isArray', () => { + expect(isArray([1, 2, 3])).toBe(true) + expect(isArray([])).toBe(true) + expect(isArray('Array')).toBe(false) + expect(isArray({})).toBe(false) + expect(isArray(null)).toBe(false) +}) + +it('isNullish', () => { + expect(isNullish(null)).toBe(true) + expect(isNullish(undefined)).toBe(true) + expect(isNullish(0)).toBe(false) + expect(isNullish('')).toBe(false) + expect(isNullish(false)).toBe(false) +}) + +it('isTruthy', () => { + expect(isTruthy(1)).toBe(true) + expect(isTruthy(true)).toBe(true) + expect(isTruthy('string')).toBe(true) + expect(isTruthy(0)).toBe(false) + expect(isTruthy(null)).toBe(false) + expect(isTruthy(undefined)).toBe(false) + expect(isTruthy('')).toBe(false) +}) + +it('isURL', () => { + expect(isURL('http://example.com')).toBe(true) + expect(isURL('https://example.com')).toBe(true) + expect(isURL('/relative/path')).toBe(true) + expect(isURL('relative/path')).toBe(true) + expect(isURL('example.com')).toBe(false) + expect(isURL('')).toBe(false) + expect(isURL(null)).toBe(false) + expect(isURL(undefined)).toBe(false) +}) + +it('isEmpty', () => { + expect(isEmpty(undefined)).toBe(true) + expect(isEmpty(null)).toBe(true) + expect(isEmpty('')).toBe(true) + expect(isEmpty([])).toBe(true) + expect(isEmpty([1, 2, 3])).toBe(false) + expect(isEmpty('non-empty string')).toBe(false) + expect(isEmpty(0)).toBe(false) + expect(isEmpty({})).toBe(false) +}) + +it('isWindow', () => { + expect(isWindow(window)).toBe(true) + expect(isWindow({})).toBe(false) + expect(isWindow(undefined)).toBe(false) +}) + +it('hasOwn', () => { + expect(hasOwn({ foo: 1 }, 'foo')).toBe(true) + expect(hasOwn({ foo: 1 }, 'bar')).toBe(false) + expect(hasOwn(Object.create({ foo: 1 }), 'foo')).toBe(false) +}) From 50ec701ad065e498fb797a9f796d3e5dc287497a Mon Sep 17 00:00:00 2001 From: chouchouji <1305974212@qq.com> Date: Mon, 28 Oct 2024 14:12:53 +0800 Subject: [PATCH 2/3] refactor: optimize test file code --- tests/file.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/file.spec.ts b/tests/file.spec.ts index 062f296..7eaed45 100644 --- a/tests/file.spec.ts +++ b/tests/file.spec.ts @@ -1,5 +1,5 @@ import { describe, it, expect, beforeEach } from 'vitest'; -import { toDataURL, toText, toArrayBuffer } from '../src'; +import { toDataURL, toText, toArrayBuffer } from '../src/file'; describe('file', () => { let file: File; From 34cc49960a0b9ebe9b5e2d0d5d1264a142e399c4 Mon Sep 17 00:00:00 2001 From: chouchouji <1305974212@qq.com> Date: Mon, 28 Oct 2024 14:48:10 +0800 Subject: [PATCH 3/3] test: update import path --- tests/array.spec.ts | 2 +- tests/file.spec.ts | 44 +++++++++++++++++++++---------------------- tests/general.spec.ts | 2 +- tests/json.spec.ts | 2 +- tests/number.spec.ts | 2 +- tests/storage.spec.ts | 4 ++-- tests/string.spec.ts | 2 +- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/tests/array.spec.ts b/tests/array.spec.ts index 2bb78b5..f96bc74 100644 --- a/tests/array.spec.ts +++ b/tests/array.spec.ts @@ -12,7 +12,7 @@ import { shuffle, sum, sumBy, -} from '../src/array' +} from '../src' it('uniq', () => { const arr = uniq([1, 2, 2, 3, 4, 4]) diff --git a/tests/file.spec.ts b/tests/file.spec.ts index 7eaed45..46ae850 100644 --- a/tests/file.spec.ts +++ b/tests/file.spec.ts @@ -1,36 +1,36 @@ -import { describe, it, expect, beforeEach } from 'vitest'; -import { toDataURL, toText, toArrayBuffer } from '../src/file'; +import { describe, it, expect, beforeEach } from 'vitest' +import { toDataURL, toText, toArrayBuffer } from '../src' describe('file', () => { - let file: File; + let file: File beforeEach(() => { - const blob = new Blob(['Hello, World!'], { type: 'text/plain' }); - file = new File([blob], 'hello.txt', { type: 'text/plain' }); - }); + const blob = new Blob(['Hello, World!'], { type: 'text/plain' }) + file = new File([blob], 'hello.txt', { type: 'text/plain' }) + }) describe('toDataURL', () => { it('should convert file to data URL', async () => { - const dataURL = await toDataURL(file); - expect(dataURL).toMatch(/^data:text\/plain;base64,/); - }); - }); + const dataURL = await toDataURL(file) + expect(dataURL).toMatch(/^data:text\/plain;base64,/) + }) + }) describe('toText', () => { it('should read file as text', async () => { - const text = await toText(file); - expect(text).toBe('Hello, World!'); - }); - }); + const text = await toText(file) + expect(text).toBe('Hello, World!') + }) + }) describe('toArrayBuffer', () => { it('should read file as array buffer', async () => { - const arrayBuffer = await toArrayBuffer(file); - expect(arrayBuffer).toBeInstanceOf(ArrayBuffer); + const arrayBuffer = await toArrayBuffer(file) + expect(arrayBuffer).toBeInstanceOf(ArrayBuffer) - const uint8Array = new Uint8Array(arrayBuffer); - const text = new TextDecoder().decode(uint8Array); - expect(text).toBe('Hello, World!'); - }); - }); -}); \ No newline at end of file + const uint8Array = new Uint8Array(arrayBuffer) + const text = new TextDecoder().decode(uint8Array) + expect(text).toBe('Hello, World!') + }) + }) +}) diff --git a/tests/general.spec.ts b/tests/general.spec.ts index 1b7b636..2ee07f0 100644 --- a/tests/general.spec.ts +++ b/tests/general.spec.ts @@ -23,7 +23,7 @@ import { isTruthy, toRawType, hasOwn, -} from '../src/general' +} from '../src' it('isNonEmptyArray', () => { expect(isNonEmptyArray([])).toBe(false) diff --git a/tests/json.spec.ts b/tests/json.spec.ts index c7bbd39..8c1a4eb 100644 --- a/tests/json.spec.ts +++ b/tests/json.spec.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest' -import { tryParseJSON, prettyJSONObject } from '../src/json' +import { tryParseJSON, prettyJSONObject } from '../src' describe('JSON utility functions', () => { it('should parse valid JSON strings', () => { diff --git a/tests/number.spec.ts b/tests/number.spec.ts index b7771e2..e884a92 100644 --- a/tests/number.spec.ts +++ b/tests/number.spec.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest' -import { toNumber, clamp, clampArrayRange, genNumberKey, randomNumber } from '../src/number' +import { toNumber, clamp, clampArrayRange, genNumberKey, randomNumber } from '../src' describe('Number utility functions', () => { it('should convert various types to number', () => { diff --git a/tests/storage.spec.ts b/tests/storage.spec.ts index 3dc7143..1b974f0 100644 --- a/tests/storage.spec.ts +++ b/tests/storage.spec.ts @@ -1,5 +1,5 @@ import { describe, it, expect, beforeEach, vi } from 'vitest' -import { createStorage, sessionStorage, localStorage } from '../src/storage' +import { createStorage, sessionStorage, localStorage } from '../src' describe('Storage utility functions', () => { let mockStorage: Storage @@ -9,7 +9,7 @@ describe('Storage utility functions', () => { mockStorage = { length: 0, clear: vi.fn(() => { - Object.keys(store).forEach(key => { + Object.keys(store).forEach((key) => { delete store[key] }) }), diff --git a/tests/string.spec.ts b/tests/string.spec.ts index 1e46445..0bb11f6 100644 --- a/tests/string.spec.ts +++ b/tests/string.spec.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest' -import { pascalCase, camelize, kebabCase, slash, genStringKey, capitalizeFirstLetter } from '../src/string' +import { pascalCase, camelize, kebabCase, slash, genStringKey, capitalizeFirstLetter } from '../src' describe('string utility functions', () => { it('should convert string to pascal case', () => {