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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
import { addNumberReducer, FakePrefs } from "test/unit/utils";
import { createStore } from "redux";
import injector from "inject!lib/Store.jsm";
describe("Store", () => {
let Store;
let sandbox;
let store;
let dbStub;
beforeEach(() => {
sandbox = sinon.createSandbox();
function ActivityStreamMessageChannel(options) {
this.dispatch = options.dispatch;
this.createChannel = sandbox.spy();
this.destroyChannel = sandbox.spy();
this.middleware = sandbox.spy(s => next => action => next(action));
this.simulateMessagesForExistingTabs = sandbox.stub();
}
dbStub = sandbox.stub().resolves();
function FakeActivityStreamStorage() {
this.db = {};
sinon.stub(this, "db").get(dbStub);
}
({ Store } = injector({
"lib/ActivityStreamMessageChannel.jsm": { ActivityStreamMessageChannel },
"lib/ActivityStreamPrefs.jsm": { Prefs: FakePrefs },
"lib/ActivityStreamStorage.jsm": {
ActivityStreamStorage: FakeActivityStreamStorage,
},
}));
store = new Store();
sandbox.stub(store, "_initIndexedDB").resolves();
});
afterEach(() => {
sandbox.restore();
});
it("should have a .feeds property that is a Map", () => {
assert.instanceOf(store.feeds, Map);
assert.equal(store.feeds.size, 0, ".feeds.size");
});
it("should have a redux store at ._store", () => {
assert.ok(store._store);
assert.property(store, "dispatch");
assert.property(store, "getState");
});
it("should create a ActivityStreamMessageChannel with the right dispatcher", () => {
assert.ok(store.getMessageChannel());
assert.equal(store.getMessageChannel().dispatch, store.dispatch);
assert.equal(store.getMessageChannel(), store._messageChannel);
});
it("should connect the ActivityStreamMessageChannel's middleware", () => {
store.dispatch({ type: "FOO" });
assert.calledOnce(store._messageChannel.middleware);
});
describe("#initFeed", () => {
it("should add an instance of the feed to .feeds", () => {
class Foo {}
store._prefs.set("foo", true);
store.init(new Map([["foo", () => new Foo()]]));
store.initFeed("foo");
assert.isTrue(store.feeds.has("foo"), "foo is set");
assert.instanceOf(store.feeds.get("foo"), Foo);
});
it("should call the feed's onAction with uninit action if it exists", () => {
let feed;
function createFeed() {
feed = { onAction: sinon.spy() };
return feed;
}
const action = { type: "FOO" };
store._feedFactories = new Map([["foo", createFeed]]);
store.initFeed("foo", action);
assert.calledOnce(feed.onAction);
assert.calledWith(feed.onAction, action);
});
it("should add a .store property to the feed", () => {
class Foo {}
store._feedFactories = new Map([["foo", () => new Foo()]]);
store.initFeed("foo");
assert.propertyVal(store.feeds.get("foo"), "store", store);
});
});
describe("#uninitFeed", () => {
it("should not throw if no feed with that name exists", () => {
assert.doesNotThrow(() => {
store.uninitFeed("bar");
});
});
it("should call the feed's onAction with uninit action if it exists", () => {
let feed;
function createFeed() {
feed = { onAction: sinon.spy() };
return feed;
}
const action = { type: "BAR" };
store._feedFactories = new Map([["foo", createFeed]]);
store.initFeed("foo");
store.uninitFeed("foo", action);
assert.calledOnce(feed.onAction);
assert.calledWith(feed.onAction, action);
});
it("should remove the feed from .feeds", () => {
class Foo {}
store._feedFactories = new Map([["foo", () => new Foo()]]);
store.initFeed("foo");
store.uninitFeed("foo");
assert.isFalse(store.feeds.has("foo"), "foo is not in .feeds");
});
});
describe("onPrefChanged", () => {
beforeEach(() => {
sinon.stub(store, "initFeed");
sinon.stub(store, "uninitFeed");
store._prefs.set("foo", false);
store.init(new Map([["foo", () => ({})]]));
});
it("should initialize the feed if called with true", () => {
store.onPrefChanged("foo", true);
assert.calledWith(store.initFeed, "foo");
assert.notCalled(store.uninitFeed);
});
it("should uninitialize the feed if called with false", () => {
store.onPrefChanged("foo", false);
assert.calledWith(store.uninitFeed, "foo");
assert.notCalled(store.initFeed);
});
it("should do nothing if not an expected feed", () => {
store.onPrefChanged("bar", false);
assert.notCalled(store.initFeed);
assert.notCalled(store.uninitFeed);
});
});
describe("#init", () => {
it("should call .initFeed with each key", async () => {
sinon.stub(store, "initFeed");
store._prefs.set("foo", true);
store._prefs.set("bar", true);
await store.init(
new Map([
["foo", () => {}],
["bar", () => {}],
])
);
assert.calledWith(store.initFeed, "foo");
assert.calledWith(store.initFeed, "bar");
});
it("should call _initIndexedDB", async () => {
await store.init(new Map());
assert.calledOnce(store._initIndexedDB);
assert.calledWithExactly(store._initIndexedDB, "feeds.telemetry");
});
it("should access the db property of indexedDB", async () => {
store._initIndexedDB.restore();
await store.init(new Map());
assert.calledOnce(dbStub);
});
it("should reset ActivityStreamStorage telemetry if opening the db fails", async () => {
store._initIndexedDB.restore();
// Force an IndexedDB error
dbStub.rejects();
await store.init(new Map());
assert.calledOnce(dbStub);
assert.isNull(store.dbStorage.telemetry);
});
it("should not initialize the feed if the Pref is set to false", async () => {
sinon.stub(store, "initFeed");
store._prefs.set("foo", false);
await store.init(new Map([["foo", () => {}]]));
assert.notCalled(store.initFeed);
});
it("should observe the pref branch", async () => {
sinon.stub(store._prefs, "observeBranch");
await store.init(new Map());
assert.calledOnce(store._prefs.observeBranch);
assert.calledWith(store._prefs.observeBranch, store);
});
it("should initialize the ActivityStreamMessageChannel channel", async () => {
await store.init(new Map());
});
it("should emit an initial event if provided", async () => {
sinon.stub(store, "dispatch");
const action = { type: "FOO" };
await store.init(new Map(), action);
assert.calledOnce(store.dispatch);
assert.calledWith(store.dispatch, action);
});
it("should initialize the telemtry feed first", () => {
store._prefs.set("feeds.foo", true);
store._prefs.set("feeds.telemetry", true);
const telemetrySpy = sandbox.stub().returns({});
const fooSpy = sandbox.stub().returns({});
// Intentionally put the telemetry feed as the second item.
const feedFactories = new Map([
["feeds.foo", fooSpy],
["feeds.telemetry", telemetrySpy],
]);
store.init(feedFactories);
assert.ok(telemetrySpy.calledBefore(fooSpy));
});
it("should dispatch init/load events", async () => {
await store.init(new Map(), { type: "FOO" });
assert.calledOnce(
store.getMessageChannel().simulateMessagesForExistingTabs
);
});
it("should dispatch INIT before LOAD", async () => {
const init = { type: "INIT" };
const load = { type: "TAB_LOAD" };
sandbox.stub(store, "dispatch");
store
.getMessageChannel()
.simulateMessagesForExistingTabs.callsFake(() => store.dispatch(load));
await store.init(new Map(), init);
assert.calledTwice(store.dispatch);
assert.equal(store.dispatch.firstCall.args[0], init);
assert.equal(store.dispatch.secondCall.args[0], load);
});
});
describe("#uninit", () => {
it("should emit an uninit event if provided on init", () => {
sinon.stub(store, "dispatch");
const action = { type: "BAR" };
store.init(new Map(), null, action);
store.uninit();
assert.calledOnce(store.dispatch);
assert.calledWith(store.dispatch, action);
});
it("should clear .feeds and ._feedFactories", () => {
store._prefs.set("a", true);
store.init(
new Map([
["a", () => ({})],
["b", () => ({})],
["c", () => ({})],
])
);
store.uninit();
assert.equal(store.feeds.size, 0);
assert.isNull(store._feedFactories);
});
});
describe("#getState", () => {
it("should return the redux state", () => {
store._store = createStore((prevState = 123) => prevState);
const { getState } = store;
assert.equal(getState(), 123);
});
});
describe("#dispatch", () => {
it("should call .onAction of each feed", async () => {
const { dispatch } = store;
const sub = { onAction: sinon.spy() };
const action = { type: "FOO" };
store._prefs.set("sub", true);
await store.init(new Map([["sub", () => sub]]));
dispatch(action);
assert.calledWith(sub.onAction, action);
});
it("should call the reducers", () => {
const { dispatch } = store;
store._store = createStore(addNumberReducer);
dispatch({ type: "ADD", data: 14 });
assert.equal(store.getState(), 14);
});
});
describe("#subscribe", () => {
it("should subscribe to changes to the store", () => {
const sub = sinon.spy();
const action = { type: "FOO" };
store.subscribe(sub);
store.dispatch(action);
assert.calledOnce(sub);
});
});
});
|