8
0

utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import utils from '/ckeditor5/core/utils.js';
  7. describe( 'utils', () => {
  8. describe( 'spy', () => {
  9. it( 'should not have `called` after creation', () => {
  10. let spy = utils.spy();
  11. expect( spy.called ).to.not.be.true();
  12. } );
  13. it( 'should register calls', () => {
  14. let fn1 = utils.spy();
  15. let fn2 = utils.spy();
  16. fn1();
  17. expect( fn1.called ).to.be.true();
  18. expect( fn2.called ).to.not.be.true();
  19. } );
  20. } );
  21. describe( 'uid', () => {
  22. it( 'should return different ids', () => {
  23. let id1 = utils.uid();
  24. let id2 = utils.uid();
  25. let id3 = utils.uid();
  26. expect( id1 ).to.be.a( 'number' );
  27. expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
  28. expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
  29. } );
  30. } );
  31. describe( 'isIterable', () => {
  32. it( 'should be true for string', () => {
  33. let string = 'foo';
  34. expect( utils.isIterable( string ) ).to.be.true;
  35. } );
  36. it( 'should be true for arrays', () => {
  37. let array = [ 1, 2, 3 ];
  38. expect( utils.isIterable( array ) ).to.be.true;
  39. } );
  40. it( 'should be true for iterable classes', () => {
  41. class IterableClass {
  42. constructor() {
  43. this.array = [ 1, 2, 3 ];
  44. }
  45. [ Symbol.iterator ]() {
  46. return this.array[ Symbol.iterator ]();
  47. }
  48. }
  49. let instance = new IterableClass();
  50. expect( utils.isIterable( instance ) ).to.be.true;
  51. } );
  52. it( 'should be false for not iterable objects', () => {
  53. let notIterable = { foo: 'bar' };
  54. expect( utils.isIterable( notIterable ) ).to.be.false;
  55. } );
  56. it( 'should be false for undefined', () => {
  57. expect( utils.isIterable() ).to.be.false;
  58. } );
  59. } );
  60. describe( 'compareArrays', () => {
  61. it( 'should return SAME flag, when arrays are same', () => {
  62. let a = [ 'abc', 0, 3 ];
  63. let b = [ 'abc', 0, 3 ];
  64. let result = utils.compareArrays( a, b );
  65. expect( result ).to.equal( 'SAME' );
  66. } );
  67. it( 'should return PREFIX flag, when all n elements of first array are same as n first elements of the second array', () => {
  68. let a = [ 'abc', 0 ];
  69. let b = [ 'abc', 0, 3 ];
  70. let result = utils.compareArrays( a, b );
  71. expect( result ).to.equal( 'PREFIX' );
  72. } );
  73. it( 'should return EXTENSION flag, when n first elements of first array are same as all elements of the second array', () => {
  74. let a = [ 'abc', 0, 3 ];
  75. let b = [ 'abc', 0 ];
  76. let result = utils.compareArrays( a, b );
  77. expect( result ).to.equal( 'EXTENSION' );
  78. } );
  79. it( 'should return index on which arrays differ, when arrays are not the same', () => {
  80. let a = [ 'abc', 0, 3 ];
  81. let b = [ 'abc', 1, 3 ];
  82. let result = utils.compareArrays( a, b );
  83. expect( result ).to.equal( 1 );
  84. } );
  85. } );
  86. describe( 'nth', () => {
  87. it( 'should return 0th item', () => {
  88. expect( utils.nth( 0, getIterator() ) ).to.equal( 11 );
  89. } );
  90. it( 'should return the last item', () => {
  91. expect( utils.nth( 2, getIterator() ) ).to.equal( 33 );
  92. } );
  93. it( 'should return null if out of range (bottom)', () => {
  94. expect( utils.nth( -1, getIterator() ) ).to.be.null;
  95. } );
  96. it( 'should return null if out of range (top)', () => {
  97. expect( utils.nth( 3, getIterator() ) ).to.be.null;
  98. } );
  99. it( 'should return null if iterator is empty', () => {
  100. expect( utils.nth( 0, [] ) ).to.be.null;
  101. } );
  102. function *getIterator() {
  103. yield 11;
  104. yield 22;
  105. yield 33;
  106. }
  107. } );
  108. } );