/
diaspora-spec.js
148 lines (115 loc) · 4.21 KB
/
diaspora-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Copyright (c) 2010-2011, Diaspora Inc. This file is
* licensed under the Affero General Public License version 3 or later. See
* the COPYRIGHT file.
*/
describe("Diaspora", function() {
describe("EventBroker", function() {
describe("extend", function() {
var klass;
beforeEach(function() {
klass = new function() {
};
});
it("should add a subscribe method to the class", function() {
Diaspora.EventBroker.extend(klass);
expect(typeof klass.subscribe).toEqual("function");
});
it("should add a publish method to the class", function() {
Diaspora.EventBroker.extend(klass);
expect(typeof klass.publish).toEqual("function");
});
it("should add an events container to the class", function() {
Diaspora.EventBroker.extend(klass);
expect(typeof klass.eventsContainer).toEqual("object");
});
it("knows what to extend", function() {
var Klass = function() {
};
Diaspora.EventBroker.extend(Klass);
expect(typeof Klass.prototype.publish).toEqual("function");
expect(typeof Klass.prototype.subscribe).toEqual("function");
expect(typeof Klass.prototype.eventsContainer).toEqual("object");
});
it("adds basic pub/sub functionality to an object", function() {
Diaspora.EventBroker.extend(klass);
var called = false;
klass.subscribe("events/event", function() {
called = true;
});
klass.publish("events/event");
expect(called).toBeTruthy();
});
describe("subscribe", function() {
it("will subscribe to multiple events", function() {
var firstEventCalled = false,
secondEventCalled = false
events = Diaspora.EventBroker.extend({});
events.subscribe("first/event second/event", function() {
if (firstEventCalled) {
secondEventCalled = true;
} else {
firstEventCalled = true;
}
});
events.publish("first/event second/event");
expect(firstEventCalled).toBeTruthy();
expect(secondEventCalled).toBeTruthy();
});
});
describe("publish", function() {
it("will publish multiple events", function() {
var firstEventCalled = false,
secondEventCalled = false
events = Diaspora.EventBroker.extend({});
events.subscribe("first/event second/event", function() {
if (firstEventCalled) {
secondEventCalled = true;
} else {
firstEventCalled = true;
}
});
events.publish("first/event second/event");
expect(firstEventCalled).toBeTruthy();
expect(secondEventCalled).toBeTruthy();
});
});
});
});
describe("BaseWidget", function() {
var MyWidget = function() {
var self = this;
this.ready = false;
this.subscribe("widget/ready", function(evt, element) {
self.ready = true;
self.element = element;
});
};
beforeEach(function() {
Diaspora.Widgets.MyWidget = MyWidget;
});
describe("instantiate", function() {
it("instantiates a widget and calls widget/ready with an element", function() {
var element = $("foo bar baz"),
myWidget = Diaspora.BaseWidget.instantiate("MyWidget", element);
expect(myWidget.ready).toBeTruthy();
expect(myWidget.element.selector).toEqual(element.selector);
});
});
describe("globalSubscribe", function() {
it("calls subscribe on Diaspora.page", function() {
var spy = spyOn(Diaspora.page, "subscribe");
var myWidget = Diaspora.BaseWidget.instantiate("MyWidget", null);
myWidget.globalSubscribe("myEvent", $.noop);
expect(spy).toHaveBeenCalled();
});
});
describe("globalPublish", function() {
it("calls publish on Diaspora.Page", function() {
var spy = spyOn(Diaspora.page, "publish");
var myWidget = Diaspora.BaseWidget.instantiate("MyWidget", null);
myWidget.globalPublish();
expect(spy).toHaveBeenCalled();
});
});
});
});