8
0

utils.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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( 'mapsEqual', () => {
  87. it( 'should return true if maps have exactly same entries (order of adding does not matter)', () => {
  88. let mapA = new Map();
  89. let mapB = new Map();
  90. mapA.set( 'foo', 'bar' );
  91. mapA.set( 'abc', 'xyz' );
  92. mapB.set( 'abc', 'xyz' );
  93. mapB.set( 'foo', 'bar' );
  94. expect( utils.mapsEqual( mapA, mapB ) ).to.be.true;
  95. } );
  96. } );
  97. describe( 'nth', () => {
  98. it( 'should return 0th item', () => {
  99. expect( utils.nth( 0, getIterator() ) ).to.equal( 11 );
  100. } );
  101. it( 'should return the last item', () => {
  102. expect( utils.nth( 2, getIterator() ) ).to.equal( 33 );
  103. } );
  104. it( 'should return null if out of range (bottom)', () => {
  105. expect( utils.nth( -1, getIterator() ) ).to.be.null;
  106. } );
  107. it( 'should return null if out of range (top)', () => {
  108. expect( utils.nth( 3, getIterator() ) ).to.be.null;
  109. } );
  110. it( 'should return null if iterator is empty', () => {
  111. expect( utils.nth( 0, [] ) ).to.be.null;
  112. } );
  113. function *getIterator() {
  114. yield 11;
  115. yield 22;
  116. yield 33;
  117. }
  118. } );
  119. describe( 'mix', () => {
  120. const MixinA = {
  121. a() {
  122. return 'a';
  123. }
  124. };
  125. const MixinB = {
  126. b() {
  127. return 'b';
  128. }
  129. };
  130. it( 'mixes 2nd+ argument\'s properties into the first class', () => {
  131. class Foo {}
  132. utils.mix( Foo, MixinA, MixinB );
  133. expect( Foo ).to.not.have.property( 'a' );
  134. expect( Foo ).to.not.have.property( 'b' );
  135. const foo = new Foo();
  136. expect( foo.a() ).to.equal( 'a' );
  137. expect( foo.b() ).to.equal( 'b' );
  138. } );
  139. it( 'does not break the instanceof operator', () => {
  140. class Foo {}
  141. utils.mix( Foo, MixinA );
  142. let foo = new Foo();
  143. expect( foo ).to.be.instanceof( Foo );
  144. } );
  145. it( 'defines properties with the same descriptors as native classes', () => {
  146. class Foo {
  147. foo() {}
  148. }
  149. utils.mix( Foo, MixinA );
  150. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'a' );
  151. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'foo' );
  152. expect( actualDescriptor ).to.have.property( 'writable', expectedDescriptor.writable );
  153. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  154. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  155. } );
  156. it( 'copies setters and getters (with descriptors as of native classes)', () => {
  157. class Foo {
  158. set foo( v ) {
  159. this._foo = v;
  160. }
  161. get foo() {}
  162. }
  163. const Mixin = {
  164. set a( v ) {
  165. this._a = v;
  166. },
  167. get a() {
  168. return this._a;
  169. }
  170. };
  171. utils.mix( Foo, Mixin );
  172. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'a' );
  173. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'foo' );
  174. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  175. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  176. const foo = new Foo();
  177. foo.a = 1;
  178. expect( foo._a ).to.equal( 1 );
  179. foo._a = 2;
  180. expect( foo.a ).to.equal( 2 );
  181. } );
  182. it( 'copies symbols (with descriptors as of native classes)', () => {
  183. const symbolA = Symbol( 'a' );
  184. const symbolFoo = Symbol( 'foo' );
  185. // https://github.com/jscs-dev/node-jscs/issues/2078
  186. // jscs:disable disallowSpacesInFunction
  187. class Foo {
  188. [ symbolFoo ]() {
  189. return 'foo';
  190. }
  191. }
  192. const Mixin = {
  193. [ symbolA ]() {
  194. return 'a';
  195. }
  196. };
  197. // jscs:enable disallowSpacesInFunction
  198. utils.mix( Foo, Mixin );
  199. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, symbolA );
  200. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, symbolFoo );
  201. expect( actualDescriptor ).to.have.property( 'writable', expectedDescriptor.writable );
  202. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  203. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  204. const foo = new Foo();
  205. expect( foo[ symbolA ]() ).to.equal( 'a' );
  206. } );
  207. it( 'does not copy already existing properties', () => {
  208. class Foo {
  209. a() {
  210. return 'foo';
  211. }
  212. }
  213. utils.mix( Foo, MixinA, MixinB );
  214. const foo = new Foo();
  215. expect( foo.a() ).to.equal( 'foo' );
  216. expect( foo.b() ).to.equal( 'b' );
  217. } );
  218. it( 'does not copy already existing properties - properties deep in the proto chain', () => {
  219. class Foo {
  220. a() {
  221. return 'foo';
  222. }
  223. }
  224. class Bar extends Foo {}
  225. utils.mix( Bar, MixinA, MixinB );
  226. const bar = new Bar();
  227. expect( bar.a() ).to.equal( 'foo' );
  228. expect( bar.b() ).to.equal( 'b' );
  229. } );
  230. } );
  231. } );