-
Notifications
You must be signed in to change notification settings - Fork 83
/
assign.test.js
52 lines (45 loc) · 1.27 KB
/
assign.test.js
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
'use strict'
const { assign } = require('../../lib/conventions')
describe('assign', () => {
test.each([null, undefined, true, false, 0, NaN])(
'should throw when `target` is `%s`',
(target) => {
expect(() => assign(target, {})).toThrow(TypeError)
}
)
test('should return target', () => {
const target = {}
expect(assign(target, undefined)).toBe(target)
})
test('should copy all enumerable fields from source to target', () => {
const target = {}
const source = { a: 'A', 0: 0 }
assign(target, source)
expect(target).toEqual(source)
})
test('should not copy prototype properties to source', () => {
const target = {}
function Clazz(yes) {
this.yes = yes
}
Clazz.prototype.dont = 5
Clazz.prototype.hasOwnProperty = () => true
const source = new Clazz(1)
assign(target, source)
expect(target).toEqual({ yes: 1 })
})
test('should have no issue with null source', () => {
const target = {}
assign(target, null)
})
test('should have no issue with undefined source', () => {
const target = {}
assign(target, undefined)
})
test('should override existing keys', () => {
const target = { key: 4, same: 'same' }
const source = { key: undefined }
assign(target, source)
expect(target).toEqual({ key: undefined, same: 'same' })
})
})