Skip to content

Commit 393e2cb

Browse files
committed
adding a test to ensure using merge-deep for inheritance still works
1 parent c39b161 commit 393e2cb

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

test.js

+52
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,56 @@ describe('mergeDeep', function() {
146146
assert.notEqual(actual.keys, 42);
147147
assert.notEqual(actual.constructor.keys, 42);
148148
});
149+
150+
it('should allow being used for custom constructors', function() {
151+
// The following setup code is a simple way to demonstrate multiple inheritance by merging the prototype of one class onto another
152+
function Shape() {
153+
this.type = '';
154+
}
155+
156+
function Position(x, y) {
157+
this.x = x || 0;
158+
this.y = y || 0;
159+
}
160+
161+
Position.prototype.stringify = function() {
162+
return '(' + this.x + ', ' + this.y + ')';
163+
};
164+
165+
function Moveable(x, y) {
166+
Position.call(this, x, y);
167+
}
168+
169+
// By making Moveable inherit from Position, allows us to test what happens when `constructor` is passed to `isValidKey`.
170+
Moveable.prototype = Object.create(Position.prototype);
171+
Moveable.prototype.constructor = Moveable;
172+
Moveable.prototype = merge(Moveable.prototype, Position.prototype);
173+
174+
Moveable.prototype.move = function(x, y) {
175+
this.x += x;
176+
this.y += y;
177+
};
178+
179+
Moveable.prototype.position = function() {
180+
return this.stringify();
181+
};
182+
183+
function Rectangle() {
184+
Shape.call(this);
185+
Moveable.call(this);
186+
this.type = 'rectangle';
187+
}
188+
189+
// Single inheritance using Object.create
190+
Rectangle.prototype = Object.create(Shape.prototype);
191+
Rectangle.prototype.constructor = Rectangle;
192+
193+
// This is the test to ensure that `merge-deep` can be used with prototypal inheritance
194+
Rectangle.prototype = merge(Rectangle.prototype, Moveable.prototype);
195+
196+
var rectangle = new Rectangle();
197+
assert.equal(rectangle.position(), '(0, 0)');
198+
rectangle.move(10, 20);
199+
assert.equal(rectangle.position(), '(10, 20)');
200+
});
149201
});

0 commit comments

Comments
 (0)