decorator-tests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. module('Decorators');
  2. var Utils = require('select2/utils');
  3. test('overridden - method', function (assert) {
  4. function BaseClass () {}
  5. BaseClass.prototype.hello = function () {
  6. return 'A';
  7. };
  8. function DecoratorClass () {}
  9. DecoratorClass.prototype.hello = function () {
  10. return 'B';
  11. };
  12. var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
  13. var inst = new DecoratedClass();
  14. assert.strictEqual(inst.hello(), 'B');
  15. });
  16. test('overridden - constructor', function (assert) {
  17. function BaseClass () {
  18. this.inherited = true;
  19. }
  20. BaseClass.prototype.hello = function () {
  21. return 'A';
  22. };
  23. function DecoratorClass (decorated) {
  24. this.called = true;
  25. }
  26. DecoratorClass.prototype.other = function () {
  27. return 'B';
  28. };
  29. var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
  30. var inst = new DecoratedClass();
  31. assert.ok(inst.called);
  32. assert.ok(!inst.inherited);
  33. });
  34. test('not overridden - method', function (assert) {
  35. function BaseClass () {}
  36. BaseClass.prototype.hello = function () {
  37. return 'A';
  38. };
  39. function DecoratorClass () {}
  40. DecoratorClass.prototype.other = function () {
  41. return 'B';
  42. };
  43. var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
  44. var inst = new DecoratedClass();
  45. assert.strictEqual(inst.hello(), 'A');
  46. });
  47. test('not overridden - constructor', function (assert) {
  48. function BaseClass () {
  49. this.called = true;
  50. }
  51. BaseClass.prototype.hello = function () {
  52. return 'A';
  53. };
  54. function DecoratorClass () {}
  55. DecoratorClass.prototype.other = function () {
  56. return 'B';
  57. };
  58. var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
  59. var inst = new DecoratedClass();
  60. assert.ok(inst.called);
  61. });
  62. test('inherited - method', function (assert) {
  63. function BaseClass () {}
  64. BaseClass.prototype.hello = function () {
  65. return 'A';
  66. };
  67. function DecoratorClass (decorated) {}
  68. DecoratorClass.prototype.hello = function (decorated) {
  69. return 'B' + decorated.call(this) + 'C';
  70. };
  71. var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
  72. var inst = new DecoratedClass();
  73. assert.strictEqual(inst.hello(), 'BAC');
  74. });
  75. test('inherited - constructor', function (assert) {
  76. function BaseClass () {
  77. this.inherited = true;
  78. }
  79. BaseClass.prototype.hello = function () {
  80. return 'A';
  81. };
  82. function DecoratorClass (decorated) {
  83. this.called = true;
  84. decorated.call(this);
  85. }
  86. DecoratorClass.prototype.other = function () {
  87. return 'B';
  88. };
  89. var DecoratedClass = Utils.Decorate(BaseClass, DecoratorClass);
  90. var inst = new DecoratedClass();
  91. assert.ok(inst.called);
  92. assert.ok(inst.inherited);
  93. });
  94. test('inherited - three levels', function (assert) {
  95. function BaseClass (testArgument) {
  96. this.baseCalled = true;
  97. this.baseTestArgument = testArgument;
  98. }
  99. BaseClass.prototype.test = function (a) {
  100. return a + 'c';
  101. };
  102. function MiddleClass (decorated, testArgument) {
  103. this.middleCalled = true;
  104. this.middleTestArgument = testArgument;
  105. decorated.call(this, testArgument);
  106. }
  107. MiddleClass.prototype.test = function (decorated, a) {
  108. return decorated.call(this, a + 'b');
  109. };
  110. function DecoratorClass (decorated, testArgument) {
  111. this.decoratorCalled = true;
  112. this.decoratorTestArgument = testArgument;
  113. decorated.call(this, testArgument);
  114. }
  115. DecoratorClass.prototype.test = function (decorated, a) {
  116. return decorated.call(this, a + 'a');
  117. };
  118. var DecoratedClass = Utils.Decorate(
  119. Utils.Decorate(BaseClass, MiddleClass),
  120. DecoratorClass
  121. );
  122. var inst = new DecoratedClass('test');
  123. assert.ok(inst.baseCalled, 'The base class contructor was called');
  124. assert.ok(inst.middleCalled, 'The middle class constructor was called');
  125. assert.ok(inst.decoratorCalled, 'The decorator constructor was called');
  126. assert.strictEqual(inst.baseTestArgument, 'test');
  127. assert.strictEqual(inst.middleTestArgument, 'test');
  128. assert.strictEqual(inst.decoratorTestArgument, 'test');
  129. var out = inst.test('test');
  130. assert.strictEqual(out, 'testabc');
  131. });