utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. } );
  62. describe( 'spy', function() {
  63. it( 'should not have `called` after creation', function() {
  64. var utils = modules.utils;
  65. var spy = utils.spy();
  66. expect( spy.called ).to.not.be.true();
  67. } );
  68. it( 'should register calls', function() {
  69. var utils = modules.utils;
  70. var fn1 = utils.spy();
  71. var fn2 = utils.spy();
  72. fn1();
  73. expect( fn1.called ).to.be.true();
  74. expect( fn2.called ).to.not.be.true();
  75. } );
  76. } );
  77. describe( 'uid', function() {
  78. it( 'should return different ids', function() {
  79. var utils = modules.utils;
  80. var id1 = utils.uid();
  81. var id2 = utils.uid();
  82. var id3 = utils.uid();
  83. expect( id1 ).to.be.a( 'number' );
  84. expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
  85. expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
  86. } );
  87. } );
  88. describe( 'Lo-Dash extensions', function() {
  89. // Ensures that the required Lo-Dash extensions are available in `utils`.
  90. it( 'should be exposed in utils', function() {
  91. var utils = modules.utils;
  92. var extensions = modules[ 'utils-lodash' ];
  93. extensions.forEach( function( extension ) {
  94. expect( utils ).to.have.property( extension ).to.not.be.undefined();
  95. } );
  96. } );
  97. } );