Skip to content

Commit

Permalink
test: add general test cases (#6)
Browse files Browse the repository at this point in the history
* test: add general test cases

* refactor: optimize test file code

* test: update import path
  • Loading branch information
chouchouji authored Oct 28, 2024
1 parent 9551a3d commit 65aefe5
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 28 deletions.
44 changes: 22 additions & 22 deletions tests/file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { toDataURL, toText, toArrayBuffer } from '../src';
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!');
});
});
});
const uint8Array = new Uint8Array(arrayBuffer)
const text = new TextDecoder().decode(uint8Array)
expect(text).toBe('Hello, World!')
})
})
})
220 changes: 219 additions & 1 deletion tests/general.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

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)
})
2 changes: 1 addition & 1 deletion tests/json.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/number.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/storage.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
})
}),
Expand Down
2 changes: 1 addition & 1 deletion tests/string.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down

0 comments on commit 65aefe5

Please sign in to comment.