Skip to content

Commit dbf77f8

Browse files
authored
Merge pull request #38 from dmurvihill/split-child-keys
Split child keys
2 parents fd9ead6 + f015d93 commit dbf77f8

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ node_js:
44
- "4.5"
55
- "5.12"
66
- "6.4"
7+
- "8"
78
before_script:
89
- gulp lint
910
script:

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ exports.MockFirestore = require('./firestore');
99
exports.MockStorage = require('./storage');
1010
exports.MockMessaging = require('./messaging');
1111
exports.DeltaDocumentSnapshot = MockFirestoreDeltaDocumentSnapshot.create;
12+
exports.DataSnapshot = require('./snapshot');

src/snapshot.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,34 @@ 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+
if (typeof path === 'number') path = String(path);
19+
// Strip leading or trailing /
20+
path = path.replace(/^\/|\/$/g, '');
21+
var ref = this.ref.child(path);
1922
var data = null;
23+
24+
var key;
25+
var firstPathIdx = path.indexOf('/');
26+
if (firstPathIdx === -1) {
27+
// Single path
28+
key = path;
29+
path = null;
30+
} else {
31+
// Multiple paths
32+
key = path.slice(0, firstPathIdx);
33+
path = path.slice(firstPathIdx + 1);
34+
}
2035
if (_.isObject(this._snapshotdata) && !_.isUndefined(this._snapshotdata[key])) {
2136
data = this._snapshotdata[key];
2237
}
23-
var priority = this.ref.child(key).priority;
24-
return new MockDataSnapshot(ref, data, priority);
38+
var snapshot = new MockDataSnapshot(ref, data, ref.priority);
39+
if (path === null) {
40+
return snapshot;
41+
} else {
42+
// Look for child
43+
return snapshot.child(path);
44+
}
2545
};
2646

2747
MockDataSnapshot.prototype.val = function () {
@@ -40,7 +60,7 @@ MockDataSnapshot.prototype.forEach = function (callback, context) {
4060
};
4161

4262
MockDataSnapshot.prototype.hasChild = function (path) {
43-
return !!(this._snapshotdata && this._snapshotdata[path]);
63+
return this.child(path).exists();
4464
};
4565

4666
MockDataSnapshot.prototype.hasChildren = function () {

test/unit/snapshot.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ describe('DataSnapshot', function () {
6363
expect(child.val()).to.equal('val');
6464
});
6565

66+
it('uses child data starting with /', function () {
67+
var parent = new Snapshot(ref, {key: 'val'});
68+
var child = parent.child('/key');
69+
expect(child.val()).to.equal('val');
70+
});
71+
72+
it('uses child data ending with /', function () {
73+
var parent = new Snapshot(ref, {key: 'val'});
74+
var child = parent.child('key/');
75+
expect(child.val()).to.equal('val');
76+
});
77+
6678
it('uses child data for false values', function () {
6779
var parent = new Snapshot(ref, {key: false});
6880
var child = parent.child('key');
@@ -75,12 +87,36 @@ describe('DataSnapshot', function () {
7587
expect(child.val()).to.equal(0);
7688
});
7789

90+
it('uses child data when accessing with multiple paths', function () {
91+
var parent = new Snapshot(ref, { key: { value: 'val' }});
92+
var child = parent.child('key/value');
93+
expect(child.val()).to.equal('val');
94+
});
95+
96+
it('uses child data when accessing with multiple paths for false values', function () {
97+
var parent = new Snapshot(ref, { key: { value: false }});
98+
var child = parent.child('key/value');
99+
expect(child.val()).to.equal(false);
100+
});
101+
102+
it('uses child data when accessing with multiple paths for 0 values', function () {
103+
var parent = new Snapshot(ref, { key: { value: 0 }});
104+
var child = parent.child('key/value');
105+
expect(child.val()).to.equal(0);
106+
});
107+
78108
it('uses null when there is no child data', function () {
79109
var parent = new Snapshot(ref);
80110
var child = parent.child('key');
81111
expect(child.val()).to.equal(null);
82112
});
83113

114+
it('uses null when there is no child data with multiple paths', function () {
115+
var parent = new Snapshot(ref);
116+
var child = parent.child('key/value');
117+
expect(child.val()).to.equal(null);
118+
});
119+
84120
it('passes the priority', function () {
85121
var parent = new Snapshot(ref);
86122
ref.child('key').setPriority(10);
@@ -89,6 +125,18 @@ describe('DataSnapshot', function () {
89125
expect(child.getPriority()).to.equal(10);
90126
});
91127

128+
it('allows array indexes', function () {
129+
var parent = new Snapshot(ref, ['foo', 'bar']);
130+
var child = parent.child(0);
131+
expect(child.val()).to.equal('foo');
132+
});
133+
134+
it('allows array indexes in multiple paths', function () {
135+
var parent = new Snapshot(ref, { key: { array: ['foo', 'bar'] }});
136+
var child = parent.child('key/array/1');
137+
expect(child.val()).to.equal('bar');
138+
});
139+
92140
});
93141

94142
describe('#exists', function () {
@@ -137,6 +185,25 @@ describe('DataSnapshot', function () {
137185
expect(snapshot.hasChild('bar')).to.equal(false);
138186
});
139187

188+
it('tests for the key starting with /', function () {
189+
var snapshot = new Snapshot(ref, {foo: 'bar'});
190+
expect(snapshot.hasChild('/foo')).to.equal(true);
191+
expect(snapshot.hasChild('/bar')).to.equal(false);
192+
});
193+
194+
it('tests for the key ending with /', function () {
195+
var snapshot = new Snapshot(ref, {foo: 'bar'});
196+
expect(snapshot.hasChild('foo/')).to.equal(true);
197+
expect(snapshot.hasChild('bar/')).to.equal(false);
198+
});
199+
200+
it('tests for the key with multiple paths', function () {
201+
var snapshot = new Snapshot(ref, {key: {foo: 'bar'}});
202+
expect(snapshot.hasChild('key/foo')).to.equal(true);
203+
expect(snapshot.hasChild('key/bar')).to.equal(false);
204+
expect(snapshot.hasChild('foo/key')).to.equal(false);
205+
});
206+
140207
});
141208

142209
describe('#hasChildren', function () {

0 commit comments

Comments
 (0)