1
0
mirror of synced 2024-11-22 13:06:08 +03:00
select2/tests/utils/decorator-tests.js

190 lines
4.0 KiB
JavaScript
Raw Normal View History

2014-09-21 23:33:24 +04:00
module('Decorators');
2014-09-21 23:33:24 +04:00
var Utils = require('select2/utils');
2014-09-21 23:33:24 +04:00
test('overridden - method', function (assert) {
function BaseClass () {}
2014-09-21 23:05:00 +04:00
BaseClass.prototype.hello = function () {
2014-09-21 23:33:24 +04:00
return 'A';
};
2014-09-21 23:33:24 +04:00
function DecoratorClass () {}
2014-09-21 23:05:00 +04:00
DecoratorClass.prototype.hello = function () {
2014-09-21 23:33:24 +04:00
return 'B';
};
2014-09-21 23:05:00 +04:00
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
2014-09-21 23:05:00 +04:00
var inst = new DecoratedClass();
2014-09-21 23:33:24 +04:00
assert.strictEqual(inst.hello(), 'B');
});
2014-09-21 23:33:24 +04:00
test('overridden - constructor', function (assert) {
2014-09-21 23:05:00 +04:00
function BaseClass () {
this.inherited = true;
2014-09-21 23:33:24 +04:00
}
2014-09-21 23:05:00 +04:00
BaseClass.prototype.hello = function () {
2014-09-21 23:33:24 +04:00
return 'A';
};
2014-09-21 23:05:00 +04:00
function DecoratorClass (decorated) {
this.called = true;
2014-09-21 23:33:24 +04:00
}
2014-09-21 23:05:00 +04:00
DecoratorClass.prototype.other = function () {
2014-09-21 23:33:24 +04:00
return 'B';
};
2014-09-21 23:05:00 +04:00
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
2014-09-21 23:05:00 +04:00
var inst = new DecoratedClass();
2014-09-21 23:05:00 +04:00
assert.ok(inst.called);
assert.ok(!inst.inherited);
});
2014-09-21 23:33:24 +04:00
test('not overridden - method', function (assert) {
function BaseClass () {}
2014-09-21 23:05:00 +04:00
BaseClass.prototype.hello = function () {
2014-09-21 23:33:24 +04:00
return 'A';
};
2014-09-21 23:33:24 +04:00
function DecoratorClass () {}
2014-09-21 23:05:00 +04:00
DecoratorClass.prototype.other = function () {
2014-09-21 23:33:24 +04:00
return 'B';
};
2014-09-21 23:05:00 +04:00
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
2014-09-21 23:05:00 +04:00
var inst = new DecoratedClass();
2014-09-21 23:33:24 +04:00
assert.strictEqual(inst.hello(), 'A');
});
2014-09-21 23:33:24 +04:00
test('not overridden - constructor', function (assert) {
2014-09-21 23:05:00 +04:00
function BaseClass () {
this.called = true;
2014-09-21 23:33:24 +04:00
}
2014-09-21 23:05:00 +04:00
BaseClass.prototype.hello = function () {
2014-09-21 23:33:24 +04:00
return 'A';
};
2014-09-21 23:33:24 +04:00
function DecoratorClass () {}
2014-09-21 23:05:00 +04:00
DecoratorClass.prototype.other = function () {
2014-09-21 23:33:24 +04:00
return 'B';
};
2014-09-21 23:05:00 +04:00
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
2014-09-21 23:05:00 +04:00
var inst = new DecoratedClass();
2014-09-21 23:05:00 +04:00
assert.ok(inst.called);
});
2014-09-21 23:33:24 +04:00
test('inherited - method', function (assert) {
function BaseClass () {}
2014-09-21 23:05:00 +04:00
BaseClass.prototype.hello = function () {
2014-09-21 23:33:24 +04:00
return 'A';
};
2014-09-21 23:33:24 +04:00
function DecoratorClass (decorated) {}
2014-09-21 23:05:00 +04:00
DecoratorClass.prototype.hello = function (decorated) {
2014-09-21 23:33:24 +04:00
return 'B' + decorated.call(this) + 'C';
};
2014-09-21 23:05:00 +04:00
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
2014-09-21 23:05:00 +04:00
var inst = new DecoratedClass();
2014-09-21 23:33:24 +04:00
assert.strictEqual(inst.hello(), 'BAC');
});
2014-09-21 23:33:24 +04:00
test('inherited - constructor', function (assert) {
2014-09-21 23:05:00 +04:00
function BaseClass () {
this.inherited = true;
2014-09-21 23:33:24 +04:00
}
2014-09-21 23:05:00 +04:00
BaseClass.prototype.hello = function () {
2014-09-21 23:33:24 +04:00
return 'A';
};
2014-09-21 23:05:00 +04:00
function DecoratorClass (decorated) {
this.called = true;
2014-09-21 23:05:00 +04:00
decorated.call(this);
2014-09-21 23:33:24 +04:00
}
2014-09-21 23:05:00 +04:00
DecoratorClass.prototype.other = function () {
2014-09-21 23:33:24 +04:00
return 'B';
};
2014-09-21 23:05:00 +04:00
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
2014-09-21 23:05:00 +04:00
var inst = new DecoratedClass();
2014-09-21 23:05:00 +04:00
assert.ok(inst.called);
assert.ok(inst.inherited);
});
test('inherited - three levels', function (assert) {
function BaseClass (testArgument) {
this.baseCalled = true;
this.baseTestArgument = testArgument;
}
BaseClass.prototype.test = function (a) {
return a + 'c';
};
function MiddleClass (decorated, testArgument) {
this.middleCalled = true;
this.middleTestArgument = testArgument;
decorated.call(this, testArgument);
}
MiddleClass.prototype.test = function (decorated, a) {
return decorated.call(this, a + 'b');
};
function DecoratorClass (decorated, testArgument) {
this.decoratorCalled = true;
this.decoratorTestArgument = testArgument;
decorated.call(this, testArgument);
}
DecoratorClass.prototype.test = function (decorated, a) {
return decorated.call(this, a + 'a');
};
var DecoratedClass = Utils.Decorate(
Utils.Decorate(BaseClass, MiddleClass),
DecoratorClass
);
var inst = new DecoratedClass('test');
assert.ok(inst.baseCalled, 'The base class contructor was called');
assert.ok(inst.middleCalled, 'The middle class constructor was called');
assert.ok(inst.decoratorCalled, 'The decorator constructor was called');
assert.strictEqual(inst.baseTestArgument, 'test');
assert.strictEqual(inst.middleTestArgument, 'test');
assert.strictEqual(inst.decoratorTestArgument, 'test');
var out = inst.test('test');
assert.strictEqual(out, 'testabc');
});