import mockWebFragments from 'mocks/web-fragments';

/**
 * This file specifically tests the MockWebFragments implementation and example web item functions and expects the example web
 * fragments to be returned in every case.
 */

QUnit.module('mock-web-fragments');

function testFragmentBase(assert, fragmentType, location, fragment) {
    assert.ok(fragment);
    assert.strictEqual(fragment.type, fragmentType);
    assert.strictEqual(fragment.location, location);
    assert.deepEqual(fragment.params, {});
    assert.strictEqual(fragment.key, 'example-web-' + fragmentType.toLowerCase());
    assert.strictEqual(fragment.weight, 1000);
}

function testItem(assert, location, context, fragment) {
    testFragmentBase(assert, 'ITEM', location, fragment);
    assert.strictEqual(fragment.url, "#example-web-item-url");
    assert.strictEqual(fragment.text, "Client Web Item: " + location);
    assert.strictEqual(fragment.id, null);
    assert.strictEqual(fragment.cssClass, 'plugin-point');
    assert.ok(fragment.tooltip && fragment.tooltip.indexOf(Object.keys(context)[0]) !== -1, 'exposes context keys');
    assert.strictEqual(fragment.iconUrl, null);
    assert.strictEqual(fragment.iconWidth, 0);
    assert.strictEqual(fragment.iconHeight, 0);
}

function testSection(assert, location, context, fragment) {
    testFragmentBase(assert, 'SECTION', location, fragment);
    assert.strictEqual(fragment.text, "Client Web Section: " + location);
}

function testPanel(assert, location, context, fragment) {
    testFragmentBase(assert, 'PANEL', location, fragment);
    assert.ok(fragment.html.indexOf('Client Web Panel') !== -1, 'labelled as Client Web Panel');
    assert.ok(fragment.html.indexOf(location) !== -1, 'exposes location');
    assert.ok(fragment.html.indexOf(Object.keys(context)[0]) !== -1, 'exposes context keys');
}

QUnit.test('getWebItems', function(assert) {
    const fragments = mockWebFragments.getWebItems('some-location', { data: 'thing' });
    assert.ok(fragments instanceof Array);
    assert.strictEqual(fragments.length, 1);
    testFragmentBase(assert, 'ITEM', 'some-location', fragments[0]);
    testItem(assert, 'some-location', { data: 'thing' }, fragments[0]);
});

QUnit.test('getWebSections', function(assert) {
    const fragments = mockWebFragments.getWebSections('some-location', { data: 'thing' });
    assert.ok(fragments instanceof Array);
    assert.strictEqual(fragments.length, 1);
    testSection(assert, 'some-location', { data: 'thing' }, fragments[0]);
});

QUnit.test('getWebPanels', function(assert) {
    const fragments = mockWebFragments.getWebPanels('some-location', { mydata: 'thing' });
    assert.ok(fragments instanceof Array);
    assert.strictEqual(fragments.length, 1);
    testPanel(assert, 'some-location', { mydata: 'thing' }, fragments[0]);
});


QUnit.test('getWebFragments', function(assert) {
    const testerByType = {
        ITEM: testItem,
        SECTION: testSection,
        PANEL: testPanel,
    };
    Object.keys(testerByType).forEach(function(type) {
        const fragments = mockWebFragments.getWebFragments('some-location', [type], { data: 'thing' });
        assert.ok(fragments instanceof Array);
        assert.strictEqual(fragments.length, 1);
        testerByType[type](assert, 'some-location', { data: 'thing' }, fragments[0]);
    });

    const fragments = mockWebFragments.getWebFragments('some-location', ['ITEM', 'SECTION', 'PANEL'], { data: 'thing' });
    assert.ok(fragments instanceof Array);
    assert.strictEqual(fragments.length, 3);
    fragments.forEach(function(fragment) {
        assert.ok(fragment);
        assert.ok(testerByType[fragment.type], 'Known fragment type');
        testerByType[fragment.type](assert, 'some-location', { data: 'thing' }, fragment);
    });
});
