utils.js 3.6 KB

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