utils.js 4.0 KB

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