@@ -14,14 +14,33 @@ function MockDataSnapshot (ref, data, priority) {
14
14
} ;
15
15
}
16
16
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 ) ;
19
21
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
+ }
20
34
if ( _ . isObject ( this . _snapshotdata ) && ! _ . isUndefined ( this . _snapshotdata [ key ] ) ) {
21
35
data = this . _snapshotdata [ key ] ;
22
36
}
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
+ }
25
44
} ;
26
45
27
46
MockDataSnapshot . prototype . val = function ( ) {
@@ -40,7 +59,7 @@ MockDataSnapshot.prototype.forEach = function (callback, context) {
40
59
} ;
41
60
42
61
MockDataSnapshot . prototype . hasChild = function ( path ) {
43
- return ! ! ( this . _snapshotdata && this . _snapshotdata [ path ] ) ;
62
+ return this . child ( path ) . exists ( ) ;
44
63
} ;
45
64
46
65
MockDataSnapshot . prototype . hasChildren = function ( ) {
0 commit comments