|
| 1 | +// Copyright 2020 Signal Messenger, LLC |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 3 | + |
| 4 | +import { assert } from 'chai'; |
| 5 | + |
| 6 | +import { assignWithNoUnnecessaryAllocation } from '../../util/assignWithNoUnnecessaryAllocation'; |
| 7 | + |
| 8 | +describe('assignWithNoUnnecessaryAllocation', () => { |
| 9 | + interface Person { |
| 10 | + name?: string; |
| 11 | + age?: number; |
| 12 | + } |
| 13 | + |
| 14 | + it('returns the same object if there are no modifications', () => { |
| 15 | + const empty = {}; |
| 16 | + assert.strictEqual(assignWithNoUnnecessaryAllocation(empty, {}), empty); |
| 17 | + |
| 18 | + const obj = { |
| 19 | + foo: 'bar', |
| 20 | + baz: 'qux', |
| 21 | + und: undefined, |
| 22 | + }; |
| 23 | + assert.strictEqual(assignWithNoUnnecessaryAllocation(obj, {}), obj); |
| 24 | + assert.strictEqual( |
| 25 | + assignWithNoUnnecessaryAllocation(obj, { foo: 'bar' }), |
| 26 | + obj |
| 27 | + ); |
| 28 | + assert.strictEqual( |
| 29 | + assignWithNoUnnecessaryAllocation(obj, { baz: 'qux' }), |
| 30 | + obj |
| 31 | + ); |
| 32 | + assert.strictEqual( |
| 33 | + assignWithNoUnnecessaryAllocation(obj, { und: undefined }), |
| 34 | + obj |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns a new object if there are modifications', () => { |
| 39 | + const empty: Person = {}; |
| 40 | + assert.deepEqual( |
| 41 | + assignWithNoUnnecessaryAllocation(empty, { name: 'Bert' }), |
| 42 | + { name: 'Bert' } |
| 43 | + ); |
| 44 | + assert.deepEqual(assignWithNoUnnecessaryAllocation(empty, { age: 8 }), { |
| 45 | + age: 8, |
| 46 | + }); |
| 47 | + assert.deepEqual( |
| 48 | + assignWithNoUnnecessaryAllocation(empty, { name: undefined }), |
| 49 | + { |
| 50 | + name: undefined, |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + const obj: Person = { name: 'Ernie' }; |
| 55 | + assert.deepEqual( |
| 56 | + assignWithNoUnnecessaryAllocation(obj, { name: 'Big Bird' }), |
| 57 | + { |
| 58 | + name: 'Big Bird', |
| 59 | + } |
| 60 | + ); |
| 61 | + assert.deepEqual(assignWithNoUnnecessaryAllocation(obj, { age: 9 }), { |
| 62 | + name: 'Ernie', |
| 63 | + age: 9, |
| 64 | + }); |
| 65 | + assert.deepEqual( |
| 66 | + assignWithNoUnnecessaryAllocation(obj, { age: undefined }), |
| 67 | + { |
| 68 | + name: 'Ernie', |
| 69 | + age: undefined, |
| 70 | + } |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('only performs a shallow comparison', () => { |
| 75 | + const obj = { foo: { bar: 'baz' } }; |
| 76 | + assert.notStrictEqual( |
| 77 | + assignWithNoUnnecessaryAllocation(obj, { foo: { bar: 'baz' } }), |
| 78 | + obj |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it("doesn't modify the original object when there are no modifications", () => { |
| 83 | + const empty = {}; |
| 84 | + assignWithNoUnnecessaryAllocation(empty, {}); |
| 85 | + assert.deepEqual(empty, {}); |
| 86 | + |
| 87 | + const obj = { foo: 'bar' }; |
| 88 | + assignWithNoUnnecessaryAllocation(obj, { foo: 'bar' }); |
| 89 | + assert.deepEqual(obj, { foo: 'bar' }); |
| 90 | + }); |
| 91 | + |
| 92 | + it("doesn't modify the original object when there are modifications", () => { |
| 93 | + const empty: Person = {}; |
| 94 | + assignWithNoUnnecessaryAllocation(empty, { name: 'Bert' }); |
| 95 | + assert.deepEqual(empty, {}); |
| 96 | + |
| 97 | + const obj = { foo: 'bar' }; |
| 98 | + assignWithNoUnnecessaryAllocation(obj, { foo: 'baz' }); |
| 99 | + assert.deepEqual(obj, { foo: 'bar' }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments