| 1 |
import { deepMerge, isObject, sleep } from '@shared/lib/utils'; |
| 2 |
|
| 3 |
describe('isObject', () => { |
| 4 |
it('returns true for plain objects', () => { |
| 5 |
expect(isObject({})).toBe(true); |
| 6 |
expect(isObject({ a: 1 })).toBe(true); |
| 7 |
}); |
| 8 |
|
| 9 |
it('returns false for arrays', () => { |
| 10 |
expect(isObject([])).toBe(false); |
| 11 |
}); |
| 12 |
|
| 13 |
it('returns false for null', () => { |
| 14 |
expect(isObject(null)).toBe(false); |
| 15 |
}); |
| 16 |
|
| 17 |
it('returns false for other types', () => { |
| 18 |
expect(isObject(42)).toBe(false); |
| 19 |
expect(isObject('string')).toBe(false); |
| 20 |
expect(isObject(undefined)).toBe(false); |
| 21 |
}); |
| 22 |
}); |
| 23 |
|
| 24 |
describe('deepMerge', () => { |
| 25 |
it('merges two flat objects', () => { |
| 26 |
const result = deepMerge({ a: 1 }, { b: 2 }); |
| 27 |
expect(result).toEqual({ a: 1, b: 2 }); |
| 28 |
}); |
| 29 |
|
| 30 |
it('merges deeply nested objects', () => { |
| 31 |
const result = deepMerge({ a: { x: 1 }, b: 2 }, { a: { y: 3 }, c: 4 }); |
| 32 |
expect(result).toEqual({ a: { x: 1, y: 3 }, b: 2, c: 4 }); |
| 33 |
}); |
| 34 |
|
| 35 |
it('overwrites non-object values', () => { |
| 36 |
const result = deepMerge({ a: 1 }, { a: 2 }); |
| 37 |
expect(result).toEqual({ a: 2 }); |
| 38 |
}); |
| 39 |
|
| 40 |
it('returns null if any input is not an object', () => { |
| 41 |
expect(deepMerge(null, { a: 1 })).toBeNull(); |
| 42 |
expect(deepMerge({ a: 1 }, 123)).toBeNull(); |
| 43 |
}); |
| 44 |
}); |
| 45 |
|
| 46 |
describe('sleep', () => { |
| 47 |
it('resolves after given time', async () => { |
| 48 |
const start = Date.now(); |
| 49 |
await sleep(50); |
| 50 |
const duration = Date.now() - start; |
| 51 |
// Note: setTimeout can fire slightly early due to timer coalescing, |
| 52 |
// so we allow some buffer in the assertion |
| 53 |
expect(duration).toBeGreaterThanOrEqual(47); |
| 54 |
}); |
| 55 |
}); |
| 56 |
|