2019-08-08 04:16:45 +03:00
|
|
|
QUnit.module('Decorators');
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:33:24 +04:00
|
|
|
var Utils = require('select2/utils');
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2019-08-08 04:16:45 +03:00
|
|
|
QUnit.test('overridden - method', function (assert) {
|
2014-09-21 23:33:24 +04:00
|
|
|
function BaseClass () {}
|
2014-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:33:24 +04:00
|
|
|
function DecoratorClass () {}
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
DecoratorClass.prototype.hello = function () {
|
2014-09-21 23:33:24 +04:00
|
|
|
return 'B';
|
|
|
|
};
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var inst = new DecoratedClass();
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:33:24 +04:00
|
|
|
assert.strictEqual(inst.hello(), 'B');
|
2014-08-29 03:54:01 +04:00
|
|
|
});
|
|
|
|
|
2019-08-08 04:16:45 +03:00
|
|
|
QUnit.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-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
function DecoratorClass (decorated) {
|
|
|
|
this.called = true;
|
2014-09-21 23:33:24 +04:00
|
|
|
}
|
2014-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var inst = new DecoratedClass();
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
assert.ok(inst.called);
|
|
|
|
assert.ok(!inst.inherited);
|
2014-08-29 03:54:01 +04:00
|
|
|
});
|
|
|
|
|
2019-08-08 04:16:45 +03:00
|
|
|
QUnit.test('not overridden - method', function (assert) {
|
2014-09-21 23:33:24 +04:00
|
|
|
function BaseClass () {}
|
2014-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:33:24 +04:00
|
|
|
function DecoratorClass () {}
|
2014-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var inst = new DecoratedClass();
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:33:24 +04:00
|
|
|
assert.strictEqual(inst.hello(), 'A');
|
2014-08-29 03:54:01 +04:00
|
|
|
});
|
|
|
|
|
2019-08-08 04:16:45 +03:00
|
|
|
QUnit.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-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:33:24 +04:00
|
|
|
function DecoratorClass () {}
|
2014-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var inst = new DecoratedClass();
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
assert.ok(inst.called);
|
2014-08-29 03:54:01 +04:00
|
|
|
});
|
|
|
|
|
2019-08-08 04:16:45 +03:00
|
|
|
QUnit.test('inherited - method', function (assert) {
|
2014-09-21 23:33:24 +04:00
|
|
|
function BaseClass () {}
|
2014-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:33:24 +04:00
|
|
|
function DecoratorClass (decorated) {}
|
2014-08-29 03:54:01 +04:00
|
|
|
|
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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var inst = new DecoratedClass();
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:33:24 +04:00
|
|
|
assert.strictEqual(inst.hello(), 'BAC');
|
2014-08-29 03:54:01 +04:00
|
|
|
});
|
|
|
|
|
2019-08-08 04:16:45 +03:00
|
|
|
QUnit.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-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
function DecoratorClass (decorated) {
|
|
|
|
this.called = true;
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
decorated.call(this);
|
2014-09-21 23:33:24 +04:00
|
|
|
}
|
2014-08-29 03:54:01 +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-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
var inst = new DecoratedClass();
|
2014-08-29 03:54:01 +04:00
|
|
|
|
2014-09-21 23:05:00 +04:00
|
|
|
assert.ok(inst.called);
|
|
|
|
assert.ok(inst.inherited);
|
2014-08-29 03:54:01 +04:00
|
|
|
});
|
2014-11-23 03:21:46 +03:00
|
|
|
|
2019-08-08 04:16:45 +03:00
|
|
|
QUnit.test('inherited - three levels', function (assert) {
|
2014-11-23 03:21:46 +03:00
|
|
|
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');
|
|
|
|
});
|