2021-05-30 19:18:37 +03:00
|
|
|
import clone from '@/utils/clone'
|
2020-10-26 00:07:23 +03:00
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
class Sample {
|
|
|
|
constructor() {
|
|
|
|
this.fieldA = 'fieldA'
|
|
|
|
this.fieldB = 'fieldB'
|
|
|
|
}
|
2020-10-26 00:07:23 +03:00
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
doSomething () {}
|
|
|
|
}
|
2020-10-26 00:07:23 +03:00
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
describe('clone', () => {
|
|
|
|
test.each([
|
|
|
|
[{ a: 123, b: 'hello' }],
|
|
|
|
[{ a: 123, b: { c: 'hello-world' } }],
|
|
|
|
[{
|
|
|
|
id: 123,
|
|
|
|
addresses: [{
|
|
|
|
street: 'Baker Street',
|
|
|
|
building: '221b',
|
|
|
|
}],
|
|
|
|
}],
|
|
|
|
])('recreates object, preserving its structure', state => {
|
|
|
|
expect(clone(state)).toEqual(state)
|
|
|
|
expect(clone({ ref: state }).ref === state).toBe(false)
|
2020-10-26 00:07:23 +03:00
|
|
|
})
|
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
test('retains array structures inside of a pojo', () => {
|
2021-05-28 21:14:09 +03:00
|
|
|
const obj = { a: 'abc', d: ['first', 'second'] }
|
2020-10-26 00:07:23 +03:00
|
|
|
expect(Array.isArray(clone(obj).d)).toBe(true)
|
|
|
|
})
|
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
test('removes references inside array structures', () => {
|
2021-05-28 21:14:09 +03:00
|
|
|
const obj = { a: 'abc', d: ['first', { foo: 'bar' }] }
|
2020-10-26 00:07:23 +03:00
|
|
|
expect(clone(obj).d[1] === obj.d[1]).toBe(false)
|
|
|
|
})
|
2021-05-28 21:14:09 +03:00
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
test('creates a copy of a date', () => {
|
|
|
|
const date = new Date()
|
|
|
|
const copy = clone(date)
|
|
|
|
|
|
|
|
expect(date === copy).toBeFalsy()
|
|
|
|
expect(copy.toISOString()).toStrictEqual(date.toISOString())
|
|
|
|
})
|
|
|
|
|
|
|
|
test('creates a copy of a nested date', () => {
|
|
|
|
const date = new Date()
|
|
|
|
const copy = clone({ date })
|
2021-05-28 21:14:09 +03:00
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
expect(date === copy.date).toBeFalsy()
|
|
|
|
expect(copy.date.toISOString()).toStrictEqual(date.toISOString())
|
|
|
|
})
|
2021-05-28 21:14:09 +03:00
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
test('creates a copy of a class instance', () => {
|
2021-05-28 21:14:09 +03:00
|
|
|
const sample = new Sample()
|
2021-05-30 19:18:37 +03:00
|
|
|
const copy = clone(sample)
|
2021-05-28 21:14:09 +03:00
|
|
|
|
|
|
|
expect(sample === copy).toBeFalsy()
|
|
|
|
|
|
|
|
expect(copy).toBeInstanceOf(Sample)
|
|
|
|
expect(copy.fieldA).toEqual('fieldA')
|
|
|
|
expect(copy.fieldB).toEqual('fieldB')
|
|
|
|
expect(copy.doSomething).toBeTruthy()
|
|
|
|
expect(copy.doSomething).not.toThrow()
|
|
|
|
})
|
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
test('creates a copy of a nested class instance', () => {
|
|
|
|
const sample = new Sample()
|
|
|
|
const copy = clone({ sample })
|
2021-05-28 21:14:09 +03:00
|
|
|
|
2021-05-30 19:18:37 +03:00
|
|
|
expect(sample === copy.sample).toBeFalsy()
|
|
|
|
|
|
|
|
expect(copy.sample).toBeInstanceOf(Sample)
|
|
|
|
expect(copy.sample.fieldA).toEqual('fieldA')
|
|
|
|
expect(copy.sample.fieldB).toEqual('fieldB')
|
|
|
|
expect(copy.sample.doSomething).toBeTruthy()
|
|
|
|
expect(copy.sample.doSomething).not.toThrow()
|
2021-05-28 21:14:09 +03:00
|
|
|
})
|
2021-10-21 12:45:43 +03:00
|
|
|
|
|
|
|
test('does not create a copy of a blob', () => {
|
|
|
|
const blob = new Blob(['{"fieldA": "fieldA"}'], { type : 'application/json' })
|
|
|
|
const copy = clone(blob)
|
|
|
|
|
|
|
|
expect(blob === copy).toBeTruthy()
|
|
|
|
})
|
2021-05-28 21:14:09 +03:00
|
|
|
})
|