/*globals define*/
/*jslint nomen: true*/

define([
    'jquery',
    'testsuite/qunit',
    './test-view',
    '$ready!'
    /**
     * gadgetsdialog test case: constants
     * @param  {[type]} $       jQuery
     * @param  {[type]} QUnit   QUnit
     * @param  {[type]} CONST   constants
     */
], function ($, QUnit, TestView) {
    'use strict';

    var fixture = $('#qunit-fixture'),
        testView,
        expectedOptions;

    $.fn.externalcontentUiTest = function (o) {
        expectedOptions = o;
    };

    QUnit.module('de_epages/externalcontent/gadgetdialog/config-view', {
        setup: function () {
            testView = new TestView({
                test: true
            });
            fixture.empty().append(testView.render().$el);
        },
        teardown: function () {

        }
    });

    QUnit.test('Check methods of config-view', 12, function () {
        $.each([
            'setOption',
            'setOptions',
            'renderPreview',
            'onSelect',
            'onInput',
            'onSubmit',
            'onRadio',
            'onApply',
            'getAPI',
            'initialize',
            'render',
            'addAdditionalTemplateOptions'], function () {
            QUnit.ok(
                $.isFunction(testView[this]),
                'Check if ' + this + ' is a method of config-view'
            );
        });
    });

    QUnit.test('Check DOM structure and set options', function () {
        QUnit.ok(
            $('.PreviewNode').length > 0,
            'Check for preview node'
        );

        var options = {
            'o1': true,
            'o2': 'test',
            'o3': 42
        };

        testView.setOptions(options);

        QUnit.deepEqual(
            // extend options object with preset option (see setup) and check against gOptions
            $.extend(options, {
                test: true
            }),
            testView.gOptions,
            'Check setting options'
        );
    });

    // make double assertions to check passed values and set options
    QUnit.test('Check form events (onInput, onSubmit, onSelect, onRadio, onApply)', function () {

        $('input[name="radioTest"]').trigger('change');
        QUnit.strictEqual(
            testView.gOptions.radioTest === 'radio_correct' && testView.gOptions.radioTest === expectedOptions.radioTest,
            true,
            'Check if radio options are set'
        );

        $('input[name="textTest"]').trigger('change');
        QUnit.strictEqual(
            testView.gOptions.textTest === 'text_correct' && testView.gOptions.textTest === expectedOptions.textTest,
            true,
            'Check if text options are set'
        );

        $('input[name="checkboxTest"]').prop('checked', true).trigger('change');
        QUnit.strictEqual(
            testView.gOptions.checkboxTest === true && testView.gOptions.checkboxTest === expectedOptions.checkboxTest,
            true,
            'Check if checkbox options are set'
        );

        $('select').trigger('change');
        QUnit.strictEqual(
            testView.gOptions.selectTest === 'testOption' && testView.gOptions.selectTest === expectedOptions.selectTest,
            true,
            'Check if select options are set'
        );

        $('input[name="textTest"]').val('').trigger('change');
        QUnit.strictEqual(
            testView.onApply(),
            false,
            'Check if onApply returns false if field is invalid'
        );

        $('input[name="textTest"]').val('Test').trigger('change');
        QUnit.strictEqual(
            testView.onApply(),
            true,
            'Check if onApply returns true if field is valid'
        );

        testView.elements.verifyingUrl.addClass('fail-icon');
        QUnit.strictEqual(
            testView.onApply(),
            false,
            'Check if onApply returns false if fail icon is attached'
        );
    });
});