File size: 4,288 Bytes
1e92f2d |
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
import cloneObject from '../../utils/cloneObject';
import noop from '../../utils/noop';
describe('clone', () => {
it('should clone object and not mutate the original object', () => {
const fileData = new File([''], 'filename');
const data: Record<string, any> = {
items: [],
test: {
date: new Date('2020-10-15'),
test0: 12,
test1: '12',
test2: [1, 2, 3, 4],
deep: {
date: new Date('2020-10-15'),
test0: 12,
test1: '12',
test2: [
1,
2,
3,
4,
{
file: fileData,
},
],
file: fileData,
},
},
file: fileData,
test2: new Set([1, 2]),
test1: new Map([
[1, 'one'],
[2, 'two'],
[3, 'three'],
]),
};
const copy = cloneObject(data);
expect(cloneObject(data)).toEqual(copy);
copy.test.what = '1243';
copy.test.date = new Date('2020-10-16');
copy.items[0] = 2;
expect(data).toEqual({
items: [],
test: {
date: new Date('2020-10-15'),
test0: 12,
test1: '12',
test2: [1, 2, 3, 4],
deep: {
date: new Date('2020-10-15'),
test0: 12,
test1: '12',
test2: [
1,
2,
3,
4,
{
file: fileData,
},
],
file: fileData,
},
},
file: fileData,
test2: new Set([1, 2]),
test1: new Map([
[1, 'one'],
[2, 'two'],
[3, 'three'],
]),
});
data.items = [1, 2, 3];
expect(copy.items).toEqual([2]);
});
it('should skip clone if a node is instance of function', () => {
const data = {
test: {
testFunction: noop,
test: 'inner-string',
deep: {
testFunction: noop,
test: 'deep-string',
},
},
testFunction: noop,
other: 'string',
};
const copy = cloneObject(data);
data.test.deep.test = 'changed-deep-string';
expect(copy).toEqual({
test: {
test: 'inner-string',
deep: {
testFunction: noop,
test: 'deep-string',
},
testFunction: noop,
},
testFunction: noop,
other: 'string',
});
});
it('should skip clone if a node is not planeObject', () => {
class Foo {
a = 1;
b = 1;
static c = function () {};
}
const object = new Foo();
const copy = cloneObject(object);
expect(copy).toBe(object);
});
describe('FileList not defined', () => {
const fileList = globalThis.FileList;
beforeAll(() => {
// @ts-expect-error we want to test that clone skips if FileList is not defined.
delete globalThis.FileList;
});
afterAll(() => {
globalThis.FileList = fileList;
});
it('should skip clone if FileList is not defined', () => {
const data = {
a: 1,
b: 2,
};
const copy = cloneObject(data);
expect(copy).toEqual(data);
});
});
describe('in presence of Array polyfills', () => {
beforeAll(() => {
// @ts-expect-error we want to test that clone skips polyfill
Array.prototype.somePolyfill = () => 123;
});
it('should skip polyfills while cloning', () => {
const data = [1];
const copy = cloneObject(data);
expect(Object.hasOwn(copy, 'somePolyfill')).toBe(false);
});
afterAll(() => {
// @ts-expect-error we want to test that clone skips polyfill
delete Array.prototype.somePolyfill;
});
});
it('should not override prototype of nested object', () => {
const UtcProto = {
_tag: 'Utc',
};
const formValues = {
dateTime: Object.create(UtcProto),
};
const copy = cloneObject(formValues);
expect(Object.getPrototypeOf(copy.dateTime)).toEqual(UtcProto);
expect(copy.dateTime._tag).toBe('Utc');
});
it('should not override prototype of nested object', () => {
const UtcProto = {
_tag: 'Utc',
};
const dateTime = Object.create(UtcProto);
const copy = cloneObject(dateTime);
expect(copy._tag).toBe('Utc');
});
});
|