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

define([
    'jquery',
    '$ready!'
/**
 * [ description]
 * @param  {[type]} $     [description]
 * @param  {[type]} QUnit [description]
 * @return {[type]}       [description]
 */
], function ($) {
    'use strict';

    var container = $('#qunit-fixture'),
        treeResult = [{
            "children": [{
                "metadata": {
                    "WebUrl": "http://www.DemoShop.de/Shop.sf/?ObjectPath=/Shops/DemoShop/Products/ho_40407",
                    "parentId": "16375",
                    "WebUrlSSL": "http://www.DemoShop.de/Shop.sf/?ObjectPath=/Shops/DemoShop/Products/ho_40407",
                    "id": "16524",
                    "type": "Product",
                    "ClassID": "16447"
                },
                "data": {
                    "icon": "Product",
                    "title": "Jack Wolfskin Blizzard Jacket"
                }
            }, {
                "metadata": {
                    "WebUrl": "http://www.DemoShop.de/Shop.sf/?ObjectPath=/Shops/DemoShop/Products/lt_0401107001",
                    "parentId": "16375",
                    "WebUrlSSL": "http://www.DemoShop.de/Shop.sf/?ObjectPath=/Shops/DemoShop/Products/lt_0401107001",
                    "id": "16491",
                    "type": "Product",
                    "ClassID": "16368"
                },
                "data": {
                    "icon": "Product",
                    "title": "Leatherman Tool Survival"
                }
            }, {
                "metadata": {
                    "WebUrl": "http://www.DemoShop.de/Shop.sf/?ObjectPath=/Shops/DemoShop/Products/eg_1000111010",
                    "parentId": "16375",
                    "WebUrlSSL": "http://www.DemoShop.de/Shop.sf/?ObjectPath=/Shops/DemoShop/Products/eg_1000111010",
                    "id": "16497",
                    "type": "Product",
                    "ClassID": "16462"
                },
                "data": {
                    "icon": "Product",
                    "title": "Eureka El Capitan IV"
                }
            }],
            "metadata": {
                "WebUrl": "http://www.DemoShop.de/Shop.sf/?ObjectPath=/Shops/DemoShop/Categories",
                "childCount": 15,
                "parentId": "16350",
                "WebUrlSSL": "http://www.DemoShop.de/Shop.sf/?ObjectPath=/Shops/DemoShop/Categories",
                "id": "16375",
                "ClassID": "3671"
            },
            "data": {
                "icon": "StartPage",
                "title": "Home page"
            },
            "state": "open"
        }],

        _ajax = $.ajax;

    $.ajax = function (o) {

        var result = $.Deferred(),
            jqXHR = result.promise();
        if (o.data) {
            o.success.call(o.context, treeResult, 'success', jqXHR);
        } else {
            return _ajax(o);
        }
        return result;
    };

    require(['testsuite/qunit',
        'de_epages/shop/linkpicker/pagetreeview'], function (QUnit, Pagetreeview) {

        QUnit.module("de_epages/shop/linkpicker/pagetreeview");

        var pagetreeview = new Pagetreeview(),
            view = {};

        QUnit.test("Linkpicker-pagetreeview Prototype", function () {

            $.each([
                'render',
                '_generateTreeJSONData',
                'clearLinkData',
                '_publishLinkData'], function (i, fn) {

                QUnit.equal($.isFunction(pagetreeview[fn]), true, "Check whether " + fn + " is a function: $.isFunction(pagetreeview.prototype['" + fn + "'] )");
            });
        });

        QUnit.test("Rendered Template and UI", function () {

            view = pagetreeview.render().$el;
            container.empty().append(view);
            QUnit.notEqual(container.html(), view.html(), 'Template is rendered');

            pagetreeview.on('linkpicked', function (_text, _url) {
                QUnit.strictEqual(_text, 'Jack Wolfskin Blizzard Jacket', 'Link text fetched correctly');
                QUnit.strictEqual(_url, 'http://www.DemoShop.de/Shop.sf/?ObjectPath=/Shops/DemoShop/Products/ho_40407', 'Link url fetched correctly');
            });

            $('.jstree-leaf:first a').trigger('click');
        });
    });
});