test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* global describe, it, window, require */
  2. (function () {
  3. var Proto, assert;
  4. if (typeof require === 'function') {
  5. Proto = require('../lib/proto');
  6. assert = require('assert');
  7. } else {
  8. Proto = window.Proto;
  9. assert = window.assert;
  10. }
  11. describe('UberProto', function () {
  12. it('extends objects', function () {
  13. var Extended = Proto.extend({
  14. sayHi: function () {
  15. assert.ok(true, 'sayHi called');
  16. return 'hi';
  17. }
  18. });
  19. assert.equal(Extended.create().sayHi(), 'hi', 'Said hi');
  20. });
  21. it('creates a new object', function () {
  22. var Obj = Proto.extend({
  23. init: function (name) {
  24. assert.ok(true, 'Init called');
  25. this.name = name;
  26. },
  27. sayHi: function () {
  28. return 'Hi ' + this.name;
  29. },
  30. prop: 'Testing'
  31. });
  32. var inst = Obj.create('Tester');
  33. assert.equal(inst.name, 'Tester', 'Name set');
  34. assert.equal(inst.prop, 'Testing', 'Prototype property still there');
  35. assert.equal(inst.sayHi(), 'Hi Tester', 'Said hi with name');
  36. assert.ok(Proto.isPrototypeOf(Obj), 'Should have prototype of Proto');
  37. assert.ok(Obj.isPrototypeOf(inst), 'Instance should have prototype of Obj');
  38. });
  39. it('uses an init method alias', function () {
  40. var Obj = Proto.extend({
  41. __init: 'myConstructor',
  42. myConstructor: function (arg) {
  43. assert.equal(arg, 'myConstructor', 'Got proper arguments in myConstructor');
  44. }
  45. }),
  46. OtherObj = {
  47. __init: 'testConstructor',
  48. testConstructor: function (arg) {
  49. assert.equal(arg, 'testConstructor', 'Got proper arguments in myConstructor');
  50. }
  51. };
  52. Obj.create('myConstructor');
  53. Proto.create.call(OtherObj, 'testConstructor');
  54. });
  55. it('uses _super', function () {
  56. var Obj = Proto.extend({
  57. init: function (name) {
  58. assert.ok(true, 'Super init called');
  59. this.name = name;
  60. }
  61. }), Sub = Obj.extend({
  62. init: function () {
  63. this._super.apply(this, arguments);
  64. assert.ok(true, 'Sub init called');
  65. }
  66. });
  67. var inst = Sub.create('Tester');
  68. assert.equal(inst.name, 'Tester', 'Name set in prototype');
  69. });
  70. it('extends an existing object', function () {
  71. var Obj = {
  72. test: function (name) {
  73. assert.ok(true, 'Super test method called');
  74. this.name = name;
  75. }
  76. };
  77. var Extended = Proto.extend({
  78. test: function () {
  79. this._super.apply(this, arguments);
  80. assert.ok(true, 'Sub init called');
  81. }
  82. }, Obj);
  83. Extended.test('Tester');
  84. assert.equal(Extended.name, 'Tester', 'Name set in prototype');
  85. });
  86. it('uses .mixin', function () {
  87. var Obj = Proto.extend({
  88. init: function (name) {
  89. assert.ok(true, 'Init called');
  90. this.name = name;
  91. }
  92. });
  93. Obj.mixin({
  94. test: function () {
  95. return this.name;
  96. }
  97. });
  98. var inst = Obj.create('Tester');
  99. assert.equal(inst.test(), 'Tester', 'Mixin returned name');
  100. Obj.mixin({
  101. test: function () {
  102. return this._super() + ' mixed in';
  103. }
  104. });
  105. assert.equal(inst.test(), 'Tester mixed in', 'Mixin called overwritten');
  106. });
  107. it('.mixin(Object)', function () {
  108. var Obj = {
  109. test: function (name) {
  110. assert.ok(true, 'Super test method called');
  111. this.name = name;
  112. }
  113. };
  114. Proto.mixin({
  115. test: function () {
  116. this._super.apply(this, arguments);
  117. assert.ok(true, 'Sub init called');
  118. }
  119. }, Obj);
  120. Obj.test('Tester');
  121. assert.equal(Obj.name, 'Tester', 'Name set in prototype');
  122. });
  123. it('uses .proxy', function () {
  124. var Obj = Proto.extend({
  125. init: function (name) {
  126. this.name = name;
  127. },
  128. test: function (arg) {
  129. return this.name + ' ' + arg;
  130. }
  131. });
  132. var inst = Obj.create('Tester');
  133. var callback = inst.proxy('test');
  134. assert.equal(callback('arg'), 'Tester arg', 'Callback set scope properly');
  135. callback = inst.proxy('test', 'partialed');
  136. assert.equal(callback(), 'Tester partialed', 'Callback partially applied');
  137. });
  138. describe('Babel transpiled classes (#10)', function() {
  139. if (typeof require !== 'function' || typeof Object.defineProperty !== 'function') {
  140. return;
  141. }
  142. var classes = require('./class-fixture.es5.js');
  143. it('works with Babel transpiled classes (#10)', function() {
  144. var person = new classes.Person('John');
  145. assert.equal(person.sayHi(), 'Hi John');
  146. Proto.mixin({
  147. sayHi: function() {
  148. return this._super() + '!!';
  149. }
  150. }, person);
  151. assert.equal(person.sayHi(), 'Hi John!!');
  152. var otherPerson = new classes.OtherPerson();
  153. assert.equal(otherPerson.sayHi(), 'Hi David Luecke');
  154. Proto.mixin({
  155. sayHi: function() {
  156. return this._super() + '???';
  157. }
  158. }, otherPerson);
  159. assert.equal(otherPerson.sayHi(), 'Hi David Luecke???');
  160. assert.ok(otherPerson.test());
  161. });
  162. it('can extend from Babel transpiled classes (#10)', function() {
  163. var otherPerson = new classes.OtherPerson();
  164. assert.equal(otherPerson.sayHi(), 'Hi David Luecke');
  165. var extended = Proto.extend(otherPerson);
  166. assert.equal(typeof extended.sayHi, 'function');
  167. assert.equal(extended.sayHi(), 'Hi David Luecke');
  168. assert.ok(extended.test());
  169. assert.ok(!Object.getOwnPropertyDescriptor(extended, 'sayHi').enumerable);
  170. assert.ok(!Object.getOwnPropertyDescriptor(extended, 'test').enumerable);
  171. assert.ok(Object.getOwnPropertyDescriptor(extended, 'name').enumerable);
  172. var extext = extended.extend({
  173. sayHi: function() {
  174. return this._super() + '!!!';
  175. }
  176. });
  177. assert.equal(extext.sayHi(), 'Hi David Luecke!!!');
  178. });
  179. });
  180. });
  181. })();