/
mixin.js
63 lines (53 loc) · 2.24 KB
/
mixin.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
var test = require('tape')
, mixly = require('../')
;
/**
* Test object one
*/
var o1 = {
O1: true,
commonThing: 'o1'
};
/**
* Test object one
*/
var o2 = {
O2: true,
commonThing: 'o2'
};
/**
* Test object one
*/
var o3 = {
O3: true,
commonThing: 'o3'
};
test('mixin', function(t)
{
t.plan(19);
var o0 = mixly(o1, o2, o3);
t.notEqual(Object.getPrototypeOf(o0), o1, 'keeps `o1` out of the "loop"');
t.notEqual(Object.getPrototypeOf(o1), o2, 'keeps original objects (o1, o2) untouched');
t.notEqual(Object.getPrototypeOf(o2), o3, 'keeps original objects (o2, o3) untouched');
t.true(o0.O1, 'copies properties from `o1` into prototype chain of `o0`');
t.true(o0.O2, 'copies properties from `o2` into prototype chain of `o0`');
t.true(o0.O3, 'copies properties from `o3` into prototype chain of `o0`');
t.equal(o0.commonThing, 'o1', 'properties from left-most object "win"');
// o0 == o1
t.true(o0.hasOwnProperty('commonThing'), 'makes result object a shallow copy of the `o1` object');
t.true(o0.hasOwnProperty('O1'), 'makes result object a shallow copy of the `o1` object');
t.false(o0.hasOwnProperty('O2'), 'keeps copied properties from `o2` out of result object');
t.false(o0.hasOwnProperty('O3'), 'keeps all the copied properties from `o3` out of result object');
// o0 -> o2
var o0o2 = Object.getPrototypeOf(o0);
t.false(o0o2.hasOwnProperty('O1'), 'keeps o1 properties on the respective place in the prototype chain');
t.true(o0o2.hasOwnProperty('O2'), 'keeps o2 properties on the respective place in the prototype chain');
t.false(o0o2.hasOwnProperty('O3'), 'keeps o3 properties on the respective place in the prototype chain');
t.equal(o0o2.commonThing, 'o2', 'shared properties accessible via prototype chain');
// o0 -> o2 -> o3
var o0o2o3 = Object.getPrototypeOf(Object.getPrototypeOf(o0));
t.false(o0o2o3.hasOwnProperty('O1'), 'keeps o1 properties on the respective place in the prototype chain');
t.false(o0o2o3.hasOwnProperty('O2'), 'keeps o2 properties on the respective place in the prototype chain');
t.true(o0o2o3.hasOwnProperty('O3'), 'keeps o3 properties on the respective place in the prototype chain');
t.equal(o0o2o3.commonThing, 'o3', 'shared properties accessible via prototype chain');
});