decorator-tests.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. });