Skip to content

Commit c0af997

Browse files
committed
WIP: add info.test.ts
1 parent e290f67 commit c0af997

File tree

4 files changed

+176
-5
lines changed

4 files changed

+176
-5
lines changed

tests/database/event.test.ts

Whitespace-only changes.

tests/database/helpers/events.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function rawPath(firebaseRef) {
3131
* @param {string=} opt_helperName
3232
* @return {{waiter: waiter, watchesInitializedWaiter: watchesInitializedWaiter, unregister: unregister, addExpectedEvents: addExpectedEvents}}
3333
*/
34-
export function eventTestHelper(pathAndEvents, helperName?, eventAccumulator?: EventAccumulator) {
34+
export function eventTestHelper(pathAndEvents, helperName?, eventAccumulator?: EventAccumulator | EventAccumulator[]) {
3535
var expectedPathAndEvents = [];
3636
var actualPathAndEvents = [];
3737
var pathEventListeners = {};
@@ -43,10 +43,19 @@ export function eventTestHelper(pathAndEvents, helperName?, eventAccumulator?: E
4343
// appends to actualPathAndEvents.
4444
var make_eventCallback = function(type) {
4545
return function(snap) {
46-
console.log('evt', type);
4746
// Get the ref of where the snapshot came from.
4847
var ref = type === 'value' ? snap.ref : snap.ref.parent;
49-
eventAccumulator && eventAccumulator.addEvent && eventAccumulator.addEvent();
48+
49+
if (eventAccumulator) {
50+
if (Array.isArray(eventAccumulator)) {
51+
eventAccumulator.forEach(ea => {
52+
ea.addEvent();
53+
})
54+
} else {
55+
eventAccumulator.addEvent();
56+
}
57+
}
58+
5059
actualPathAndEvents.push([rawPath(ref), [type, snap.key]]);
5160

5261
if (!pathEventListeners[ref].initialized) {

tests/database/helpers/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export function testAuthTokenProvider(app) {
170170
let freshRepoId = 1;
171171
const activeFreshApps = [];
172172

173-
export function getFreshRepo(url, path) {
173+
export function getFreshRepo(url, path?) {
174174
var app = firebase.initializeApp({databaseURL: url}, 'ISOLATED_REPO_' + freshRepoId++);
175175
patchFakeAuthFunctions(app);
176176
activeFreshApps.push(app);
@@ -210,4 +210,4 @@ export function buildObjFromKey(key) {
210210
parent = parent[key];
211211
}
212212
return obj;
213-
};
213+
};

tests/database/info.test.ts

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { expect } from "chai";
2+
import {
3+
getFreshRepo,
4+
getRootNode,
5+
getRandomNode,
6+
getPath
7+
} from "./helpers/util";
8+
import { Reference } from "../../src/database/api/Reference";
9+
import { EventAccumulator } from "./helpers/EventAccumulator";
10+
11+
describe.only(".info Tests", function () {
12+
it("Can get a reference to .info nodes.", function() {
13+
var f = (getRootNode() as Reference);
14+
expect(getPath(f.child('.info'))).to.equal('/.info');
15+
expect(getPath(f.child('.info/foo'))).to.equal('/.info/foo');
16+
});
17+
18+
it("Can't write to .info", function() {
19+
var f = (getRootNode() as Reference).child('.info');
20+
expect(function() {f.set('hi');}).to.throw;
21+
expect(function() {f.setWithPriority('hi', 5);}).to.throw;
22+
expect(function() {f.setPriority('hi');}).to.throw;
23+
expect(function() {f.transaction(function() { });}).to.throw;
24+
expect(function() {f.push();}).to.throw;
25+
expect(function() {f.remove();}).to.throw;
26+
27+
expect(function() {f.child('test').set('hi');}).to.throw;
28+
var f2 = f.child('foo/baz');
29+
expect(function() {f2.set('hi');}).to.throw;
30+
});
31+
32+
it("Can watch .info/connected.", function (done) {
33+
var f = (getRandomNode() as Reference).root;
34+
f.child('.info/connected').on('value', function(snap) {
35+
if (snap.val() === true) done();
36+
});
37+
});
38+
39+
40+
it('.info/connected correctly goes to false when disconnected.', async function() {
41+
var f = (getRandomNode() as Reference).root;
42+
var everConnected = false;
43+
var connectHistory = '';
44+
45+
const ea = new EventAccumulator(() => everConnected);
46+
f.child('.info/connected').on('value', function(snap) {
47+
if (snap.val() === true)
48+
everConnected = true;
49+
50+
if (everConnected)
51+
connectHistory += snap.val() + ',';
52+
ea.addEvent();
53+
});
54+
55+
await ea.promise;
56+
57+
ea.reset(() => connectHistory);
58+
f.database.goOffline();
59+
f.database.goOnline();
60+
61+
return ea.promise;
62+
});
63+
64+
it(".info/serverTimeOffset", async function() {
65+
var ref = (getRootNode() as Reference);
66+
67+
// make sure push works
68+
var child = ref.push();
69+
70+
var offsets = [];
71+
72+
const ea = new EventAccumulator(() => offsets.length === 1);
73+
74+
ref.child('.info/serverTimeOffset').on('value', function(snap) {
75+
offsets.push(snap.val());
76+
ea.addEvent();
77+
});
78+
79+
await ea.promise;
80+
81+
expect(typeof offsets[0]).to.equal('number');
82+
expect(offsets[0]).not.to.be.greaterThan(0);
83+
84+
// Make sure push still works
85+
ref.push();
86+
ref.child('.info/serverTimeOffset').off();
87+
});
88+
89+
it.skip("database.goOffline() / database.goOnline() connection management", function() {
90+
var ref = getFreshRepo(TEST_NAMESPACE);
91+
var refAlt = getFreshRepo(TEST_ALT_NAMESPACE);
92+
var ready;
93+
94+
// Wait until we're connected to both Firebases
95+
runs(function() {
96+
ready = 0;
97+
var eventHandler = function(snap) {
98+
if (snap.val() === true) {
99+
snap.ref.off();
100+
ready += 1;
101+
}
102+
};
103+
ref.child(".info/connected").on("value", eventHandler);
104+
refAlt.child(".info/connected").on("value", eventHandler);
105+
});
106+
waitsFor(function() { return (ready == 2); });
107+
108+
runs(function() {
109+
ref.database.goOffline();
110+
refAlt.database.goOffline();
111+
});
112+
113+
// Ensure we're disconnected from both Firebases
114+
runs(function() {
115+
ready = 0;
116+
var eventHandler = function(snap) {
117+
expect(snap.val() === false);
118+
ready += 1;
119+
}
120+
ref.child(".info/connected").once("value", eventHandler);
121+
refAlt.child(".info/connected").once("value", eventHandler);
122+
});
123+
waitsFor(function() { return (ready == 2); });
124+
125+
// Ensure that we don't automatically reconnect upon Reference creation
126+
runs(function() {
127+
ready = 0;
128+
var refDup = ref.database.ref();
129+
refDup.child(".info/connected").on("value", function(snap) {
130+
ready = (snap.val() === true) || ready;
131+
});
132+
setTimeout(function() {
133+
expect(ready).to.equal(0);
134+
refDup.child(".info/connected").off();
135+
ready = -1;
136+
}, 500);
137+
});
138+
waitsFor(function() { return ready == -1; });
139+
140+
runs(function() {
141+
ref.database.goOnline();
142+
refAlt.database.goOnline();
143+
});
144+
145+
// Ensure we're connected to both Firebases
146+
runs(function() {
147+
ready = 0;
148+
var eventHandler = function(snap) {
149+
if (snap.val() === true) {
150+
snap.ref.off();
151+
ready += 1;
152+
}
153+
};
154+
ref.child(".info/connected").on("value", eventHandler);
155+
refAlt.child(".info/connected").on("value", eventHandler);
156+
});
157+
158+
waitsFor(function() {
159+
return (ready == 2);
160+
});
161+
});
162+
});

0 commit comments

Comments
 (0)