Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed accessing DataSnapshot with multiple paths #113

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
60 changes: 60 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,6 +87,24 @@ 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');
Expand All @@ -89,6 +119,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 +179,24 @@ 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);
});

});

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