Skip to content

Commit 881ba39

Browse files
committed
Allow accessing child data in DataSnapshots in /
1 parent fb7ef54 commit 881ba39

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/snapshot.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,33 @@ function MockDataSnapshot (ref, data, priority) {
1414
};
1515
}
1616

17-
MockDataSnapshot.prototype.child = function (key) {
18-
var ref = this.ref.child(key);
17+
MockDataSnapshot.prototype.child = function (path) {
18+
// Strip leading or trailing /
19+
path = path.replace(/^\/|\/$/g, '');
20+
var ref = this.ref.child(path);
1921
var data = null;
22+
23+
var key;
24+
var firstPathIdx = path.indexOf('/');
25+
if (firstPathIdx === -1) {
26+
// Single path
27+
key = path;
28+
path = null;
29+
} else {
30+
// Multiple paths
31+
key = path.slice(0, firstPathIdx);
32+
path = path.slice(firstPathIdx + 1);
33+
}
2034
if (_.isObject(this._snapshotdata) && !_.isUndefined(this._snapshotdata[key])) {
2135
data = this._snapshotdata[key];
2236
}
23-
var priority = this.ref.child(key).priority;
24-
return new MockDataSnapshot(ref, data, priority);
37+
var snapshot = new MockDataSnapshot(ref, data, ref.priority);
38+
if (path === null) {
39+
return snapshot;
40+
} else {
41+
// Look for child
42+
return snapshot.child(path);
43+
}
2544
};
2645

2746
MockDataSnapshot.prototype.val = function () {
@@ -40,7 +59,7 @@ MockDataSnapshot.prototype.forEach = function (callback, context) {
4059
};
4160

4261
MockDataSnapshot.prototype.hasChild = function (path) {
43-
return !!(this._snapshotdata && this._snapshotdata[path]);
62+
return this.child(path).exists();
4463
};
4564

4665
MockDataSnapshot.prototype.hasChildren = function () {

0 commit comments

Comments
 (0)