import skate from 'skate';

import 'bitbucket/internal/bbui/bb-panel/bb-panel';

QUnit.module('bb-panel', {
    beforeEach() {
        this.bbPanel = document.createElement('bb-panel');
        document.getElementById('qunit-fixture').appendChild(this.bbPanel);
        skate.init(this.bbPanel);

        this.assertClosed = assert => {
            assert.ok(!this.bbPanel.open, 'Dialog is closed on load');
            assert.equal(this.bbPanel.getAttribute('aria-hidden'), 'true', 'Dialog is not visible in the document');
        };
        this.assertOpen = assert => {
            assert.ok(this.bbPanel.open, 'Dialog is now open');
            assert.equal(this.bbPanel.getAttribute('aria-hidden'), 'false', 'dialog is now visible');
        };
    },
    afterEach() {
        // it might have moved to <body> so we need to manually remove it
        this.bbPanel.remove();
    },
});

QUnit.test('Panel is closed on load', function(assert) {
    this.assertClosed(assert);
});

QUnit.test('Setting open prototype causes the panel to open and close', function(assert) {
    this.bbPanel.open = true;

    this.assertOpen(assert);

    this.bbPanel.open = false;

    this.assertClosed(assert);
});

QUnit.test('Setting open attribute causes the panel to open and close', function(assert) {
    this.bbPanel.setAttribute('open', '');

    this.assertOpen(assert);

    this.bbPanel.removeAttribute('open');

    this.assertClosed(assert);
});

QUnit.test('Setting aria-hidden attribute  causes the panel to open and close', function(assert) {
    this.bbPanel.setAttribute('aria-hidden', 'false');

    this.assertOpen(assert);

    this.bbPanel.setAttribute('aria-hidden', 'true');

    this.assertClosed(assert);
});
