2
0
mirror of synced 2025-01-14 08:25:04 +00:00

[firestore][DocumentSnapshot] explicitly bind context on data/get methods to keep context during destructuring (includes tests)

This commit is contained in:
Salakar 2018-04-13 11:50:30 +01:00
parent ccb5f70dc1
commit c14e60017e
2 changed files with 51 additions and 7 deletions
bridge/e2e/firestore
lib/modules/firestore

@ -24,14 +24,60 @@ describe('firestore()', () => {
});
});
describe('get()', () => {
it('using a dot notated path string', async () => {
describe('metadata', () => {
it('returns an object of meta data', async () => {
const { testCollectionDoc } = TestHelpers.firestore;
const { metadata } = await testCollectionDoc().get();
metadata.should.be.an.Object();
metadata.should.have.property('hasPendingWrites');
metadata.should.have.property('fromCache');
metadata.hasPendingWrites.should.be.a.Boolean();
metadata.fromCache.should.be.a.Boolean();
});
});
describe('exists', () => {
it('returns a boolean', async () => {
const { testCollectionDoc } = TestHelpers.firestore;
const { exists } = await testCollectionDoc().get();
exists.should.be.a.Boolean();
exists.should.be.true();
});
});
describe('data()', () => {
it('returns document data', async () => {
// additionally tests context binding not lost during destructuring
const { testCollectionDoc } = TestHelpers.firestore;
const snapshot = await testCollectionDoc().get();
const { data } = snapshot;
snapshot.data.should.be.a.Function();
data.should.be.a.Function();
snapshot.data().should.be.a.Object();
data().should.be.a.Object();
snapshot.data().baz.should.be.true();
data().baz.should.be.true();
});
});
describe('get()', () => {
it('using a dot notated path string', async () => {
// additionally tests context binding not lost during destructuring
const { testCollectionDoc } = TestHelpers.firestore;
const snapshot = await testCollectionDoc().get();
const { get } = snapshot;
should.equal(snapshot.get('foo'), 'bar');
should.equal(get('foo'), 'bar');
should.equal(snapshot.get('object.daz'), 123);
should.equal(get('object.daz'), 123);
should.equal(snapshot.get('nonexistent.object'), undefined);
should.equal(get('nonexistent.object'), undefined);
});
it('using a FieldPath instance', async () => {

@ -55,15 +55,13 @@ export default class DocumentSnapshot {
return this._ref;
}
data(): Object | void {
return this._data;
}
data = (): Object | void => this._data;
get(fieldPath: string | FieldPath): any {
get = (fieldPath: string | FieldPath): any => {
if (fieldPath instanceof FieldPath) {
return extractFieldPathData(this._data, fieldPath._segments);
}
return deepGet(this._data, fieldPath, '.');
}
};
}