2014-11-06 22:28:22 +00:00
|
|
|
/*global define,Promise,describe,it,expect,beforeEach,waitsFor,runs,jasmine*/
|
2014-11-06 16:03:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* BundleLoaderSpec. Created by vwoeltje on 11/6/14.
|
|
|
|
*/
|
|
|
|
define(
|
2014-11-06 22:28:22 +00:00
|
|
|
["../../src/load/BundleLoader"],
|
|
|
|
function (BundleLoader) {
|
2014-11-06 16:03:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-11-06 22:28:22 +00:00
|
|
|
describe("The bundle loader", function () {
|
|
|
|
var loader,
|
|
|
|
mockCallback,
|
|
|
|
mockHttp,
|
|
|
|
mockLog,
|
|
|
|
testBundles;
|
|
|
|
|
|
|
|
// Used to wait for then-chain resolution;
|
|
|
|
// native promises may not resolve synchronously,
|
|
|
|
// even when values are immediately available.
|
|
|
|
function mockCallbackResolved() {
|
|
|
|
return mockCallback.calls.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2014-11-06 22:47:56 +00:00
|
|
|
testBundles = {
|
|
|
|
"bundles.json": [ "bundle/a", "bundle/b"],
|
|
|
|
"bundle/a/bundle.json": {"someValue": 6},
|
|
|
|
"bundle/b/bundle.json": {"someValue": 7}
|
|
|
|
};
|
2014-11-06 22:28:22 +00:00
|
|
|
|
|
|
|
mockCallback = jasmine.createSpy("callback");
|
|
|
|
mockHttp = jasmine.createSpyObj("$http", ["get"]);
|
|
|
|
mockLog = jasmine.createSpyObj("$log", ["error", "warn", "info", "debug"]);
|
|
|
|
loader = new BundleLoader(mockHttp, mockLog);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("accepts a JSON file name and loads all bundles", function () {
|
2014-11-06 22:47:56 +00:00
|
|
|
// Set up; return named bundles
|
|
|
|
mockHttp.get.andReturn(Promise.resolve({ data: [] }));
|
2014-11-06 22:28:22 +00:00
|
|
|
|
|
|
|
// Call load bundles
|
|
|
|
loader.loadBundles("test-filename.json").then(mockCallback);
|
|
|
|
|
2014-11-06 22:57:32 +00:00
|
|
|
waitsFor(mockCallbackResolved, "then-chain resolution", 100);
|
2014-11-06 22:28:22 +00:00
|
|
|
|
|
|
|
runs(function () {
|
|
|
|
// Should have loaded the file via $http
|
|
|
|
expect(mockHttp.get).toHaveBeenCalledWith("test-filename.json");
|
|
|
|
|
|
|
|
// Should have gotten an empty bundle list
|
|
|
|
expect(mockCallback).toHaveBeenCalledWith([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-06 22:47:56 +00:00
|
|
|
it("accepts a list of bundle paths", function () {
|
|
|
|
// Set up; return named bundles
|
|
|
|
mockHttp.get.andCallFake(function (name) {
|
|
|
|
return Promise.resolve({data: testBundles[name]});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Call load bundles
|
|
|
|
loader.loadBundles(["bundle/a", "bundle/b"]).then(mockCallback);
|
|
|
|
|
2014-11-06 22:57:32 +00:00
|
|
|
waitsFor(mockCallbackResolved, "then-chain resolution", 100);
|
2014-11-06 22:47:56 +00:00
|
|
|
|
|
|
|
runs(function () {
|
|
|
|
// Should have gotten back two bundles
|
|
|
|
expect(mockCallback.mostRecentCall.args[0].length).toEqual(2);
|
|
|
|
|
|
|
|
// They should have the values we expect; don't care about order,
|
|
|
|
// some map/reduce the summation.
|
|
|
|
expect(mockCallback.mostRecentCall.args[0].map(function (call) {
|
|
|
|
return call.getDefinition().someValue;
|
|
|
|
}).reduce(function (a, b) {
|
|
|
|
return a + b;
|
|
|
|
}, 0)).toEqual(13);
|
2014-11-06 22:57:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("warns about, then ignores, missing bundle declarations", function () {
|
|
|
|
// File-not-found
|
|
|
|
mockHttp.get.andReturn(Promise.reject(new Error("test error")));
|
|
|
|
|
|
|
|
// Try and load
|
|
|
|
loader.loadBundles(["some/bundle"]).then(mockCallback);
|
|
|
|
|
|
|
|
waitsFor(mockCallbackResolved, "then-chain resolution", 100);
|
2014-11-06 22:47:56 +00:00
|
|
|
|
2014-11-06 22:57:32 +00:00
|
|
|
runs(function () {
|
|
|
|
// Should have gotten zero bundle
|
|
|
|
expect(mockCallback.mostRecentCall.args[0].length).toEqual(0);
|
|
|
|
|
|
|
|
// They should have the values we expect; don't care about order,
|
|
|
|
// some map/reduce the summation.
|
|
|
|
expect(mockLog.warn).toHaveBeenCalled();
|
2014-11-06 22:47:56 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-06 22:28:22 +00:00
|
|
|
|
2014-11-06 16:03:35 +00:00
|
|
|
|
2014-11-06 23:20:26 +00:00
|
|
|
it("warns about, then ignores, malformed bundle declarations", function () {
|
2014-11-06 22:57:32 +00:00
|
|
|
// File-not-found
|
|
|
|
mockHttp.get.andReturn(Promise.resolve(["I am not a valid bundle."]));
|
|
|
|
|
|
|
|
// Try and load
|
|
|
|
loader.loadBundles(["some/bundle"]).then(mockCallback);
|
|
|
|
|
|
|
|
waitsFor(mockCallbackResolved, "then-chain resolution", 100);
|
|
|
|
|
|
|
|
runs(function () {
|
|
|
|
// Should have gotten zero bundle
|
|
|
|
expect(mockCallback.mostRecentCall.args[0].length).toEqual(0);
|
|
|
|
|
|
|
|
// They should have the values we expect; don't care about order,
|
|
|
|
// some map/reduce the summation.
|
|
|
|
expect(mockLog.warn).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-06 16:03:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|