File size: 2,257 Bytes
9ada4bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { it, expect } from 'vitest'
import { cloneObject } from './cloneObject'

it('clones a shallow object', () => {
  const original = { a: 1, b: 2, c: [1, 2, 3] }
  const clone = cloneObject(original)

  expect(clone).toEqual(original)

  clone.a = 5
  clone.b = 6
  clone.c = [5, 6, 7]

  expect(clone).toHaveProperty('a', 5)
  expect(clone).toHaveProperty('b', 6)
  expect(clone).toHaveProperty('c', [5, 6, 7])
  expect(original).toHaveProperty('a', 1)
  expect(original).toHaveProperty('b', 2)
  expect(original).toHaveProperty('c', [1, 2, 3])
})

it('clones a nested object', () => {
  const original = { a: { b: 1 }, c: { d: { e: 2 } } }
  const clone = cloneObject(original)

  expect(clone).toEqual(original)

  clone.a.b = 10
  clone.c.d.e = 20

  expect(clone).toHaveProperty(['a', 'b'], 10)
  expect(clone).toHaveProperty(['c', 'd', 'e'], 20)
  expect(original).toHaveProperty(['a', 'b'], 1)
  expect(original).toHaveProperty(['c', 'd', 'e'], 2)
})

it('clones a class instance', () => {
  class Car {
    public manufacturer: string
    constructor() {
      this.manufacturer = 'Audi'
    }
    getManufacturer() {
      return this.manufacturer
    }
  }

  const car = new Car()
  const clone = cloneObject(car)

  expect(clone).toHaveProperty('manufacturer', 'Audi')
  expect(clone).toHaveProperty('getManufacturer')
  expect(clone.getManufacturer).toBeInstanceOf(Function)
  expect(clone.getManufacturer()).toEqual('Audi')
})

it('ignores nested class instances', () => {
  class Car {
    name: string
    constructor(name: string) {
      this.name = name
    }
    getName() {
      return this.name
    }
  }
  const original = {
    a: 1,
    car: new Car('Audi'),
  }
  const clone = cloneObject(original)

  expect(clone).toEqual(original)
  expect(clone.car).toBeInstanceOf(Car)
  expect(clone.car.getName()).toEqual('Audi')

  clone.car = new Car('BMW')

  expect(clone.car).toBeInstanceOf(Car)
  expect(clone.car.getName()).toEqual('BMW')
  expect(original.car).toBeInstanceOf(Car)
  expect(original.car.getName()).toEqual('Audi')
})

it('clones an object with null prototype', () => {
  const original = {
    key: Object.create(null),
  }
  const clone = cloneObject(original)

  expect(clone).toEqual({
    key: {},
  })
})