diaspora.js cleanup; don't pollute global namespace in spec
This commit is contained in:
parent
a16e83df9e
commit
0b85600e0a
2 changed files with 48 additions and 38 deletions
|
|
@ -3,33 +3,41 @@
|
||||||
* the COPYRIGHT file.
|
* the COPYRIGHT file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Diaspora = Diaspora || {};
|
(function() {
|
||||||
|
if(typeof window.Diaspora !== "undefined") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Diaspora.widgetCollection = function() {
|
var Diaspora = { };
|
||||||
|
|
||||||
|
Diaspora.WidgetCollection = function() {
|
||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
this.collection = {};
|
this.collection = { };
|
||||||
};
|
};
|
||||||
|
|
||||||
Diaspora.widgetCollection.prototype.add = function(widgetId, widget) {
|
Diaspora.WidgetCollection.prototype.add = function(widgetId, widget) {
|
||||||
this[widgetId] = this.collection[widgetId] = new widget();
|
this[widgetId] = this.collection[widgetId] = new widget();
|
||||||
if(this.initialized) {
|
if(this.initialized) {
|
||||||
this.collection[widgetId].start();
|
this.collection[widgetId].start();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Diaspora.widgetCollection.prototype.remove = function(widgetId) {
|
Diaspora.WidgetCollection.prototype.remove = function(widgetId) {
|
||||||
delete this.collection[widgetId];
|
delete this.collection[widgetId];
|
||||||
};
|
};
|
||||||
|
|
||||||
Diaspora.widgetCollection.prototype.init = function() {
|
Diaspora.WidgetCollection.prototype.init = function() {
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
for(var widgetId in this.collection) {
|
for(var widgetId in this.collection) {
|
||||||
this.collection[widgetId].start();
|
this.collection[widgetId].start();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
Diaspora.widgets = Diaspora.widgets || new Diaspora.widgetCollection();
|
Diaspora.widgets = new Diaspora.WidgetCollection();
|
||||||
|
|
||||||
|
window.Diaspora = Diaspora;
|
||||||
|
})();
|
||||||
|
|
||||||
|
|
||||||
|
$(document).ready(Diaspora.widgets.init);
|
||||||
|
|
||||||
$(document).ready(function() {
|
|
||||||
Diaspora.widgets.init();
|
|
||||||
});
|
|
||||||
|
|
|
||||||
|
|
@ -4,49 +4,51 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
describe("Diaspora", function() {
|
describe("Diaspora", function() {
|
||||||
describe("widgetCollection", function() {
|
describe("WidgetCollection", function() {
|
||||||
describe("prototype", function() {
|
describe("prototype", function() {
|
||||||
|
var widgets;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
window.widgets = new Diaspora.widgetCollection();
|
widgets = new Diaspora.WidgetCollection();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("add", function() {
|
describe("add", function() {
|
||||||
it("adds a widget to the collection", function() {
|
it("adds a widget to the collection", function() {
|
||||||
expect(window.widgets.collection["nameOfWidget"]).not.toBeDefined();
|
expect(widgets.collection["nameOfWidget"]).not.toBeDefined();
|
||||||
window.widgets.add("nameOfWidget", function() { });
|
widgets.add("nameOfWidget", function() { });
|
||||||
expect(window.widgets.collection["nameOfWidget"]).toBeDefined();
|
expect(widgets.collection["nameOfWidget"]).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sets a shortcut by referencing the object on Diaspora.widgetCollection", function() {
|
it("sets a shortcut by referencing the object on Diaspora.widgetCollection", function() {
|
||||||
expect(window.widgets.sup).toBeFalsy();
|
expect(widgets.sup).toBeFalsy();
|
||||||
window.widgets.add("sup", function() { });
|
widgets.add("sup", function() { });
|
||||||
expect(window.widgets.sup).toEqual(window.widgets.collection.sup);
|
expect(widgets.sup).toEqual(widgets.collection.sup);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("remove", function() {
|
describe("remove", function() {
|
||||||
it("removes a widget from the collection", function() {
|
it("removes a widget from the collection", function() {
|
||||||
window.widgets.add("nameOfWidget", function() { });
|
widgets.add("nameOfWidget", function() { });
|
||||||
expect(window.widgets.collection["nameOfWidget"]).toBeDefined();
|
expect(widgets.collection["nameOfWidget"]).toBeDefined();
|
||||||
window.widgets.remove("nameOfWidget");
|
widgets.remove("nameOfWidget");
|
||||||
expect(window.widgets.collection["nameOfWidget"]).not.toBeDefined();
|
expect(widgets.collection["nameOfWidget"]).not.toBeDefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("init", function() {
|
describe("init", function() {
|
||||||
it("calls the start method on all of the widgets present", function() {
|
it("calls the start method on all of the widgets present", function() {
|
||||||
window.widgets.add("nameOfWidget", function() {
|
widgets.add("nameOfWidget", function() {
|
||||||
this.start = function() { }
|
this.start = function() { }
|
||||||
});
|
});
|
||||||
|
|
||||||
spyOn(window.widgets.collection["nameOfWidget"], "start");
|
spyOn(widgets.collection["nameOfWidget"], "start");
|
||||||
window.widgets.init();
|
widgets.init();
|
||||||
expect(window.widgets.collection["nameOfWidget"].start).toHaveBeenCalled();
|
expect(widgets.collection["nameOfWidget"].start).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
it("changes the ready property to true", function() {
|
|
||||||
expect(window.widgets.initialized).toBeFalsy();
|
it("changes the initialized property to true", function() {
|
||||||
window.widgets.init();
|
expect(widgets.initialized).toBeFalsy();
|
||||||
expect(window.widgets.initialized).toBeTruthy();
|
widgets.init();
|
||||||
|
expect(widgets.initialized).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue