8
0

utils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals describe, it, expect, bender */
  6. 'use strict';
  7. var modules = bender.amd.require( 'utils', 'utils-lodash' );
  8. describe( 'extend()', function() {
  9. // Properties of the subsequent objects should override properties of the preceding objects. This is critical for
  10. // CKEditor so we keep this test to ensure that Lo-Dash (or whatever) implements it in the way we need it.
  11. it( 'should extend by several params in the correct order', function() {
  12. var utils = modules.utils;
  13. var target = {
  14. a: 0,
  15. b: 0
  16. };
  17. var ext1 = {
  18. b: 1,
  19. c: 1
  20. };
  21. var ext2 = {
  22. c: 2,
  23. d: 2
  24. };
  25. utils.extend( target, ext1, ext2 );
  26. expect( target ).to.have.property( 'a' ).to.equal( 0 );
  27. expect( target ).to.have.property( 'b' ).to.equal( 1 );
  28. expect( target ).to.have.property( 'c' ).to.equal( 2 );
  29. expect( target ).to.have.property( 'd' ).to.equal( 2 );
  30. } );
  31. } );
  32. describe( 'extendMixin', function() {
  33. it( 'should extend classes', function() {
  34. var utils = modules.utils;
  35. function Car( name ) {
  36. this.name = name;
  37. }
  38. Car.prototype.addGas = function() {};
  39. Car.extend = utils.extendMixin;
  40. var Truck = Car.extend( {
  41. loadContainers: function() {}
  42. } );
  43. var volvoTruck = new Truck( 'Volvo' );
  44. expect( volvoTruck ).to.be.an.instanceof( Truck );
  45. expect( volvoTruck ).to.be.an.instanceof( Car );
  46. expect( volvoTruck ).to.have.property( 'name' ).to.equals( 'Volvo' );
  47. expect( volvoTruck ).to.have.property( 'addGas' ).to.be.a( 'function' );
  48. expect( volvoTruck ).to.have.property( 'loadContainers' ).to.be.a( 'function' );
  49. var Spacecraft = Truck.extend( {
  50. jumpToHyperspace: function() {}
  51. } );
  52. var falcon = new Spacecraft( 'Millennium Falcon' );
  53. expect( falcon ).to.be.an.instanceof( Spacecraft );
  54. expect( falcon ).to.be.an.instanceof( Truck );
  55. expect( falcon ).to.be.an.instanceof( Car );
  56. expect( falcon ).to.have.property( 'name' ).to.equals( 'Millennium Falcon' );
  57. expect( falcon ).to.have.property( 'addGas' ).to.be.a( 'function' );
  58. expect( falcon ).to.have.property( 'loadContainers' ).to.be.a( 'function' );
  59. expect( falcon ).to.have.property( 'jumpToHyperspace' ).to.be.a( 'function' );
  60. } );
  61. it( 'should extend the prototype and add statics', function() {
  62. var utils = modules.utils;
  63. function Car() {}
  64. Car.extend = utils.extendMixin;
  65. var Truck = Car.extend( {
  66. property1: 1,
  67. property2: function() {}
  68. }, {
  69. static1: 1,
  70. static2: function() {}
  71. } );
  72. expect( Truck ).to.have.property( 'static1' ).to.equals( 1 );
  73. expect( Truck ).to.have.property( 'static2' ).to.be.a( 'function' );
  74. var truck = new Truck();
  75. expect( truck ).to.have.property( 'property1' ).to.equals( 1 );
  76. expect( truck ).to.have.property( 'property2' ).to.be.a( 'function' );
  77. } );
  78. it( 'should use a custom constructor', function() {
  79. var utils = modules.utils;
  80. function customConstructor() {}
  81. function Car() {}
  82. Car.extend = utils.extendMixin;
  83. var Truck = Car.extend( {
  84. constructor: customConstructor
  85. } );
  86. expect( Truck ).to.equals( customConstructor );
  87. expect( Truck.prototype ).to.not.have.ownProperty( 'constructor' );
  88. expect( new Truck() ).to.be.an.instanceof( Truck );
  89. expect( new Truck() ).to.be.an.instanceof( Car );
  90. } );
  91. } );
  92. describe( 'spy', function() {
  93. it( 'should not have `called` after creation', function() {
  94. var utils = modules.utils;
  95. var spy = utils.spy();
  96. expect( spy.called ).to.not.be.true();
  97. } );
  98. it( 'should register calls', function() {
  99. var utils = modules.utils;
  100. var fn1 = utils.spy();
  101. var fn2 = utils.spy();
  102. fn1();
  103. expect( fn1.called ).to.be.true();
  104. expect( fn2.called ).to.not.be.true();
  105. } );
  106. } );
  107. describe( 'uid', function() {
  108. it( 'should return different ids', function() {
  109. var utils = modules.utils;
  110. var id1 = utils.uid();
  111. var id2 = utils.uid();
  112. var id3 = utils.uid();
  113. expect( id1 ).to.be.a( 'number' );
  114. expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
  115. expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
  116. } );
  117. } );
  118. describe( 'Lo-Dash extensions', function() {
  119. // Ensures that the required Lo-Dash extensions are available in `utils`.
  120. it( 'should be exposed in utils', function() {
  121. var utils = modules.utils;
  122. var extensions = modules[ 'utils-lodash' ];
  123. extensions.forEach( function( extension ) {
  124. expect( utils ).to.have.property( extension ).to.not.be.undefined();
  125. } );
  126. } );
  127. } );