| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- 'use strict';
- const modules = bender.amd.require( 'core/utils' );
- describe( 'utils', () => {
- let utils;
- before( () => {
- utils = modules[ 'core/utils' ];
- } );
- describe( 'spy', () => {
- it( 'should not have `called` after creation', () => {
- let spy = utils.spy();
- expect( spy.called ).to.not.be.true();
- } );
- it( 'should register calls', () => {
- let fn1 = utils.spy();
- let fn2 = utils.spy();
- fn1();
- expect( fn1.called ).to.be.true();
- expect( fn2.called ).to.not.be.true();
- } );
- } );
- describe( 'uid', () => {
- it( 'should return different ids', () => {
- let id1 = utils.uid();
- let id2 = utils.uid();
- let id3 = utils.uid();
- expect( id1 ).to.be.a( 'number' );
- expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
- expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
- } );
- } );
- describe( 'isIterable', () => {
- it( 'should be true for string', () => {
- let string = 'foo';
- expect( utils.isIterable( string ) ).to.be.true;
- } );
- it( 'should be true for arrays', () => {
- let array = [ 1, 2, 3 ];
- expect( utils.isIterable( array ) ).to.be.true;
- } );
- it( 'should be true for iterable classes', () => {
- class IterableClass {
- constructor() {
- this.array = [ 1, 2, 3 ];
- }
- [ Symbol.iterator ]() {
- return this.array[ Symbol.iterator ]();
- }
- }
- let instance = new IterableClass();
- expect( utils.isIterable( instance ) ).to.be.true;
- } );
- it( 'should be false for not iterable objects', () => {
- let notIterable = { foo: 'bar' };
- expect( utils.isIterable( notIterable ) ).to.be.false;
- } );
- it( 'should be false for undefined', () => {
- expect( utils.isIterable() ).to.be.false;
- } );
- } );
- describe( 'compareArrays', () => {
- it( 'should return SAME flag, when arrays are same', () => {
- let a = [ 'abc', 0, 3 ];
- let b = [ 'abc', 0, 3 ];
- let result = utils.compareArrays( a, b );
- expect( result ).to.equal( utils.compareArrays.SAME );
- } );
- it( 'should return PREFIX flag, when all n elements of first array are same as n first elements of the second array', () => {
- let a = [ 'abc', 0 ];
- let b = [ 'abc', 0, 3 ];
- let result = utils.compareArrays( a, b );
- expect( result ).to.equal( utils.compareArrays.PREFIX );
- } );
- it( 'should return EXTENSION flag, when n first elements of first array are same as all elements of the second array', () => {
- let a = [ 'abc', 0, 3 ];
- let b = [ 'abc', 0 ];
- let result = utils.compareArrays( a, b );
- expect( result ).to.equal( utils.compareArrays.EXTENSION );
- } );
- it( 'should return index on which arrays differ, when arrays are not the same', () => {
- let a = [ 'abc', 0, 3 ];
- let b = [ 'abc', 1, 3 ];
- let result = utils.compareArrays( a, b );
- expect( result ).to.equal( 1 );
- } );
- } );
- describe( 'nth', () => {
- it( 'should return 0th item', () => {
- expect( utils.nth( 0, getIterator() ) ).to.equal( 11 );
- } );
- it( 'should return the last item', () => {
- expect( utils.nth( 2, getIterator() ) ).to.equal( 33 );
- } );
- it( 'should return null if out of range (bottom)', () => {
- expect( utils.nth( -1, getIterator() ) ).to.be.null;
- } );
- it( 'should return null if out of range (top)', () => {
- expect( utils.nth( 3, getIterator() ) ).to.be.null;
- } );
- it( 'should return null if iterator is empty', () => {
- expect( utils.nth( 0, [] ) ).to.be.null;
- } );
- function *getIterator() {
- yield 11;
- yield 22;
- yield 33;
- }
- } );
- } );
|