1
0
mirror of synced 2024-11-26 06:46:04 +03:00
select2/tests/utils/decorator-tests.js

138 lines
2.6 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);
});