-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtest.js
129 lines (110 loc) · 2.74 KB
/
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
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
import test from 'ava';
Object.assign = require('./');
const objectAssign = require('./');
test('have the correct length', t => {
t.is(objectAssign.length, 2);
});
test('throw when target is not an object', t => {
t.throws(() => {
objectAssign(null);
}, TypeError);
t.throws(() => {
objectAssign(undefined);
}, TypeError);
});
test('objectAssign own enumerable properties from source to target object', t => {
t.deepEqual(objectAssign({foo: 0}, {bar: 1}), {
foo: 0,
bar: 1
});
t.deepEqual(objectAssign({foo: 0}, null, undefined), {foo: 0});
t.deepEqual(objectAssign({foo: 0}, null, undefined, {bar: 1}, null), {
foo: 0,
bar: 1
});
});
test('throw on null/undefined target', t => {
t.throws(() => {
objectAssign(null, {});
});
t.throws(() => {
objectAssign(undefined, {});
});
t.throws(() => {
objectAssign(undefined, undefined);
});
});
test('not throw on null/undefined sources', t => {
t.notThrows(() => {
objectAssign({}, null);
});
t.notThrows(() => {
objectAssign({}, undefined);
});
t.notThrows(() => {
objectAssign({}, undefined, null);
});
});
test('support multiple sources', t => {
t.deepEqual(objectAssign({foo: 0}, {bar: 1}, {bar: 2}), {
foo: 0,
bar: 2
});
t.deepEqual(objectAssign({}, {}, {foo: 1}), {foo: 1});
});
test('only iterate own keys', t => {
const Unicorn = function () {};
Unicorn.prototype.rainbows = 'many';
const unicorn = new Unicorn();
unicorn.bar = 1;
t.deepEqual(objectAssign({foo: 1}, unicorn), {
foo: 1,
bar: 1
});
});
test('return the modified target object', t => {
const target = {};
const returned = objectAssign(target, {a: 1});
t.is(returned, target);
});
test('support `Object.create(null)` objects', t => {
const obj = Object.create(null);
obj.foo = true;
t.deepEqual(objectAssign({}, obj), {foo: true});
});
test('preserve property order', t => {
const letters = 'abcdefghijklmnopqrst';
const source = {};
letters.split('').forEach(letter => {
source[letter] = letter;
});
const target = objectAssign({}, source);
t.is(Object.keys(target).join(''), letters);
});
test('accept primitives as target', t => {
const target = objectAssign('abcdefg', {foo: 'bar'});
const strObj = Object('abcdefg');
strObj.foo = 'bar';
t.deepEqual(target, strObj);
});
if (typeof global.Symbol !== 'undefined') {
test('support symbol properties', t => {
const target = {};
const source = {};
const sym = Symbol('foo');
source[sym] = 'bar';
objectAssign(target, source);
t.is(target[sym], 'bar');
});
test('only copy enumerable symbols', t => {
const target = {};
const source = {};
const sym = Symbol('foo');
Object.defineProperty(source, sym, {
enumerable: false,
value: 'bar'
});
objectAssign(target, source);
t.is(target[sym], undefined);
});
}