mirror of
https://github.com/nasa/openmct.git
synced 2025-02-20 17:33:23 +00:00
[Framework] Add spec for partial constructor
Add spec for partial constructor, used to handle two-stage application of Angular dependencies and constructor arguments. WTD-518.
This commit is contained in:
parent
b0267a9c59
commit
5d4f1f2008
@ -31,14 +31,13 @@ define(
|
|||||||
|
|
||||||
return function () { // Bind everything else
|
return function () { // Bind everything else
|
||||||
var other = Array.prototype.slice.call(arguments),
|
var other = Array.prototype.slice.call(arguments),
|
||||||
instance = {};
|
instance = Object.create(Constructor.prototype);
|
||||||
|
|
||||||
// Mimic "new" behavior with apply.
|
// Mimic "new" behavior with apply.
|
||||||
instance = Constructor.apply(
|
instance = Constructor.apply(
|
||||||
instance,
|
instance,
|
||||||
dependencies.concat(other)
|
dependencies.concat(other)
|
||||||
) || instance;
|
) || instance;
|
||||||
instance.prototype = Constructor.prototype;
|
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
};
|
};
|
||||||
|
@ -4,11 +4,63 @@
|
|||||||
* PartialConstructorSpec. Created by vwoeltje on 11/6/14.
|
* PartialConstructorSpec. Created by vwoeltje on 11/6/14.
|
||||||
*/
|
*/
|
||||||
define(
|
define(
|
||||||
[],
|
["../../src/register/PartialConstructor"],
|
||||||
function () {
|
function (PartialConstructor) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
describe("", function () {
|
describe("A partial constructor", function () {
|
||||||
|
var result,
|
||||||
|
PartializedConstructor;
|
||||||
|
|
||||||
|
function RegularConstructor(a, b, c, d) {
|
||||||
|
result = {
|
||||||
|
a: a,
|
||||||
|
b: b,
|
||||||
|
c: c,
|
||||||
|
d: d
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ThisStyleConstructor(x, y, z) {
|
||||||
|
this.message = [x, y, z].join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
result = undefined;
|
||||||
|
PartializedConstructor = new PartialConstructor(RegularConstructor);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("splits a constructor call into two stages", function () {
|
||||||
|
var RemainingConstructor = new PartializedConstructor("test"),
|
||||||
|
instance;
|
||||||
|
// first call should not have hit constructor
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
|
||||||
|
// Call again
|
||||||
|
instance = new RemainingConstructor(1, 2, 3);
|
||||||
|
expect(result).toEqual({
|
||||||
|
a: "test",
|
||||||
|
b: 1,
|
||||||
|
c: 2,
|
||||||
|
d: 3
|
||||||
|
});
|
||||||
|
|
||||||
|
// Should have returned the constructor's value
|
||||||
|
expect(instance).toEqual(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles this-style constructors", function () {
|
||||||
|
var Partialized = new PartialConstructor(ThisStyleConstructor),
|
||||||
|
Remaining = new Partialized("this"),
|
||||||
|
instance = new Remaining("is", "correct");
|
||||||
|
|
||||||
|
// We should have everything we put in "this", and we
|
||||||
|
// should still pass an instanceof test.g
|
||||||
|
expect(instance.message).toEqual("this is correct");
|
||||||
|
expect(instance).toEqual(new ThisStyleConstructor("this", "is", "correct"));
|
||||||
|
expect(instance instanceof ThisStyleConstructor).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user