Skip to content

Commit

Permalink
Merge pull request #38 from dmurvihill/split-child-keys
Browse files Browse the repository at this point in the history
Split child keys
  • Loading branch information
dmurvihill authored Nov 20, 2019
2 parents fd9ead6 + f015d93 commit dbf77f8
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_js:
- "4.5"
- "5.12"
- "6.4"
- "8"
before_script:
- gulp lint
script:
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ exports.MockFirestore = require('./firestore');
exports.MockStorage = require('./storage');
exports.MockMessaging = require('./messaging');
exports.DeltaDocumentSnapshot = MockFirestoreDeltaDocumentSnapshot.create;
exports.DataSnapshot = require('./snapshot');
30 changes: 25 additions & 5 deletions src/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,34 @@ function MockDataSnapshot (ref, data, priority) {
};
}

MockDataSnapshot.prototype.child = function (key) {
var ref = this.ref.child(key);
MockDataSnapshot.prototype.child = function (path) {
if (typeof path === 'number') path = String(path);
// Strip leading or trailing /
path = path.replace(/^\/|\/$/g, '');
var ref = this.ref.child(path);
var data = null;

var key;
var firstPathIdx = path.indexOf('/');
if (firstPathIdx === -1) {
// Single path
key = path;
path = null;
} else {
// Multiple paths
key = path.slice(0, firstPathIdx);
path = path.slice(firstPathIdx + 1);
}
if (_.isObject(this._snapshotdata) && !_.isUndefined(this._snapshotdata[key])) {
data = this._snapshotdata[key];
}
var priority = this.ref.child(key).priority;
return new MockDataSnapshot(ref, data, priority);
var snapshot = new MockDataSnapshot(ref, data, ref.priority);
if (path === null) {
return snapshot;
} else {
// Look for child
return snapshot.child(path);
}
};

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

MockDataSnapshot.prototype.hasChild = function (path) {
return !!(this._snapshotdata && this._snapshotdata[path]);
return this.child(path).exists();
};

MockDataSnapshot.prototype.hasChildren = function () {
Expand Down
67 changes: 67 additions & 0 deletions test/unit/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ describe('DataSnapshot', function () {
expect(child.val()).to.equal('val');
});

it('uses child data starting with /', function () {
var parent = new Snapshot(ref, {key: 'val'});
var child = parent.child('/key');
expect(child.val()).to.equal('val');
});

it('uses child data ending with /', function () {
var parent = new Snapshot(ref, {key: 'val'});
var child = parent.child('key/');
expect(child.val()).to.equal('val');
});

it('uses child data for false values', function () {
var parent = new Snapshot(ref, {key: false});
var child = parent.child('key');
Expand All @@ -75,12 +87,36 @@ describe('DataSnapshot', function () {
expect(child.val()).to.equal(0);
});

it('uses child data when accessing with multiple paths', function () {
var parent = new Snapshot(ref, { key: { value: 'val' }});
var child = parent.child('key/value');
expect(child.val()).to.equal('val');
});

it('uses child data when accessing with multiple paths for false values', function () {
var parent = new Snapshot(ref, { key: { value: false }});
var child = parent.child('key/value');
expect(child.val()).to.equal(false);
});

it('uses child data when accessing with multiple paths for 0 values', function () {
var parent = new Snapshot(ref, { key: { value: 0 }});
var child = parent.child('key/value');
expect(child.val()).to.equal(0);
});

it('uses null when there is no child data', function () {
var parent = new Snapshot(ref);
var child = parent.child('key');
expect(child.val()).to.equal(null);
});

it('uses null when there is no child data with multiple paths', function () {
var parent = new Snapshot(ref);
var child = parent.child('key/value');
expect(child.val()).to.equal(null);
});

it('passes the priority', function () {
var parent = new Snapshot(ref);
ref.child('key').setPriority(10);
Expand All @@ -89,6 +125,18 @@ describe('DataSnapshot', function () {
expect(child.getPriority()).to.equal(10);
});

it('allows array indexes', function () {
var parent = new Snapshot(ref, ['foo', 'bar']);
var child = parent.child(0);
expect(child.val()).to.equal('foo');
});

it('allows array indexes in multiple paths', function () {
var parent = new Snapshot(ref, { key: { array: ['foo', 'bar'] }});
var child = parent.child('key/array/1');
expect(child.val()).to.equal('bar');
});

});

describe('#exists', function () {
Expand Down Expand Up @@ -137,6 +185,25 @@ describe('DataSnapshot', function () {
expect(snapshot.hasChild('bar')).to.equal(false);
});

it('tests for the key starting with /', function () {
var snapshot = new Snapshot(ref, {foo: 'bar'});
expect(snapshot.hasChild('/foo')).to.equal(true);
expect(snapshot.hasChild('/bar')).to.equal(false);
});

it('tests for the key ending with /', function () {
var snapshot = new Snapshot(ref, {foo: 'bar'});
expect(snapshot.hasChild('foo/')).to.equal(true);
expect(snapshot.hasChild('bar/')).to.equal(false);
});

it('tests for the key with multiple paths', function () {
var snapshot = new Snapshot(ref, {key: {foo: 'bar'}});
expect(snapshot.hasChild('key/foo')).to.equal(true);
expect(snapshot.hasChild('key/bar')).to.equal(false);
expect(snapshot.hasChild('foo/key')).to.equal(false);
});

});

describe('#hasChildren', function () {
Expand Down

0 comments on commit dbf77f8

Please sign in to comment.