utils.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/utils/utils.js';
  7. const count = utils.count;
  8. describe( 'utils', () => {
  9. describe( 'spy', () => {
  10. it( 'should not have `called` after creation', () => {
  11. let spy = utils.spy();
  12. expect( spy.called ).to.not.be.true();
  13. } );
  14. it( 'should register calls', () => {
  15. let fn1 = utils.spy();
  16. let fn2 = utils.spy();
  17. fn1();
  18. expect( fn1.called ).to.be.true();
  19. expect( fn2.called ).to.not.be.true();
  20. } );
  21. } );
  22. describe( 'uid', () => {
  23. it( 'should return different ids', () => {
  24. let id1 = utils.uid();
  25. let id2 = utils.uid();
  26. let id3 = utils.uid();
  27. expect( id1 ).to.be.a( 'number' );
  28. expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
  29. expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
  30. } );
  31. } );
  32. describe( 'isIterable', () => {
  33. it( 'should be true for string', () => {
  34. let string = 'foo';
  35. expect( utils.isIterable( string ) ).to.be.true;
  36. } );
  37. it( 'should be true for arrays', () => {
  38. let array = [ 1, 2, 3 ];
  39. expect( utils.isIterable( array ) ).to.be.true;
  40. } );
  41. it( 'should be true for iterable classes', () => {
  42. class IterableClass {
  43. constructor() {
  44. this.array = [ 1, 2, 3 ];
  45. }
  46. [ Symbol.iterator ]() {
  47. return this.array[ Symbol.iterator ]();
  48. }
  49. }
  50. let instance = new IterableClass();
  51. expect( utils.isIterable( instance ) ).to.be.true;
  52. } );
  53. it( 'should be false for not iterable objects', () => {
  54. let notIterable = { foo: 'bar' };
  55. expect( utils.isIterable( notIterable ) ).to.be.false;
  56. } );
  57. it( 'should be false for undefined', () => {
  58. expect( utils.isIterable() ).to.be.false;
  59. } );
  60. } );
  61. describe( 'compareArrays', () => {
  62. it( 'should return SAME flag, when arrays are same', () => {
  63. let a = [ 'abc', 0, 3 ];
  64. let b = [ 'abc', 0, 3 ];
  65. let result = utils.compareArrays( a, b );
  66. expect( result ).to.equal( 'SAME' );
  67. } );
  68. it( 'should return PREFIX flag, when all n elements of first array are same as n first elements of the second array', () => {
  69. let a = [ 'abc', 0 ];
  70. let b = [ 'abc', 0, 3 ];
  71. let result = utils.compareArrays( a, b );
  72. expect( result ).to.equal( 'PREFIX' );
  73. } );
  74. it( 'should return EXTENSION flag, when n first elements of first array are same as all elements of the second array', () => {
  75. let a = [ 'abc', 0, 3 ];
  76. let b = [ 'abc', 0 ];
  77. let result = utils.compareArrays( a, b );
  78. expect( result ).to.equal( 'EXTENSION' );
  79. } );
  80. it( 'should return index on which arrays differ, when arrays are not the same', () => {
  81. let a = [ 'abc', 0, 3 ];
  82. let b = [ 'abc', 1, 3 ];
  83. let result = utils.compareArrays( a, b );
  84. expect( result ).to.equal( 1 );
  85. } );
  86. } );
  87. describe( 'toMap', () => {
  88. it( 'should create map from object', () => {
  89. const map = utils.toMap( { foo: 1, bar: 2 } );
  90. expect( count( map ) ).to.equal( 2 );
  91. expect( map.get( 'foo' ) ).to.equal( 1 );
  92. expect( map.get( 'bar' ) ).to.equal( 2 );
  93. } );
  94. it( 'should create map from iterator', () => {
  95. const map = utils.toMap( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
  96. expect( count( map ) ).to.equal( 2 );
  97. expect( map.get( 'foo' ) ).to.equal( 1 );
  98. expect( map.get( 'bar' ) ).to.equal( 2 );
  99. } );
  100. it( 'should create map from another map', () => {
  101. const data = new Map( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
  102. const map = utils.toMap( data );
  103. expect( count( map ) ).to.equal( 2 );
  104. expect( map.get( 'foo' ) ).to.equal( 1 );
  105. expect( map.get( 'bar' ) ).to.equal( 2 );
  106. } );
  107. } );
  108. describe( 'mapsEqual', () => {
  109. it( 'should return true if maps have exactly same entries (order of adding does not matter)', () => {
  110. let mapA = new Map();
  111. let mapB = new Map();
  112. mapA.set( 'foo', 'bar' );
  113. mapA.set( 'abc', 'xyz' );
  114. mapB.set( 'abc', 'xyz' );
  115. mapB.set( 'foo', 'bar' );
  116. expect( utils.mapsEqual( mapA, mapB ) ).to.be.true;
  117. } );
  118. } );
  119. describe( 'nth', () => {
  120. it( 'should return 0th item', () => {
  121. expect( utils.nth( 0, getIterator() ) ).to.equal( 11 );
  122. } );
  123. it( 'should return the last item', () => {
  124. expect( utils.nth( 2, getIterator() ) ).to.equal( 33 );
  125. } );
  126. it( 'should return null if out of range (bottom)', () => {
  127. expect( utils.nth( -1, getIterator() ) ).to.be.null;
  128. } );
  129. it( 'should return null if out of range (top)', () => {
  130. expect( utils.nth( 3, getIterator() ) ).to.be.null;
  131. } );
  132. it( 'should return null if iterator is empty', () => {
  133. expect( utils.nth( 0, [] ) ).to.be.null;
  134. } );
  135. function *getIterator() {
  136. yield 11;
  137. yield 22;
  138. yield 33;
  139. }
  140. } );
  141. describe( 'count', () => {
  142. it( 'should returns number of editable items', () => {
  143. const count = utils.count( [ 1, 2, 3, 4, 5 ] );
  144. expect( count ).to.equal( 5 );
  145. } );
  146. } );
  147. describe( 'mix', () => {
  148. const MixinA = {
  149. a() {
  150. return 'a';
  151. }
  152. };
  153. const MixinB = {
  154. b() {
  155. return 'b';
  156. }
  157. };
  158. it( 'mixes 2nd+ argument\'s properties into the first class', () => {
  159. class Foo {}
  160. utils.mix( Foo, MixinA, MixinB );
  161. expect( Foo ).to.not.have.property( 'a' );
  162. expect( Foo ).to.not.have.property( 'b' );
  163. const foo = new Foo();
  164. expect( foo.a() ).to.equal( 'a' );
  165. expect( foo.b() ).to.equal( 'b' );
  166. } );
  167. it( 'does not break the instanceof operator', () => {
  168. class Foo {}
  169. utils.mix( Foo, MixinA );
  170. let foo = new Foo();
  171. expect( foo ).to.be.instanceof( Foo );
  172. } );
  173. it( 'defines properties with the same descriptors as native classes', () => {
  174. class Foo {
  175. foo() {}
  176. }
  177. utils.mix( Foo, MixinA );
  178. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'a' );
  179. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'foo' );
  180. expect( actualDescriptor ).to.have.property( 'writable', expectedDescriptor.writable );
  181. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  182. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  183. } );
  184. it( 'copies setters and getters (with descriptors as of native classes)', () => {
  185. class Foo {
  186. set foo( v ) {
  187. this._foo = v;
  188. }
  189. get foo() {}
  190. }
  191. const Mixin = {
  192. set a( v ) {
  193. this._a = v;
  194. },
  195. get a() {
  196. return this._a;
  197. }
  198. };
  199. utils.mix( Foo, Mixin );
  200. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'a' );
  201. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'foo' );
  202. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  203. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  204. const foo = new Foo();
  205. foo.a = 1;
  206. expect( foo._a ).to.equal( 1 );
  207. foo._a = 2;
  208. expect( foo.a ).to.equal( 2 );
  209. } );
  210. it( 'copies symbols (with descriptors as of native classes)', () => {
  211. const symbolA = Symbol( 'a' );
  212. const symbolFoo = Symbol( 'foo' );
  213. // https://github.com/jscs-dev/node-jscs/issues/2078
  214. // jscs:disable disallowSpacesInFunction
  215. class Foo {
  216. [ symbolFoo ]() {
  217. return 'foo';
  218. }
  219. }
  220. const Mixin = {
  221. [ symbolA ]() {
  222. return 'a';
  223. }
  224. };
  225. // jscs:enable disallowSpacesInFunction
  226. utils.mix( Foo, Mixin );
  227. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, symbolA );
  228. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, symbolFoo );
  229. expect( actualDescriptor ).to.have.property( 'writable', expectedDescriptor.writable );
  230. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  231. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  232. const foo = new Foo();
  233. expect( foo[ symbolA ]() ).to.equal( 'a' );
  234. } );
  235. it( 'does not copy already existing properties', () => {
  236. class Foo {
  237. a() {
  238. return 'foo';
  239. }
  240. }
  241. utils.mix( Foo, MixinA, MixinB );
  242. const foo = new Foo();
  243. expect( foo.a() ).to.equal( 'foo' );
  244. expect( foo.b() ).to.equal( 'b' );
  245. } );
  246. it( 'does not copy already existing properties - properties deep in the proto chain', () => {
  247. class Foo {
  248. a() {
  249. return 'foo';
  250. }
  251. }
  252. class Bar extends Foo {}
  253. utils.mix( Bar, MixinA, MixinB );
  254. const bar = new Bar();
  255. expect( bar.a() ).to.equal( 'foo' );
  256. expect( bar.b() ).to.equal( 'b' );
  257. } );
  258. } );
  259. } );