Home Reference Source Test Repository

buildDocs/throttle.spec.js

import * as tslib_1 from "tslib";
import { expect } from 'chai';
import { spy } from 'sinon';
import { Throttle, ThrottleSetter, ThrottleGetter } from './throttle';
describe('throttle', () => {
    it('should throttle the method', done => {
        let _spy = spy();
        class MyClass {
            fn(n) {
                _spy();
            }
        }
        tslib_1.__decorate([
            Throttle(10)
        ], MyClass.prototype, "fn", null);
        const myClass = new MyClass();
        myClass.fn(1);
        myClass.fn(2);
        setTimeout(() => myClass.fn(3), 1);
        setTimeout(() => myClass.fn(4), 2);
        setTimeout(() => {
            expect(_spy.callCount).to.equal(2);
            done();
        }, 20);
    });
    it('should debounce the property setter', done => {
        class MyClass {
            constructor() {
                this._value = 100;
            }
            set value(value) {
                this._value = value;
            }
            get value() {
                return this._value;
            }
        }
        tslib_1.__decorate([
            ThrottleSetter(10)
        ], MyClass.prototype, "value", null);
        const myClass = new MyClass();
        myClass.value = 5;
        myClass.value = 15;
        setTimeout(() => myClass.value = 20, 5);
        expect(myClass.value).to.equal(5);
        setTimeout(() => {
            expect(myClass.value).to.equal(20);
            done();
        }, 11);
    });
    it('should debounce the property getter', done => {
        class MyClass {
            constructor() {
                this._value = 0;
            }
            get value() {
                return ++this._value;
            }
        }
        tslib_1.__decorate([
            ThrottleGetter(10)
        ], MyClass.prototype, "value", null);
        const myClass = new MyClass();
        expect(myClass.value).to.equal(1);
        expect(myClass.value).to.equal(1);
        setTimeout(() => expect(myClass.value).to.equal(1), 5);
        setTimeout(() => {
            expect(myClass.value).to.equal(2);
            done();
        }, 11);
    });
    it('should contain the cancel and flush methods', () => {
        class MyClass {
            fn() { }
        }
        tslib_1.__decorate([
            Throttle(10)
        ], MyClass.prototype, "fn", null);
        const myClass = new MyClass();
        expect(myClass.fn.cancel).to.be.a('function');
        expect(myClass.fn.flush).to.be.a('function');
    });
});