utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. } );
  79. describe( 'spy', function() {
  80. it( 'should not have `called` after creation', function() {
  81. var utils = modules.utils;
  82. var spy = utils.spy();
  83. expect( spy.called ).to.not.be.true();
  84. } );
  85. it( 'should register calls', function() {
  86. var utils = modules.utils;
  87. var fn1 = utils.spy();
  88. var fn2 = utils.spy();
  89. fn1();
  90. expect( fn1.called ).to.be.true();
  91. expect( fn2.called ).to.not.be.true();
  92. } );
  93. } );
  94. describe( 'uid', function() {
  95. it( 'should return different ids', function() {
  96. var utils = modules.utils;
  97. var id1 = utils.uid();
  98. var id2 = utils.uid();
  99. var id3 = utils.uid();
  100. expect( id1 ).to.be.a( 'number' );
  101. expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
  102. expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
  103. } );
  104. } );
  105. describe( 'Lo-Dash extensions', function() {
  106. // Ensures that the required Lo-Dash extensions are available in `utils`.
  107. it( 'should be exposed in utils', function() {
  108. var utils = modules.utils;
  109. var extensions = modules[ 'utils-lodash' ];
  110. extensions.forEach( function( extension ) {
  111. expect( utils ).to.have.property( extension ).to.not.be.undefined();
  112. } );
  113. } );
  114. } );