|
| 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