import {ObserverList} from "../../../source/types/observerlist.mjs";
import {Observer} from "../../../source/types/observer.mjs";
import {expect} from "chai"

describe('ObserverList', function () {

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

        it('should return instanceof ObserverList', function () {
            let observerList = new ObserverList()
            let result = observerList.attach(new Observer(() => {
            }))
            expect(result).to.be.instanceOf(ObserverList)
        });


    });

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

        it('should return instanceof ObserverList', function () {
            let observerList = new ObserverList()
            let result = observerList.detach(new Observer(() => {
            }))
            expect(result).to.be.instanceOf(ObserverList)
        });


    });

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

        const resultValue = "Yes!";
        let o;
        let observerList;

        beforeEach(() => {
            o = new Observer(() => {
                return resultValue
            });

            observerList = new ObserverList()
            observerList.attach(o)

        });

        it('should instanceof Promise', function () {
            expect(observerList.notify({})).to.be.instanceOf(Promise)
        });

        it('should return ' + resultValue, function (done) {

            let obj = {}

            observerList.notify(obj).then(r => {
                expect(r).to.be.an('array').that.includes(resultValue);
                done();
            }).catch(e => {
                done(new Error(e));
            });
        });


    });

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

        let o1 = new Observer(() => {
        });
        let o2 = new Observer(() => {
        });

        it('should not contain o1 and o2', function () {
            let observerList = new ObserverList()

            expect(observerList.contains(o1)).to.be.false;
            expect(observerList.contains(o2)).to.be.false;
        });

        it('should contain o2 and not o1', function () {
            let observerList = new ObserverList()
            observerList.attach(o2)
            expect(observerList.contains(o1)).to.be.false;
            expect(observerList.contains(o2)).to.be.true;
        });

        it('should contain o1 and not o2', function () {
            let observerList = new ObserverList()
            observerList.attach(o1)
            expect(observerList.contains(o1)).to.be.true;
            expect(observerList.contains(o2)).to.be.false;
        });


        it('should contain o2 and o1', function () {
            let observerList = new ObserverList()
            observerList.attach(o2).attach(o1)
            expect(observerList.contains(o1)).to.be.true;
            expect(observerList.contains(o2)).to.be.true;
        });


    });


});

