|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import { describe, it } from 'node:test'; |
| 5 | + |
| 6 | +import { lazy, isPlainObject, deepMerge } from '../misc.mjs'; |
| 7 | + |
| 8 | +describe('lazy', () => { |
| 9 | + it('should call the function only once and cache the result', () => { |
| 10 | + let callCount = 0; |
| 11 | + const fn = lazy(() => { |
| 12 | + callCount++; |
| 13 | + return 42; |
| 14 | + }); |
| 15 | + |
| 16 | + assert.strictEqual(fn(), 42); |
| 17 | + assert.strictEqual(fn(), 42); |
| 18 | + assert.strictEqual(callCount, 1); |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('isPlainObject', () => { |
| 23 | + it('should return true for plain objects', () => { |
| 24 | + assert.strictEqual(isPlainObject({}), true); |
| 25 | + assert.strictEqual(isPlainObject({ a: 1 }), true); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should return false for arrays', () => { |
| 29 | + assert.strictEqual(isPlainObject([]), false); |
| 30 | + assert.strictEqual(isPlainObject([1, 2]), false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should return false for null and primitives', () => { |
| 34 | + assert.strictEqual(isPlainObject(null), false); |
| 35 | + assert.strictEqual(isPlainObject(undefined), false); |
| 36 | + assert.strictEqual(isPlainObject(42), false); |
| 37 | + assert.strictEqual(isPlainObject('string'), false); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe('deepMerge', () => { |
| 42 | + it('should merge flat objects with source taking precedence over base', () => { |
| 43 | + const result = deepMerge({ a: 1, b: 2 }, { b: 10, c: 3 }); |
| 44 | + assert.deepStrictEqual(result, { a: 1, b: 2, c: 3 }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should merge nested objects recursively', () => { |
| 48 | + const result = deepMerge({ nested: { a: 1 } }, { nested: { b: 2 } }); |
| 49 | + assert.deepStrictEqual(result, { nested: { a: 1, b: 2 } }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should use base values when source values are undefined', () => { |
| 53 | + const result = deepMerge({ a: undefined }, { a: 'base' }); |
| 54 | + assert.deepStrictEqual(result, { a: 'base' }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments