2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-12-16 01:57:34 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
|
|
|
|
import { assignWithNoUnnecessaryAllocation } from '../../util/assignWithNoUnnecessaryAllocation';
|
|
|
|
|
|
|
|
describe('assignWithNoUnnecessaryAllocation', () => {
|
2021-01-14 18:07:05 +00:00
|
|
|
type Person = {
|
2020-12-16 01:57:34 +00:00
|
|
|
name?: string;
|
|
|
|
age?: number;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-12-16 01:57:34 +00:00
|
|
|
|
|
|
|
it('returns the same object if there are no modifications', () => {
|
|
|
|
const empty = {};
|
|
|
|
assert.strictEqual(assignWithNoUnnecessaryAllocation(empty, {}), empty);
|
|
|
|
|
|
|
|
const obj = {
|
|
|
|
foo: 'bar',
|
|
|
|
baz: 'qux',
|
|
|
|
und: undefined,
|
|
|
|
};
|
|
|
|
assert.strictEqual(assignWithNoUnnecessaryAllocation(obj, {}), obj);
|
|
|
|
assert.strictEqual(
|
|
|
|
assignWithNoUnnecessaryAllocation(obj, { foo: 'bar' }),
|
|
|
|
obj
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
assignWithNoUnnecessaryAllocation(obj, { baz: 'qux' }),
|
|
|
|
obj
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
assignWithNoUnnecessaryAllocation(obj, { und: undefined }),
|
|
|
|
obj
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a new object if there are modifications', () => {
|
|
|
|
const empty: Person = {};
|
|
|
|
assert.deepEqual(
|
|
|
|
assignWithNoUnnecessaryAllocation(empty, { name: 'Bert' }),
|
|
|
|
{ name: 'Bert' }
|
|
|
|
);
|
|
|
|
assert.deepEqual(assignWithNoUnnecessaryAllocation(empty, { age: 8 }), {
|
|
|
|
age: 8,
|
|
|
|
});
|
|
|
|
assert.deepEqual(
|
|
|
|
assignWithNoUnnecessaryAllocation(empty, { name: undefined }),
|
|
|
|
{
|
|
|
|
name: undefined,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const obj: Person = { name: 'Ernie' };
|
|
|
|
assert.deepEqual(
|
|
|
|
assignWithNoUnnecessaryAllocation(obj, { name: 'Big Bird' }),
|
|
|
|
{
|
|
|
|
name: 'Big Bird',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert.deepEqual(assignWithNoUnnecessaryAllocation(obj, { age: 9 }), {
|
|
|
|
name: 'Ernie',
|
|
|
|
age: 9,
|
|
|
|
});
|
|
|
|
assert.deepEqual(
|
|
|
|
assignWithNoUnnecessaryAllocation(obj, { age: undefined }),
|
|
|
|
{
|
|
|
|
name: 'Ernie',
|
|
|
|
age: undefined,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('only performs a shallow comparison', () => {
|
|
|
|
const obj = { foo: { bar: 'baz' } };
|
|
|
|
assert.notStrictEqual(
|
|
|
|
assignWithNoUnnecessaryAllocation(obj, { foo: { bar: 'baz' } }),
|
|
|
|
obj
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't modify the original object when there are no modifications", () => {
|
|
|
|
const empty = {};
|
|
|
|
assignWithNoUnnecessaryAllocation(empty, {});
|
|
|
|
assert.deepEqual(empty, {});
|
|
|
|
|
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
assignWithNoUnnecessaryAllocation(obj, { foo: 'bar' });
|
|
|
|
assert.deepEqual(obj, { foo: 'bar' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't modify the original object when there are modifications", () => {
|
|
|
|
const empty: Person = {};
|
|
|
|
assignWithNoUnnecessaryAllocation(empty, { name: 'Bert' });
|
|
|
|
assert.deepEqual(empty, {});
|
|
|
|
|
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
assignWithNoUnnecessaryAllocation(obj, { foo: 'baz' });
|
|
|
|
assert.deepEqual(obj, { foo: 'bar' });
|
|
|
|
});
|
|
|
|
});
|