utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. var modules = bender.amd.require( 'utils', 'utils-lodash' );
  7. describe( 'extend()', function() {
  8. // Properties of the subsequent objects should override properties of the preceding objects. This is critical for
  9. // CKEditor so we keep this test to ensure that Lo-Dash (or whatever) implements it in the way we need it.
  10. it( 'should extend by several params in the correct order', function() {
  11. var utils = modules.utils;
  12. var target = {
  13. a: 0,
  14. b: 0
  15. };
  16. var ext1 = {
  17. b: 1,
  18. c: 1
  19. };
  20. var ext2 = {
  21. c: 2,
  22. d: 2
  23. };
  24. utils.extend( target, ext1, ext2 );
  25. expect( target ).to.have.property( 'a' ).to.equal( 0 );
  26. expect( target ).to.have.property( 'b' ).to.equal( 1 );
  27. expect( target ).to.have.property( 'c' ).to.equal( 2 );
  28. expect( target ).to.have.property( 'd' ).to.equal( 2 );
  29. } );
  30. } );
  31. describe( 'spy', function() {
  32. it( 'should not have `called` after creation', function() {
  33. var utils = modules.utils;
  34. var spy = utils.spy();
  35. expect( spy.called ).to.not.be.true();
  36. } );
  37. it( 'should register calls', function() {
  38. var utils = modules.utils;
  39. var fn1 = utils.spy();
  40. var fn2 = utils.spy();
  41. fn1();
  42. expect( fn1.called ).to.be.true();
  43. expect( fn2.called ).to.not.be.true();
  44. } );
  45. } );
  46. describe( 'uid', function() {
  47. it( 'should return different ids', function() {
  48. var utils = modules.utils;
  49. var id1 = utils.uid();
  50. var id2 = utils.uid();
  51. var id3 = utils.uid();
  52. expect( id1 ).to.be.a( 'number' );
  53. expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
  54. expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
  55. } );
  56. } );
  57. describe( 'isIterable', function() {
  58. it( 'should be true for string', function() {
  59. var utils = modules.utils;
  60. var string = 'foo';
  61. expect( utils.isIterable( string ) ).to.be.true;
  62. } );
  63. it( 'should be true for arrays', function() {
  64. var utils = modules.utils;
  65. var array = [ 1, 2, 3 ];
  66. expect( utils.isIterable( array ) ).to.be.true;
  67. } );
  68. it( 'should be true for iterable classes', function() {
  69. var utils = modules.utils;
  70. class IterableClass {
  71. constructor() {
  72. this.array = [ 1, 2, 3 ];
  73. }
  74. [ Symbol.iterator ]() {
  75. return this.array[ Symbol.iterator ]();
  76. }
  77. }
  78. var instance = new IterableClass();
  79. expect( utils.isIterable( instance ) ).to.be.true;
  80. } );
  81. it( 'should be false for not iterable objects', function() {
  82. var utils = modules.utils;
  83. var notIterable = { foo: 'bar' };
  84. expect( utils.isIterable( notIterable ) ).to.be.false;
  85. } );
  86. it( 'should be false for undefined', function() {
  87. var utils = modules.utils;
  88. expect( utils.isIterable() ).to.be.false;
  89. } );
  90. } );
  91. describe( 'Lo-Dash extensions', function() {
  92. // Ensures that the required Lo-Dash extensions are available in `utils`.
  93. it( 'should be exposed in utils', function() {
  94. var utils = modules.utils;
  95. var extensions = modules[ 'utils-lodash' ];
  96. extensions.forEach( function( extension ) {
  97. expect( utils ).to.have.property( extension ).to.not.be.undefined();
  98. } );
  99. } );
  100. } );