/*jshint -W030 */

import {element} from '../src/ceb.js';

describe('ceb.element()', () => {
    var sandbox, builder, before, after;
    beforeEach(() => {
        if (sandbox) {
            sandbox.parentNode.removeChild(sandbox);
        }
        document.body.appendChild((sandbox = document.createElement('div')));
        builder = element();
        before = sinon.spy();
        after = sinon.spy();
    });

    afterEach(() => {
        sandbox.innerHTML = '';
    });

    it('should handle prototype and extends value', () => {
        builder.base(Object.create(HTMLButtonElement.prototype), 'button').register('test-prototype-extends');
        let customElement = document.createElement('button', 'test-prototype-extends');
        expect(customElement).to.be.an.instanceOf(HTMLButtonElement);
    });

    it('should handle extends and prototype value', () => {
        builder.base('button', Object.create(HTMLButtonElement.prototype)).register('test-extends-prototype');
        let customElement = document.createElement('button', 'test-extends-prototype');
        expect(customElement).to.be.an.instanceOf(HTMLButtonElement);
    });

    it('should handle prototype value', () => {
        builder.base(Object.create(HTMLButtonElement.prototype)).register('test-prototype');
        let customElement = document.createElement('test-prototype');
        expect(customElement).to.be.an.instanceOf(HTMLButtonElement);
    });

    it('should handle extends value', () => {
        builder.base('button').register('test-extends');
        let customElement = document.createElement('button', 'test-extends');
        expect(customElement).to.be.an.instanceOf(HTMLElement);
    });

    it('should handle before:builders and after:builders', () => {
        builder
            .on('before:builders').invoke(before)
            .on('after:builders').invoke(after)
            .register('test-builders');
        expect(before).to.have.been.calledOnce;
        expect(before).to.have.been.calledWith(builder.context);
        expect(after).to.have.been.calledOnce;
        expect(after).to.have.been.calledWith(builder.context);
    });

    it('should handle before:registerElement and after:registerElement', () => {
        var CustomElement = builder
            .on('before:registerElement').invoke(before)
            .on('after:registerElement').invoke(after)
            .register('test-registerElement');
        expect(before).to.have.been.calledOnce;
        expect(before).to.have.been.calledWith(builder.context);
        expect(after).to.have.been.calledOnce;
        expect(after).to.have.been.calledWith(CustomElement);
    });

    it('should handle before:createdCallback and after:createdCallback', () => {
        builder
            .on('before:createdCallback').invoke(before)
            .on('after:createdCallback').invoke(after)
            .register('test-createdCallback');
        var el = document.createElement('test-createdCallback');
        expect(before).to.have.been.calledOnce;
        expect(before).to.have.been.calledWith(sinon.match(el));
        expect(after).to.have.been.calledOnce;
        expect(after).to.have.been.calledWith(sinon.match(el));

    });

    it('should handle before:detachedCallback and after:detachedCallback', done => {
        builder
            .on('before:detachedCallback').invoke(before)
            .on('after:detachedCallback').invoke(after)
            .register('test-detachedCallback');
        var el = document.createElement('test-detachedCallback');
        sandbox.appendChild(el);
        setTimeout(() => {
            sandbox.removeChild(el);
            setTimeout(() => {
                expect(before).to.have.been.calledOnce;
                expect(before).to.have.been.calledWith(sinon.match(el));
                expect(after).to.have.been.calledOnce;
                expect(after).to.have.been.calledWith(sinon.match(el));
                done();
            }, 20);
        }, 0);
    });

    it('should handle before:attachedCallback and after:attachedCallback', done => {
        builder
            .on('before:attachedCallback').invoke(before)
            .on('after:attachedCallback').invoke(after)
            .register('test-attachedCallback');
        var el = document.createElement('test-attachedCallback');
        sandbox.appendChild(el);
        setTimeout(() => {
            expect(before).to.have.been.calledOnce;
            expect(before).to.have.been.calledWith(sinon.match(el));
            expect(after).to.have.been.calledOnce;
            expect(after).to.have.been.calledWith(sinon.match(el));
            done();
        }, 10);
    });

    it('should handle before:attributeChangedCallback and after:attributeChangedCallback', done => {
        builder
            .on('before:attributeChangedCallback').invoke(before)
            .on('after:attributeChangedCallback').invoke(after)
            .register('test-attributeChangedCallback');
        var el = document.createElement('test-attributeChangedCallback');
        el.setAttribute('name', 'value');
        setTimeout(() => {
            expect(before).to.have.been.calledOnce;
            expect(before).to.have.been.calledWith(sinon.match(el));
            expect(after).to.have.been.calledOnce;
            expect(after).to.have.been.calledWith(sinon.match(el));
            done();
        }, 10);
    });

    it('should handle builders', done => {
        var build = sinon.spy();
        builder.builders({build: build}).register('test-builders-handlers');
        setTimeout(() => {
            expect(build).to.have.been.calledOnce;
            expect(build).to.have.been.calledWith(sinon.match(Object), sinon.match(builder.on));
            done();
        }, 10);
    });

});

@@ -1071,32 +1071,34 @@
 type');%0A        
+//
 expect(customEle
@@ -1363,32 +1363,34 @@
 type');%0A        
+//
 expect(customEle
@@ -1625,32 +1625,34 @@
 ends');%0A        
+//
 expect(customEle

fix ci