utils.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. describe( 'mix', () => {
  109. const MixinA = {
  110. a() {
  111. return 'a';
  112. }
  113. };
  114. const MixinB = {
  115. b() {
  116. return 'b';
  117. }
  118. };
  119. it( 'mixes 2nd+ argument\'s properties into the first class', () => {
  120. class Foo {}
  121. utils.mix( Foo, MixinA, MixinB );
  122. expect( Foo ).to.not.have.property( 'a' );
  123. expect( Foo ).to.not.have.property( 'b' );
  124. const foo = new Foo();
  125. expect( foo.a() ).to.equal( 'a' );
  126. expect( foo.b() ).to.equal( 'b' );
  127. } );
  128. it( 'does not break the instanceof operator', () => {
  129. class Foo {}
  130. utils.mix( Foo, MixinA );
  131. let foo = new Foo();
  132. expect( foo ).to.be.instanceof( Foo );
  133. } );
  134. it( 'defines properties with the same descriptors as native classes', () => {
  135. class Foo {
  136. foo() {}
  137. }
  138. utils.mix( Foo, MixinA );
  139. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'a' );
  140. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'foo' );
  141. expect( actualDescriptor ).to.have.property( 'writable', expectedDescriptor.writable );
  142. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  143. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  144. } );
  145. it( 'copies setters and getters (with descriptors as of native classes)', () => {
  146. class Foo {
  147. set foo( v ) {
  148. this._foo = v;
  149. }
  150. get foo() {}
  151. }
  152. const Mixin = {
  153. set a( v ) {
  154. this._a = v;
  155. },
  156. get a() {
  157. return this._a;
  158. }
  159. };
  160. utils.mix( Foo, Mixin );
  161. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'a' );
  162. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'foo' );
  163. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  164. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  165. const foo = new Foo();
  166. foo.a = 1;
  167. expect( foo._a ).to.equal( 1 );
  168. foo._a = 2;
  169. expect( foo.a ).to.equal( 2 );
  170. } );
  171. it( 'copies symbols (with descriptors as of native classes)', () => {
  172. const symbolA = Symbol( 'a' );
  173. const symbolFoo = Symbol( 'foo' );
  174. // https://github.com/jscs-dev/node-jscs/issues/2078
  175. // jscs:disable disallowSpacesInFunction
  176. class Foo {
  177. [ symbolFoo ]() {
  178. return 'foo';
  179. }
  180. }
  181. const Mixin = {
  182. [ symbolA ]() {
  183. return 'a';
  184. }
  185. };
  186. // jscs:enable disallowSpacesInFunction
  187. utils.mix( Foo, Mixin );
  188. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, symbolA );
  189. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, symbolFoo );
  190. expect( actualDescriptor ).to.have.property( 'writable', expectedDescriptor.writable );
  191. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  192. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  193. const foo = new Foo();
  194. expect( foo[ symbolA ]() ).to.equal( 'a' );
  195. } );
  196. it( 'does not copy already existing properties', () => {
  197. class Foo {
  198. a() {
  199. return 'foo';
  200. }
  201. }
  202. utils.mix( Foo, MixinA, MixinB );
  203. const foo = new Foo();
  204. expect( foo.a() ).to.equal( 'foo' );
  205. expect( foo.b() ).to.equal( 'b' );
  206. } );
  207. it( 'does not copy already existing properties - properties deep in the proto chain', () => {
  208. class Foo {
  209. a() {
  210. return 'foo';
  211. }
  212. }
  213. class Bar extends Foo {}
  214. utils.mix( Bar, MixinA, MixinB );
  215. const bar = new Bar();
  216. expect( bar.a() ).to.equal( 'foo' );
  217. expect( bar.b() ).to.equal( 'b' );
  218. } );
  219. } );
  220. } );