|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { clone } from '../src/clone'; |
| 3 | + |
| 4 | +describe('clone tests', () => { |
| 5 | + describe('primitives', () => { |
| 6 | + it('should return primitives as-is', () => { |
| 7 | + expect(clone(42)).toBe(42); |
| 8 | + expect(clone('hello')).toBe('hello'); |
| 9 | + expect(clone(true)).toBe(true); |
| 10 | + expect(clone(false)).toBe(false); |
| 11 | + expect(clone(null)).toBe(null); |
| 12 | + expect(clone(undefined)).toBe(undefined); |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('arrays', () => { |
| 17 | + it('should clone simple arrays', () => { |
| 18 | + const arr = [1, 2, 3]; |
| 19 | + const cloned = clone(arr); |
| 20 | + expect(cloned).toEqual(arr); |
| 21 | + expect(cloned).not.toBe(arr); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should deep clone nested arrays', () => { |
| 25 | + const arr = [1, [2, 3], [4, [5, 6]]]; |
| 26 | + const cloned = clone(arr); |
| 27 | + expect(cloned).toEqual(arr); |
| 28 | + expect(cloned).not.toBe(arr); |
| 29 | + expect(cloned[1]).not.toBe(arr[1]); |
| 30 | + expect(cloned[2]).not.toBe(arr[2]); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should clone arrays with objects', () => { |
| 34 | + const arr = [{ a: 1 }, { b: 2 }]; |
| 35 | + const cloned = clone(arr); |
| 36 | + expect(cloned).toEqual(arr); |
| 37 | + expect(cloned).not.toBe(arr); |
| 38 | + expect(cloned[0]).not.toBe(arr[0]); |
| 39 | + expect(cloned[1]).not.toBe(arr[1]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle empty arrays', () => { |
| 43 | + const arr: number[] = []; |
| 44 | + const cloned = clone(arr); |
| 45 | + expect(cloned).toEqual([]); |
| 46 | + expect(cloned).not.toBe(arr); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('plain objects', () => { |
| 51 | + it('should clone simple objects', () => { |
| 52 | + const obj = { a: 1, b: 2 }; |
| 53 | + const cloned = clone(obj); |
| 54 | + expect(cloned).toEqual(obj); |
| 55 | + expect(cloned).not.toBe(obj); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should deep clone nested objects', () => { |
| 59 | + const obj = { a: 1, b: { c: 2, d: { e: 3 } } }; |
| 60 | + const cloned = clone(obj); |
| 61 | + expect(cloned).toEqual(obj); |
| 62 | + expect(cloned).not.toBe(obj); |
| 63 | + expect(cloned.b).not.toBe(obj.b); |
| 64 | + expect(cloned.b.d).not.toBe(obj.b.d); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should clone objects with arrays', () => { |
| 68 | + const obj = { a: [1, 2], b: { c: [3, 4] } }; |
| 69 | + const cloned = clone(obj); |
| 70 | + expect(cloned).toEqual(obj); |
| 71 | + expect(cloned).not.toBe(obj); |
| 72 | + expect(cloned.a).not.toBe(obj.a); |
| 73 | + expect(cloned.b.c).not.toBe(obj.b.c); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle empty objects', () => { |
| 77 | + const obj = {}; |
| 78 | + const cloned = clone(obj); |
| 79 | + expect(cloned).toEqual({}); |
| 80 | + expect(cloned).not.toBe(obj); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle objects with null prototype', () => { |
| 84 | + const obj = Object.create(null); |
| 85 | + obj.foo = 'bar'; |
| 86 | + const cloned = clone(obj); |
| 87 | + expect(cloned).toEqual(obj); |
| 88 | + expect(cloned).not.toBe(obj); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('non-plain objects', () => { |
| 93 | + it('should return Date objects as-is', () => { |
| 94 | + const date = new Date(); |
| 95 | + const cloned = clone(date); |
| 96 | + expect(cloned).toBe(date); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should return RegExp objects as-is', () => { |
| 100 | + const regex = /test/gi; |
| 101 | + const cloned = clone(regex); |
| 102 | + expect(cloned).toBe(regex); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should return class instances as-is', () => { |
| 106 | + class MyClass { |
| 107 | + value = 42; |
| 108 | + } |
| 109 | + const instance = new MyClass(); |
| 110 | + const cloned = clone(instance); |
| 111 | + expect(cloned).toBe(instance); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should return functions as-is', () => { |
| 115 | + const fn = () => 42; |
| 116 | + const cloned = clone(fn); |
| 117 | + expect(cloned).toBe(fn); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('mixed structures', () => { |
| 122 | + it('should handle complex mixed structures', () => { |
| 123 | + const complex = { |
| 124 | + number: 42, |
| 125 | + string: 'hello', |
| 126 | + bool: true, |
| 127 | + null: null, |
| 128 | + array: [1, 2, { nested: 'value' }], |
| 129 | + object: { |
| 130 | + a: [1, 2, 3], |
| 131 | + b: { c: 4 }, |
| 132 | + }, |
| 133 | + }; |
| 134 | + const cloned = clone(complex); |
| 135 | + expect(cloned).toEqual(complex); |
| 136 | + expect(cloned).not.toBe(complex); |
| 137 | + expect(cloned.array).not.toBe(complex.array); |
| 138 | + expect(cloned.array[2]).not.toBe(complex.array[2]); |
| 139 | + expect(cloned.object).not.toBe(complex.object); |
| 140 | + expect(cloned.object.a).not.toBe(complex.object.a); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should preserve primitive values in nested structures', () => { |
| 144 | + const obj = { a: { b: { c: 42 } } }; |
| 145 | + const cloned = clone(obj); |
| 146 | + expect(cloned.a.b.c).toBe(42); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments