Syntax previewClick to edit
1234
export default function shallowClone(value) {
throw new Error('Not implemented');
}
Syntax previewClick to edit
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
import shallowClone from './file';
describe('shallowClone', () => {
test('clones arrays shallowly', () => {
const nested = { x: 2 };
const arr = [1, nested];
const copy = shallowClone(arr);
expect(copy).toEqual(arr);
expect(copy === arr).toBe(false);
expect(copy[1]).toBe(nested);
});
test('clones objects shallowly', () => {
const nested = { c: 2 };
const obj = { a: 1, b: nested };
const copy = shallowClone(obj);
expect(copy).toEqual(obj);
expect(copy === obj).toBe(false);
expect(copy.b).toBe(nested);
});
test('returns a different array reference', () => {
const arr = [1, 2, 3];
const copy = shallowClone(arr);
expect(copy).toEqual(arr);
expect(copy === arr).toBe(false);
});
test('returns a different object reference', () => {
const obj = { a: 1, b: 2 };
const copy = shallowClone(obj);
expect(copy).toEqual(obj);
expect(copy === obj).toBe(false);
});
test('preserves nested object references', () => {
const nested = { count: 1 };
const obj = { nested };
const copy = shallowClone(obj);
expect(copy === obj).toBe(false);
expect(copy.nested).toBe(nested);
expect(copy.nested).toBe(obj.nested);
});
test('preserves nested array references', () => {
const nested = [1, 2];
const obj = { nested };
const copy = shallowClone(obj);
expect(copy === obj).toBe(false);
expect(copy.nested).toBe(nested);
expect(copy.nested).toBe(obj.nested);
});
test('top-level mutation on the clone does not mutate the original', () => {
const obj = { a: 1, nested: { count: 1 } };
const copy = shallowClone(obj);
copy.a = 99;
expect(obj.a).toBe(1);
expect(copy.a).toBe(99);
});
test('nested mutation through the clone is visible through the original', () => {
const obj = { nested: { count: 1 } };
const copy = shallowClone(obj);
copy.nested.count = 2;
expect(obj.nested.count).toBe(2);
});
test('handles empty arrays and objects', () => {
const arr = [];
const obj = {};
const arrCopy = shallowClone(arr);
const objCopy = shallowClone(obj);
expect(arrCopy).toEqual([]);
expect(arrCopy === arr).toBe(false);
expect(objCopy).toEqual({});
expect(objCopy === obj).toBe(false);
});
test('throws TypeError for null and primitives', () => {
expect(() => shallowClone(null)).toThrow(TypeError);
expect(() => shallowClone(42)).toThrow(TypeError);
expect(() => shallowClone('x')).toThrow(TypeError);
expect(() => shallowClone(true)).toThrow(TypeError);
expect(() => shallowClone(undefined)).toThrow(TypeError);
});
test('does not mutate the original container while cloning', () => {
const arr = [1, { count: 1 }];
const obj = { a: 1, nested: { count: 1 } };
const arrBefore = arr.slice();
const objKeysBefore = Object.keys(obj);
shallowClone(arr);
shallowClone(obj);
expect(arr).toEqual(arrBefore);
expect(Object.keys(obj)).toEqual(objKeysBefore);
expect(obj).toEqual({ a: 1, nested: { count: 1 } });
});
test('copies own enumerable symbol properties', () => {
const key = Symbol('id');
const obj = { a: 1, [key]: 123 };
const copy = shallowClone(obj);
expect(copy[key]).toBe(123);
});
test('does not copy prototype properties', () => {
const proto = { inherited: true };
const obj = Object.create(proto);
obj.own = true;
const copy = shallowClone(obj);
expect(copy.own).toBe(true);
expect(copy.inherited).toBe(undefined);
expect('inherited' in copy).toBe(false);
});
});
Run tests to see results.