Skip to content

Commit d35b183

Browse files
committed
WIP: add datasnapshot.test.ts
1 parent 622fed8 commit d35b183

File tree

2 files changed

+210
-1
lines changed

2 files changed

+210
-1
lines changed

tests/database/datasnapshot.test.ts

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
import { expect } from "chai";
2+
import { nodeFromJSON } from "../../src/database/core/snap/nodeFromJSON";
3+
import { PRIORITY_INDEX } from "../../src/database/core/snap/indexes/PriorityIndex";
4+
import { getRandomNode } from "./helpers";
5+
import { DataSnapshot } from "../../src/database/api/DataSnapshot";
6+
import { Reference } from "../../src/database/api/Reference";
7+
8+
describe.only("DataSnapshot Tests", function () {
9+
/** @return {!DataSnapshot} */
10+
var snapshotForJSON = function(json) {
11+
var dummyRef = <Reference>getRandomNode();
12+
return new DataSnapshot(nodeFromJSON(json), dummyRef, PRIORITY_INDEX);
13+
};
14+
15+
it("DataSnapshot.hasChildren() works.", function() {
16+
var snap = snapshotForJSON({});
17+
expect(snap.hasChildren()).to.equal(false);
18+
19+
snap = snapshotForJSON(5);
20+
expect(snap.hasChildren()).to.equal(false);
21+
22+
snap = snapshotForJSON({'x': 5});
23+
expect(snap.hasChildren()).to.equal(true);
24+
});
25+
26+
it("DataSnapshot.exists() works.", function() {
27+
var snap = snapshotForJSON({});
28+
expect(snap.exists()).to.equal(false);
29+
30+
snap = snapshotForJSON({ '.priority':1 });
31+
expect(snap.exists()).to.equal(false);
32+
33+
snap = snapshotForJSON(null);
34+
expect(snap.exists()).to.equal(false);
35+
36+
snap = snapshotForJSON(true);
37+
expect(snap.exists()).to.equal(true);
38+
39+
snap = snapshotForJSON(5);
40+
expect(snap.exists()).to.equal(true);
41+
42+
snap = snapshotForJSON({'x': 5});
43+
expect(snap.exists()).to.equal(true);
44+
});
45+
46+
it("DataSnapshot.val() works.", function() {
47+
var snap = snapshotForJSON(5);
48+
expect(snap.val()).to.equal(5);
49+
50+
snap = snapshotForJSON({ });
51+
expect(snap.val()).to.equal(null);
52+
53+
var json =
54+
{
55+
x: 5,
56+
y: {
57+
ya: 1,
58+
yb: 2,
59+
yc: { yca: 3 }
60+
}
61+
};
62+
snap = snapshotForJSON(json);
63+
expect(snap.val()).to.deep.equal(json);
64+
});
65+
66+
it("DataSnapshot.child() works.", function() {
67+
var snap = snapshotForJSON({x: 5, y: { yy: 3, yz: 4}});
68+
expect(snap.child('x').val()).to.equal(5);
69+
expect(snap.child('y').val()).to.deep.equal({yy: 3, yz: 4});
70+
expect(snap.child('y').child('yy').val()).to.equal(3);
71+
expect(snap.child('y/yz').val()).to.equal(4);
72+
expect(snap.child('z').val()).to.equal(null);
73+
expect(snap.child('x/y').val()).to.equal(null);
74+
expect(snap.child('x').child('y').val()).to.equal(null)
75+
});
76+
77+
it("DataSnapshot.hasChild() works.", function() {
78+
var snap = snapshotForJSON({x: 5, y: { yy: 3, yz: 4}});
79+
expect(snap.hasChild('x')).to.equal(true);
80+
expect(snap.hasChild('y/yy')).to.equal(true);
81+
expect(snap.hasChild('dinosaur')).to.equal(false);
82+
expect(snap.child('x').hasChild('anything')).to.equal(false);
83+
expect(snap.hasChild('x/anything/at/all')).to.equal(false);
84+
});
85+
86+
it("DataSnapshot.key works.", function() {
87+
var snap = snapshotForJSON({a: { b: { c: 5 }}});
88+
expect(snap.child('a').key).to.equal('a');
89+
expect(snap.child('a/b/c').key).to.equal('c');
90+
expect(snap.child('/a/b/c/').key).to.equal('c');
91+
expect(snap.child('////a////b/c///').key).to.equal('c');
92+
expect(snap.child('///').key).to.equal(snap.key);
93+
94+
// Should also work for nonexistent paths.
95+
expect(snap.child('/z/q/r/v/m').key).to.equal('m');
96+
});
97+
98+
it("DataSnapshot.forEach() works: no priorities.", function() {
99+
var snap = snapshotForJSON({a: 1, z: 26, m: 13, n: 14, c: 3, b: 2, e: 5});
100+
var out = '';
101+
snap.forEach(function(child) {
102+
out = out + child.key + ':' + child.val() + ':';
103+
});
104+
105+
expect(out).to.equal('a:1:b:2:c:3:e:5:m:13:n:14:z:26:');
106+
});
107+
108+
it("DataSnapshot.forEach() works: numeric priorities.", function() {
109+
var snap = snapshotForJSON({
110+
a: {'.value': 1, '.priority': 26},
111+
z: {'.value': 26, '.priority': 1},
112+
m: {'.value': 13, '.priority': 14},
113+
n: {'.value': 14, '.priority': 12},
114+
c: {'.value': 3, '.priority': 24},
115+
b: {'.value': 2, '.priority': 25},
116+
e: {'.value': 5, '.priority': 22}});
117+
118+
var out = '';
119+
snap.forEach(function(child) {
120+
out = out + child.key + ':' + child.val() + ':';
121+
});
122+
123+
expect(out).to.equal('z:26:n:14:m:13:e:5:c:3:b:2:a:1:');
124+
});
125+
126+
it("DataSnapshot.forEach() works: numeric priorities as strings.", function() {
127+
var snap = snapshotForJSON({
128+
a: {'.value': 1, '.priority': '26'},
129+
z: {'.value': 26, '.priority': '1'},
130+
m: {'.value': 13, '.priority': '14'},
131+
n: {'.value': 14, '.priority': '12'},
132+
c: {'.value': 3, '.priority': '24'},
133+
b: {'.value': 2, '.priority': '25'},
134+
e: {'.value': 5, '.priority': '22'}});
135+
136+
var out = '';
137+
snap.forEach(function(child) {
138+
out = out + child.key + ':' + child.val() + ':';
139+
});
140+
141+
expect(out).to.equal('z:26:n:14:m:13:e:5:c:3:b:2:a:1:');
142+
});
143+
144+
it("DataSnapshot.forEach() works: alpha priorities.", function() {
145+
var snap = snapshotForJSON({
146+
a: {'.value': 1, '.priority': 'first'},
147+
z: {'.value': 26, '.priority': 'second'},
148+
m: {'.value': 13, '.priority': 'third'},
149+
n: {'.value': 14, '.priority': 'fourth'},
150+
c: {'.value': 3, '.priority': 'fifth'},
151+
b: {'.value': 2, '.priority': 'sixth'},
152+
e: {'.value': 5, '.priority': 'seventh'}});
153+
154+
var out = '';
155+
snap.forEach(function(child) {
156+
out = out + child.key + ':' + child.val() + ':';
157+
});
158+
159+
expect(out).to.equal('c:3:a:1:n:14:z:26:e:5:b:2:m:13:');
160+
});
161+
162+
it("DataSnapshot.foreach() works: mixed alpha and numeric priorities", function() {
163+
var json = {
164+
"alpha42": {'.value': 1, '.priority': "zed" },
165+
"noPriorityC": {'.value': 1, '.priority': null },
166+
"num41": {'.value': 1, '.priority': 500 },
167+
"noPriorityB": {'.value': 1, '.priority': null },
168+
"num80": {'.value': 1, '.priority': 4000.1 },
169+
"num50": {'.value': 1, '.priority': 4000 },
170+
"num10": {'.value': 1, '.priority': 24 },
171+
"alpha41": {'.value': 1, '.priority': "zed" },
172+
"alpha20": {'.value': 1, '.priority': "horse" },
173+
"num20": {'.value': 1, '.priority': 123 },
174+
"num70": {'.value': 1, '.priority': 4000.01 },
175+
"noPriorityA": {'.value': 1, '.priority': null },
176+
"alpha30": {'.value': 1, '.priority': "tree" },
177+
"num30": {'.value': 1, '.priority': 300 },
178+
"num60": {'.value': 1, '.priority': 4000.001 },
179+
"alpha10": {'.value': 1, '.priority': "0horse" },
180+
"num42": {'.value': 1, '.priority': 500 },
181+
"alpha40": {'.value': 1, '.priority': "zed" },
182+
"num40": {'.value': 1, '.priority': 500 } };
183+
184+
var snap = snapshotForJSON(json);
185+
var out = '';
186+
snap.forEach(function(child) {
187+
out = out + child.key + ', ';
188+
});
189+
190+
expect(out).to.equal("noPriorityA, noPriorityB, noPriorityC, num10, num20, num30, num40, num41, num42, num50, num60, num70, num80, alpha10, alpha20, alpha30, alpha40, alpha41, alpha42, ");
191+
});
192+
193+
it(".val() exports array-like data as arrays.", function() {
194+
var array = ['bob', 'and', 'becky', 'seem', 'really', 'nice', 'yeah?'];
195+
var snap = snapshotForJSON(array);
196+
var snapVal = snap.val();
197+
expect(snapVal).to.deep.equal(array);
198+
expect(snapVal instanceof Array).to.equal(true); // to.equal doesn't verify type.
199+
});
200+
201+
it("DataSnapshot can be JSON serialized", function() {
202+
var json = {
203+
"foo": "bar",
204+
".priority": 1
205+
};
206+
var snap = snapshotForJSON(json);
207+
expect(JSON.parse(JSON.stringify(snap))).to.deep.equal(json);
208+
});
209+
});

tests/database/sortedmap.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { shuffle } from "./helpers";
88

99
// Many of these were adapted from the mugs source code.
1010
// http://mads379.github.com/mugs/
11-
describe.only("SortedMap Tests", function() {
11+
describe("SortedMap Tests", function() {
1212
var defaultCmp = function(a, b) {
1313
if (a === b) {
1414
return 0;

0 commit comments

Comments
 (0)