"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const callback_manager_1 = require("./callback-manager");
let sut;
describe('CallbackManager', () => {
    beforeEach(() => {
        sut = new callback_manager_1.CallbackManager();
    });
    it('Returns an incrementing uniqueId', () => {
        const id1 = sut.uniqueIdValue;
        expect(id1).toBe(1001);
        const id2 = sut.uniqueIdValue;
        expect(id2).toBe(1002);
    });
    it('normalises ids', () => {
        expect(callback_manager_1.CallbackManager.normaliseId(10)).toBe(10);
        expect(callback_manager_1.CallbackManager.normaliseId('hi')).toBe('HI');
    });
    it('adds a handler', () => {
        const rVal = sut.addHandler('test1', (id, message, userData) => { }, 'userData');
        expect(rVal).toBe('TEST1');
    });
    it('adds multiple handlers', () => {
        const rVal = sut.addHandler('test1', (id, message, userData) => { }, 'userData');
        expect(rVal).toBe('TEST1');
        const rVal2 = sut.addHandler('test1', (id, message, userData) => { }, 'userData');
        expect(rVal2).toBe('TEST1');
    });
    it('handles an event', (done) => {
        sut.addHandler('test1', (id, message, userData) => {
            expect(id).toBe('TEST1');
            expect(message).toEqual({ name: 'Stuart' });
            expect(userData).toBe('useful data');
            done();
        }, 'useful data');
        sut.handleEvent('test1', { name: 'Stuart' });
    });
    it('does not clean up callbacks if id is string', (done) => {
        sut.addHandler('test1', (id, message, userData) => {
            expect(id).toBe('TEST1');
            expect(message).toEqual({ name: 'Stuart' });
            expect(userData).toBe('useful data');
            done();
        }, 'useful data');
        sut.handleEvent('test1', { name: 'Stuart' });
        const rVal = sut.removeAllHandlers('test1');
        expect(rVal).toBe(1);
    });
    it('does clean up callbacks if id is number', () => {
        const id = sut.uniqueIdValue;
        sut.addHandler(id, (id, message, userData) => { }, 'useful data');
        sut.handleEvent(id, { name: 'Stuart' });
        const rVal = sut.removeAllHandlers('test1');
        expect(rVal).toBe(0);
    });
    it('handles multiple events', (done) => {
        let dones = 0;
        const myDone = () => (++dones === 2) ? done() : null;
        sut.addHandler('test1', (id, message, userData) => {
            expect(id).toBe('TEST1');
            expect(message).toEqual({ name: 'Stuart' });
            expect(userData).toBe('useful data');
            myDone();
        }, 'useful data');
        sut.addHandler('test1', (id, message, userData) => {
            expect(id).toBe('TEST1');
            expect(message).toEqual({ name: 'Stuart' });
            expect(userData).toBe('useful data');
            myDone();
        }, 'useful data');
        sut.handleEvent('test1', { name: 'Stuart' });
    });
    it('removes a single handler', () => {
        const cb = (id, message, userData) => { };
        sut.addHandler('test1', cb);
        const rVal = sut.removeHandler('test1', cb);
        expect(rVal).toBe(1);
    });
    it('does not remove anything none registered', () => {
        const cb = (id, message, userData) => { };
        const rVal = sut.removeHandler('test1', cb);
        expect(rVal).toBe(0);
    });
    it('removes all handlers', () => {
        const cb = (id, message, userData) => { };
        sut.addHandler(10, cb);
        sut.addHandler(10, cb);
        sut.addHandler(10, cb);
        const rVal = sut.removeAllHandlers(10);
        expect(rVal).toBe(1);
    });
    it('does not remove all handlers if none set', () => {
        const rVal = sut.removeAllHandlers(10);
        expect(rVal).toBe(0);
    });
});
