define([
    'jquery',
    'lib/page-data-loader'
], function(
    $,
    _PageDataPlugin
) {

    'use strict';

    var data = {
        "some.context" : {
            "some.key" : {},
            "other.key" : {}
        },
        "other.context" : {
            "some.key" : {},
            "other.key" : {}
        }
    };

    function readyMatches(context, key) {
        _PageDataPlugin.ready(key, context).done(function(actual) {
            strictEqual(actual, data[context][key]);
        });
    }

    _PageDataPlugin.load("some.context", data["some.context"]);

    module('lib-page-data-loader');

    $(document).ready(function() { // ensure that ready has already fired so it doesn't interfere with tests.
        test('ready, preloaded', function () {
            expect(1);
            readyMatches("some.context", "some.key");
        });

        test('ready, when loaded', function () {
            expect(2);
            readyMatches("other.context", "some.key");
            readyMatches("other.context", "other.key");
            _PageDataPlugin.load("other.context", data["other.context"]);
        });

        test('ready, never loaded', function() {
            expect(7);
            _PageDataPlugin.ready("not.real", "some.context").fail(function(err) {
                ok(err);
                ok(err.message);
                ok(~err.message.indexOf('not.real'));
            });

            _PageDataPlugin.ready("some.key", "not.real").fail(function(err) {
                ok(err);
                ok(err.message);
                ok(~err.message.indexOf('not.real'));
                ok(~err.message.indexOf('some.key'));
            });

            _PageDataPlugin._domReady(); // trigger rejections
        });
    });
});
