all files / test/unit/base/core/ func.spec.js

100% Statements 75/75
100% Branches 0/0
100% Functions 35/35
100% Lines 75/75
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140                                                                                                                                 
/**
 * func.spec.js
 * (c) 2013~ Alan Hong
 * summernote may be freely distributed under the MIT license./
 */
import chai from 'chai';
import func from '../../../../src/js/base/core/func';
 
var expect = chai.expect;
 
describe('base:core.func', () => {
  describe('eq', () => {
    it('should return true if two values are same', () => {
      expect(func.eq(1)(1)).to.be.ok;
    });
  });
 
  describe('eq2', () => {
    it('should return true if two values are same', () => {
      expect(func.eq2(1, 1)).to.be.ok;
    });
 
    it('should return false if two values are not same', () => {
      expect(func.eq2(1, '1')).to.be.not.ok;
    });
  });
 
  describe('peq2', () => {
    it('should return true when two properties are same', () => {
      expect(func.peq2('prop')({ prop: 'hello' }, { prop: 'hello' })).to.be.ok;
    });
 
    it('should return false when two properties are not same', () => {
      expect(func.peq2('prop')({ prop: 'hello' }, { prop: 'world' })).to.be.not.ok;
    });
  });
 
  describe('ok', () => {
    it('should return true', () => {
      expect(func.ok()).to.be.ok;
    });
  });
 
  describe('fail', () => {
    it('should return false', () => {
      expect(func.fail()).to.be.not.ok;
    });
  });
 
  describe('not', () => {
    it('should return opposite function', () => {
      expect(func.not(func.ok)()).to.be.not.ok;
      expect(func.not(func.fail)()).to.be.ok;
    });
  });
 
  describe('and', () => {
    it('should return composite function', () => {
      expect(func.and(func.ok, func.ok)()).to.be.ok;
      expect(func.and(func.fail, func.ok)()).to.be.not.ok;
      expect(func.and(func.fail, func.fail)()).to.be.not.ok;
    });
  });
 
  describe('invoke', () => {
    it('should return function which invoke the method', () => {
      expect(func.invoke(func, 'ok')()).to.be.ok;
      expect(func.invoke(func, 'fail')()).to.be.not.ok;
    });
  });
 
  describe('uniqueId', () => {
    it('should return uniqueId with the prefix as a parameter', () => {
      expect(func.uniqueId('note-')).to.be.equal('note-1');
      expect(func.uniqueId('note-')).to.be.equal('note-2');
      expect(func.uniqueId('note-')).to.be.equal('note-3');
    });
  });
 
  describe('invertObject', () => {
    it('should return inverted object between keys and values', () => {
      expect(func.invertObject({ keyA: 'valueA', keyB: 'valueB' }))
        .to.deep.equal({ valueA: 'keyA', valueB: 'keyB' });
    });
  });
 
  describe('namespaceToCamel', () => {
    it('should return camelcase text', () => {
      expect(func.namespaceToCamel('upload.image')).to.be.equal('UploadImage');
    });
 
    it('should return prefixed camelcase text', () => {
      expect(func.namespaceToCamel('upload.image', 'summernote')).to.be.equal('summernoteUploadImage');
    });
  });
 
  describe('debounce', () => {
    it('shouldnt execute immediately', () => {
      var hasHappened = false;
      var fn = func.debounce(() => {
        hasHappened = true;
      }, 100);
 
      expect(hasHappened).to.be.false;
      fn();
      expect(hasHappened).to.be.false;
    });
 
    it('should execute after delay', (done) => {
      var hasHappened = false;
      var fn = func.debounce(() => {
        hasHappened = true;
      }, 100);
 
      fn();
 
      setTimeout(() => {
        expect(hasHappened).to.be.true;
        done();
      }, 101);
    });
 
    it('should only happen once', (done) => {
      var count = 0;
      var fn = func.debounce(() => {
        count += 1;
      }, 100);
 
      fn();
      fn();
      fn();
 
      setTimeout(() => {
        expect(count).to.be.equal(1);
        done();
      }, 101);
    });
  });
});