|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { isArray, isClassInstance, isClassPrototype, isPlainObject, isString } from '../../src/utils/type-guards'; |
| 3 | + |
| 4 | +class Foo {} |
| 5 | +const fooInstance = new Foo(); |
| 6 | + |
| 7 | +describe('isPlainObject', () => { |
| 8 | + it('should return true for plain object', () => expect(isPlainObject({})).toBe(true)); |
| 9 | + it('should return true for plain object with properties', () => expect(isPlainObject({ a: 1 })).toBe(true)); |
| 10 | + it('should return false for array', () => expect(isPlainObject([])).toBe(false)); |
| 11 | + it('should return false for string', () => expect(isPlainObject('x')).toBe(false)); |
| 12 | + it('should return false for number', () => expect(isPlainObject(42)).toBe(false)); |
| 13 | + it('should return false for boolean', () => expect(isPlainObject(true)).toBe(false)); |
| 14 | + it('should return false for null', () => expect(isPlainObject(null)).toBe(false)); |
| 15 | + it('should return false for undefined', () => expect(isPlainObject(undefined)).toBe(false)); |
| 16 | + it('should return false for class prototype', () => expect(isPlainObject(Foo)).toBe(false)); |
| 17 | + it('should return false for class instance', () => expect(isPlainObject(fooInstance)).toBe(false)); |
| 18 | +}); |
| 19 | + |
| 20 | +describe('isArray', () => { |
| 21 | + it('should return true for empty array', () => expect(isArray([])).toBe(true)); |
| 22 | + it('should return true for non-empty array', () => expect(isArray([1, 2, 3])).toBe(true)); |
| 23 | + it('should return false for plain object', () => expect(isArray({})).toBe(false)); |
| 24 | + it('should return false for string', () => expect(isArray('hello')).toBe(false)); |
| 25 | + it('should return false for number', () => expect(isArray(42)).toBe(false)); |
| 26 | + it('should return false for boolean', () => expect(isArray(true)).toBe(false)); |
| 27 | + it('should return false for null', () => expect(isArray(null)).toBe(false)); |
| 28 | + it('should return false for undefined', () => expect(isArray(undefined)).toBe(false)); |
| 29 | + it('should return false for class prototype', () => expect(isArray(Foo)).toBe(false)); |
| 30 | + it('should return false for class instance', () => expect(isArray(fooInstance)).toBe(false)); |
| 31 | +}); |
| 32 | + |
| 33 | +describe('isClassPrototype', () => { |
| 34 | + it('should return true for class prototype', () => expect(isClassPrototype(Foo)).toBe(true)); |
| 35 | + it('should return false for class instance', () => expect(isClassPrototype(fooInstance)).toBe(false)); |
| 36 | + it('should return false for plain object', () => expect(isClassPrototype({})).toBe(false)); |
| 37 | + it('should return false for array', () => expect(isClassPrototype([])).toBe(false)); |
| 38 | + it('should return false for string', () => expect(isClassPrototype('foo')).toBe(false)); |
| 39 | + it('should return false for number', () => expect(isClassPrototype(42)).toBe(false)); |
| 40 | + it('should return false for boolean', () => expect(isClassPrototype(true)).toBe(false)); |
| 41 | + it('should return false for null', () => expect(isClassPrototype(null)).toBe(false)); |
| 42 | + it('should return false for undefined', () => expect(isClassPrototype(undefined)).toBe(false)); |
| 43 | +}); |
| 44 | + |
| 45 | +describe('isClassInstance', () => { |
| 46 | + it('should return true for class instance', () => expect(isClassInstance(fooInstance)).toBe(true)); |
| 47 | + it('should return false for class prototype', () => expect(isClassInstance(Foo)).toBe(false)); |
| 48 | + it('should return false for plain object', () => expect(isClassInstance({})).toBe(false)); |
| 49 | + it('should return false for array', () => expect(isClassInstance([])).toBe(false)); |
| 50 | + it('should return false for string', () => expect(isClassInstance('foo')).toBe(false)); |
| 51 | + it('should return false for number', () => expect(isClassInstance(42)).toBe(false)); |
| 52 | + it('should return false for boolean', () => expect(isClassInstance(true)).toBe(false)); |
| 53 | + it('should return false for null', () => expect(isClassInstance(null)).toBe(false)); |
| 54 | + it('should return false for undefined', () => expect(isClassInstance(undefined)).toBe(false)); |
| 55 | +}); |
| 56 | + |
| 57 | +describe('isString', () => { |
| 58 | + it('should return true for string', () => expect(isString('hello')).toBe(true)); |
| 59 | + it('should return true for empty string', () => expect(isString('')).toBe(true)); |
| 60 | + it('should return false for plain object', () => expect(isString({})).toBe(false)); |
| 61 | + it('should return false for array', () => expect(isString([])).toBe(false)); |
| 62 | + it('should return false for number', () => expect(isString(42)).toBe(false)); |
| 63 | + it('should return false for boolean', () => expect(isString(true)).toBe(false)); |
| 64 | + it('should return false for null', () => expect(isString(null)).toBe(false)); |
| 65 | + it('should return false for undefined', () => expect(isString(undefined)).toBe(false)); |
| 66 | + it('should return false for class prototype', () => expect(isString(Foo)).toBe(false)); |
| 67 | + it('should return false for class instance', () => expect(isString(fooInstance)).toBe(false)); |
| 68 | +}); |
0 commit comments