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.
|
||||
*/
|
||||
|
||||
var Diaspora = Diaspora || {};
|
||||
(function() {
|
||||
if(typeof window.Diaspora !== "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
Diaspora.widgetCollection = function() {
|
||||
this.initialized = false;
|
||||
this.collection = {};
|
||||
};
|
||||
var Diaspora = { };
|
||||
|
||||
Diaspora.widgetCollection.prototype.add = function(widgetId, widget) {
|
||||
Diaspora.WidgetCollection = function() {
|
||||
this.initialized = false;
|
||||
this.collection = { };
|
||||
};
|
||||
|
||||
Diaspora.WidgetCollection.prototype.add = function(widgetId, widget) {
|
||||
this[widgetId] = this.collection[widgetId] = new widget();
|
||||
if(this.initialized) {
|
||||
this.collection[widgetId].start();
|
||||
}
|
||||
};
|
||||
|
||||
Diaspora.widgetCollection.prototype.remove = function(widgetId) {
|
||||
Diaspora.WidgetCollection.prototype.remove = function(widgetId) {
|
||||
delete this.collection[widgetId];
|
||||
};
|
||||
};
|
||||
|
||||
Diaspora.widgetCollection.prototype.init = function() {
|
||||
this.initialized = true;
|
||||
for(var widgetId in this.collection) {
|
||||
this.collection[widgetId].start();
|
||||
}
|
||||
}
|
||||
Diaspora.WidgetCollection.prototype.init = function() {
|
||||
this.initialized = true;
|
||||
for(var widgetId in this.collection) {
|
||||
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("widgetCollection", function() {
|
||||
describe("WidgetCollection", function() {
|
||||
describe("prototype", function() {
|
||||
var widgets;
|
||||
beforeEach(function() {
|
||||
window.widgets = new Diaspora.widgetCollection();
|
||||
widgets = new Diaspora.WidgetCollection();
|
||||
});
|
||||
|
||||
describe("add", function() {
|
||||
it("adds a widget to the collection", function() {
|
||||
expect(window.widgets.collection["nameOfWidget"]).not.toBeDefined();
|
||||
window.widgets.add("nameOfWidget", function() { });
|
||||
expect(window.widgets.collection["nameOfWidget"]).toBeDefined();
|
||||
expect(widgets.collection["nameOfWidget"]).not.toBeDefined();
|
||||
widgets.add("nameOfWidget", function() { });
|
||||
expect(widgets.collection["nameOfWidget"]).toBeDefined();
|
||||
});
|
||||
|
||||
it("sets a shortcut by referencing the object on Diaspora.widgetCollection", function() {
|
||||
expect(window.widgets.sup).toBeFalsy();
|
||||
window.widgets.add("sup", function() { });
|
||||
expect(window.widgets.sup).toEqual(window.widgets.collection.sup);
|
||||
expect(widgets.sup).toBeFalsy();
|
||||
widgets.add("sup", function() { });
|
||||
expect(widgets.sup).toEqual(widgets.collection.sup);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", function() {
|
||||
it("removes a widget from the collection", function() {
|
||||
window.widgets.add("nameOfWidget", function() { });
|
||||
expect(window.widgets.collection["nameOfWidget"]).toBeDefined();
|
||||
window.widgets.remove("nameOfWidget");
|
||||
expect(window.widgets.collection["nameOfWidget"]).not.toBeDefined();
|
||||
widgets.add("nameOfWidget", function() { });
|
||||
expect(widgets.collection["nameOfWidget"]).toBeDefined();
|
||||
widgets.remove("nameOfWidget");
|
||||
expect(widgets.collection["nameOfWidget"]).not.toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("init", 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() { }
|
||||
});
|
||||
|
||||
spyOn(window.widgets.collection["nameOfWidget"], "start");
|
||||
window.widgets.init();
|
||||
expect(window.widgets.collection["nameOfWidget"].start).toHaveBeenCalled();
|
||||
spyOn(widgets.collection["nameOfWidget"], "start");
|
||||
widgets.init();
|
||||
expect(widgets.collection["nameOfWidget"].start).toHaveBeenCalled();
|
||||
});
|
||||
it("changes the ready property to true", function() {
|
||||
expect(window.widgets.initialized).toBeFalsy();
|
||||
window.widgets.init();
|
||||
expect(window.widgets.initialized).toBeTruthy();
|
||||
|
||||
it("changes the initialized property to true", function() {
|
||||
expect(widgets.initialized).toBeFalsy();
|
||||
widgets.init();
|
||||
expect(widgets.initialized).toBeTruthy();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue