|
| 1 | +/** |
| 2 | + * This file is part of helpers4. |
| 3 | + * Copyright (C) 2025 baxyz |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect, it } from 'vitest'; |
| 8 | +import { isEmpty } from './isEmpty'; |
| 9 | + |
| 10 | +describe('isEmpty', () => { |
| 11 | + it('should treat null and undefined as empty', () => { |
| 12 | + expect(isEmpty(null)).toBe(true); |
| 13 | + expect(isEmpty(undefined)).toBe(true); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should handle strings', () => { |
| 17 | + expect(isEmpty('')).toBe(true); |
| 18 | + expect(isEmpty(' ')).toBe(false); |
| 19 | + expect(isEmpty('text')).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should handle arrays', () => { |
| 23 | + expect(isEmpty([])).toBe(true); |
| 24 | + expect(isEmpty([1])).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should handle plain objects', () => { |
| 28 | + expect(isEmpty({})).toBe(true); |
| 29 | + expect(isEmpty({ a: 1 })).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should handle objects with null prototype', () => { |
| 33 | + const obj = Object.create(null) as Record<string, unknown>; |
| 34 | + expect(isEmpty(obj)).toBe(true); |
| 35 | + obj.key = 'value'; |
| 36 | + expect(isEmpty(obj)).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should handle Map and Set', () => { |
| 40 | + expect(isEmpty(new Map())).toBe(true); |
| 41 | + expect(isEmpty(new Set())).toBe(true); |
| 42 | + expect(isEmpty(new Map([['key', 'value']]))).toBe(false); |
| 43 | + expect(isEmpty(new Set([1]))).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return false for special objects', () => { |
| 47 | + expect(isEmpty(new Date())).toBe(false); |
| 48 | + class Example {} |
| 49 | + expect(isEmpty(new Example())).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return false for numbers, booleans and functions', () => { |
| 53 | + expect(isEmpty(0)).toBe(false); |
| 54 | + expect(isEmpty(false)).toBe(false); |
| 55 | + expect(isEmpty(() => undefined)).toBe(false); |
| 56 | + }); |
| 57 | +}); |
0 commit comments