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

define([
    'jquery',
    'testsuite/qunit',
    'ep',

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

    var container,
        createNode = function () {
            $('<div class="testNode" />').appendTo('#qunit-fixture');
        };


    QUnit.module('ep/fn/scrollbeyond', {
        setup: function () {
            container = $('<div id="scrollContainer" style="height:150px; overflow: scroll;" />').appendTo('#qunit-fixture');
            container.html('<h2>Test</h2><h2>Test</h2><h2>Test</h2><h2>Test</h2><h2>Test</h2><h2>Test</h2><h2>Test</h2><h2>Test</h2>');
        },
        teardown: function () {
            container.remove();
        }
    });


    QUnit.test("Prequisites", function () {
        container.onScrollBeyond(createNode);

        QUnit.deepEqual(
            $.isFunction(ep.fn.onScrollBeyond),
            true,
            "Check whether ep.fn.onScrollBeyond is a function"
        );

        QUnit.deepEqual(
            $('#scrollContainer > .ep-scrollbeyond-container').length > 0,
            true,
            "Check if inner scroll container is created correctly"
        );
    });

    QUnit.test("Test scroll behavior", function () {
        container.onScrollBeyond(createNode);
        container.scrollTop(10);
        container.triggerHandler('scroll');

        QUnit.deepEqual(
            $('.testNode').length === 0,
            true,
            "BeyondScroll should not load additional elements when not scrolling to the bottom"
        );

        container.scrollTop(255);
        container.triggerHandler('scroll');

        QUnit.deepEqual(
            $('.testNode').length === 1,
            true,
            "BeyondScroll should load additional elements"
        );
    });
});