class EventsTester extends Leaf.EventEmitter<"test"> {
    count: number = 0;
    constructor() {
        super();
        this.handleTest = this.handleTest.bind(this);
    }
    bindTest() {
        this.on("test", this.handleTest);
    }
    unbindTest() {
        this.removeEventListener("test");
    }
    test() {
        this.emit("test");
    }
    reset() {
        this.count = 0;
        this.unbindTest();
    }
    handleTest() {
        this.count = this.count + 1;
    }
}

export function run() {
    describe("Events", function () {
        let expect = chai.expect;
        let assert = chai.assert;
        let et = new EventsTester();
        describe("# on() & emit()", () => {
            it("cout should be 1 ", () => {
                et.bindTest();
                et.test();
                expect(et.count).to.equal(1);
                et.reset();
            })
        })
        describe("# removeEventListener()", () => {
            it("count should be 2", () => {
                function handleTest() {
                    et.handleTest();
                }
                et.bindTest();
                et.test();
                et.removeEventListener("test");
                et.test();
                et.on("test", handleTest);
                et.test();
                et.removeEventListener("test", handleTest);
                et.test();
                expect(et.count).to.equal(2);
                et.reset();
            })
        })
        describe("# addEventListener()", () => {
            it("count should be 1", () => {
                et.addEventListener("test", et.handleTest);
                et.test();
                expect(et.count).to.equal(1);
                et.reset();
            })
        })
        describe("# once()", () => {
            it("count should be 1", () => {
                et.once("test", et.handleTest);
                et.test();
                et.test();
                expect(et.count).to.equal(1);
                et.reset();
            })
        })
        describe("# listenBy()", () => {
            let obj = {};
            it("count should be 1", () => {
                et.listenBy(obj, "test", et.handleTest);
                et.test();
                expect(et.count).to.equal(1);
                et.reset();
            })
        })
        describe("# listenByOnce()", () => {
            let obj = {};
            it("count should be 1", () => {
                et.listenByOnce(obj, "test", et.handleTest);
                et.test();
                et.test();
                expect(et.count).to.equal(1);
                et.reset();
            })
        })
        describe("# stopListenBy()", () => {
            let obj = {};
            it("count should be 3", () => {
                function handleTest() {
                    et.handleTest();
                }
                et.listenBy(obj, "test", handleTest);
                et.test();
                et.stopListenBy(obj);
                et.test();
                et.listenBy(obj, "test", handleTest);
                et.test();
                et.stopListenBy(obj, "test");
                et.test();
                et.listenBy(obj, "test", handleTest);
                et.test();
                et.stopListenBy(obj, "test", handleTest);
                et.test();
                expect(et.count).to.equal(3);
                et.reset();
            })
        })
    })
}
