1 | var helpers = require("../../lib/helpers.js");
|
2 | var assert = require("assert");
|
3 |
|
4 | var currentFileMark = ["\t\t\t", "[", __filename, "]", "\n"].join("");
|
5 | describe("helpers -> patch" + currentFileMark, function(){
|
6 |
|
7 | it("Simple patch", function(next){
|
8 | var target = {};
|
9 | helpers.patch( target, "child", "Some value" );
|
10 | assert.deepEqual(target, { "child": "Some value" });
|
11 | next();
|
12 | });
|
13 |
|
14 | it("Deep patch", function(next){
|
15 | var target = {};
|
16 | helpers.patch( target, "child.sub_child", "Some value" );
|
17 | assert.deepEqual(target, { "child": {"sub_child": "Some value"} });
|
18 | next();
|
19 | });
|
20 |
|
21 | it("Patch with existing child", function(next){
|
22 | var target = {};
|
23 | var child = {};
|
24 | target.child = child;
|
25 | helpers.patch( target, "child.sub_child", "Some value" );
|
26 | assert.deepEqual(target, { "child": {"sub_child": "Some value"} });
|
27 | assert.equal( target.child, child );
|
28 | next();
|
29 | });
|
30 |
|
31 | it("Patch with key-values object", function(next){
|
32 | var target = {};
|
33 | var child = {};
|
34 | target.child = child;
|
35 | helpers.patch( target, {
|
36 | "child.sub_child": "Some value",
|
37 | "child.sub_child2": "Other value",
|
38 | });
|
39 | assert.deepEqual( target, { "child": { "sub_child": "Some value", "sub_child2": "Other value" } });
|
40 | assert.equal( target.child, child );
|
41 | next();
|
42 | });
|
43 |
|
44 | it("Patch with overriding property", function(next){
|
45 | var target = {};
|
46 | var child1 = {};
|
47 | var child2 = {};
|
48 | target.child = child1;
|
49 | helpers.patch( target, {
|
50 | "child": child2,
|
51 | "child.sub_child": "Some value",
|
52 | "child.sub_child2": "Other value",
|
53 | });
|
54 | assert.deepEqual( target, { "child": { "sub_child": "Some value", "sub_child2": "Other value" } });
|
55 | assert.notEqual ( target.child, child1 );
|
56 | assert.equal ( target.child, child2 );
|
57 | next();
|
58 | });
|
59 |
|
60 | it("Patches prototype property", function(next){
|
61 |
|
62 | var child = {};
|
63 |
|
64 | var Target = function(){};
|
65 | Target.prototype.child = child
|
66 |
|
67 | var target = new Target();
|
68 | helpers.patch( target, {
|
69 | "child.sub_child": "Some value",
|
70 | "child.sub_child2": "Other value",
|
71 | });
|
72 | assert.deepEqual( target.child, { "sub_child": "Some value", "sub_child2": "Other value" } );
|
73 | assert.equal ( target.child, child );
|
74 | next();
|
75 | });
|
76 |
|
77 | it("Patch overrides prototype property", function(next){
|
78 |
|
79 | var child1 = {};
|
80 | var child2 = {};
|
81 |
|
82 | var Target = function(){};
|
83 | Target.prototype.child = child1
|
84 |
|
85 | var target = new Target();
|
86 | var target2 = new Target();
|
87 | helpers.patch( target, {
|
88 | "child": child2,
|
89 | "child.sub_child": "Some value",
|
90 | "child.sub_child2": "Other value",
|
91 | });
|
92 | assert.deepEqual( target.child, { "sub_child": "Some value", "sub_child2": "Other value" } );
|
93 | assert.notEqual ( target.child, child1 );
|
94 | assert.equal ( target.child, child2 );
|
95 | assert.equal ( target.__proto__.child, child2 );
|
96 | assert.equal ( target2.__proto__.child, child2 );
|
97 | next();
|
98 | });
|
99 |
|
100 | it("Patching deeply nested instances", function(next){
|
101 | var SubChild = function(value){ this.value = value; };
|
102 | var Child = function(){};
|
103 | var sub_child = new SubChild(99);
|
104 | Child.prototype.sub_child = sub_child;
|
105 | var Target = function(){};
|
106 | var child = new Child();
|
107 | Target.prototype.child = child;
|
108 | var target = new Target();
|
109 | var result = helpers.patch( target, "child.sub_child.value", 12 );
|
110 | assert.equal ( target.__proto__.child, child);
|
111 | assert.equal ( target.child, child);
|
112 | assert.equal ( target.__proto__.child.__proto__.sub_child, sub_child);
|
113 | assert.equal ( target.child.sub_child, sub_child);
|
114 | assert.equal ( target.child.sub_child.value, 12 );
|
115 | next();
|
116 | });
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | });
|