import {expect} from "chai"

import {Observer} from "../../../source/types/observer.mjs";


describe('Observer', function () {

    let callback = function () {
    };

    let observer = new Observer(callback)

    describe('.addTag()', function () {

        [
            ['test1'],
            ['test2'],
        ].forEach(function (data) {

            let a = data.shift()

            it('addTag(' + a + ') should return instanceof Observer', function () {
                expect(observer.addTag(a)).is.instanceOf(Observer)
            });
        });

    });

    describe('.removeTag()', function () {

        [
            ['test1'],
            ['test2'],
        ].forEach(function (data) {

            let a = data.shift()

            it('removeTag(' + a + ') should return instanceof Observer', function () {
                expect(observer.removeTag(a)).is.instanceOf(Observer)
            });
        });

    });

    describe('.hasTag()', function () {

        [
            ['test1', true],
            ['test2', true],
            ['test3', false],
            ['test4', false],
        ].forEach(function (data) {

            let tempObserver = new Observer(callback)

            let a = data.shift()
            let b = data.shift()

            it('observer.hastTag(' + a + ') should return ' + b, function () {

                if (b === true) {
                    tempObserver.addTag(a)
                    expect(tempObserver.hasTag(a)).is.be.true
                } else {
                    expect(tempObserver.hasTag(a)).is.be.false
                }
            });
        });

    });

    describe('.update()', function () {

        let obj = {}
        it('observer.update(' + obj + ') is ' + 1 + 3 + 6, function (done) {

            let tempObserver = new Observer(function (a, b, c) {
                return a + b + c
            }, 1, 3, 6)


            tempObserver.update(obj).then(a => {
                expect(a).is.equal(1 + 3 + 6)
                done();
            });
        });

    });

    describe('run function .update() several times', function () {

        let obj = {}
        it('observer.update(' + obj + ') is ' + 1 + 3 + 6, function (done) {

            let counter = 0;
            let tempObserver = new Observer(function (a, b, c) {
                counter++;
            })

            tempObserver.update(obj);
            tempObserver.update(obj);
            tempObserver.update(obj);
            tempObserver.update(obj);
            tempObserver.update(obj).then(_ => {
                done()
            });

        });

    });

    describe('.getTags()', function () {

        [
            [['test1', 'test2', 'test3', 'test4'], 4],
            [['test1', 'test1', 'test3', 'test4'], 3], // double test1 (set is uniqueue)
            [['test1', 'test1', 'test1', 'test4'], 2], // multiple test1 (set is uniqueue)
        ].forEach(function (data) {

            let tempObserver = new Observer(callback)

            let a = data.shift()
            let b = data.shift()

            for (let i = 0, max = a.length; i < max; i++) {
                tempObserver.addTag(a[i]);
            }

            it('observer.getTags(' + a + ') is array', function () {
                expect(tempObserver.getTags()).to.be.an('array')
            });

            it('observer.getTags(' + a + ') should return ' + b + " tags", function () {
                expect(tempObserver.getTags().length).is.equal(b);
            });
        });

    });

});

