From 0ace49ca8144be65fca2d259bff253477205b2ba Mon Sep 17 00:00:00 2001 From: Chris Bianca Date: Fri, 26 Jan 2018 11:55:37 +0000 Subject: [PATCH] [database] Fix wrong path from snapshot reference #679 --- lib/modules/database/query.js | 6 +--- lib/modules/database/reference.js | 4 +-- lib/utils/ReferenceBase.js | 9 +++++- .../tests/database/ref/issueSpecificTests.js | 28 +++++++++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/lib/modules/database/query.js b/lib/modules/database/query.js index cc200e27..974dba85 100644 --- a/lib/modules/database/query.js +++ b/lib/modules/database/query.js @@ -16,11 +16,7 @@ export default class Query { _reference: Reference; modifiers: Array; - constructor( - ref: Reference, - path: string, - existingModifiers?: Array - ) { + constructor(ref: Reference, existingModifiers?: Array) { this.modifiers = existingModifiers ? [...existingModifiers] : []; this._reference = ref; } diff --git a/lib/modules/database/reference.js b/lib/modules/database/reference.js index 739f6121..b91e6f13 100644 --- a/lib/modules/database/reference.js +++ b/lib/modules/database/reference.js @@ -89,7 +89,7 @@ export default class Reference extends ReferenceBase { this._promise = null; this._refListeners = {}; this._database = database; - this._query = new Query(this, path, existingModifiers); + this._query = new Query(this, existingModifiers); getLogger(database).debug('Created new Reference', this._getRefKey()); } @@ -493,7 +493,7 @@ export default class Reference extends ReferenceBase { * @returns {string} */ toString(): string { - return this.path; + return `${this._database.app.options.databaseURL}/${this.path}`; } /** diff --git a/lib/utils/ReferenceBase.js b/lib/utils/ReferenceBase.js index f774e230..32056422 100644 --- a/lib/utils/ReferenceBase.js +++ b/lib/utils/ReferenceBase.js @@ -5,7 +5,14 @@ export default class ReferenceBase { path: string; constructor(path: string) { - this.path = path || '/'; + if (path) { + this.path = + path.length > 1 && path.endsWith('/') + ? path.substring(0, path.length - 1) + : path; + } else { + this.path = '/'; + } } /** diff --git a/tests/src/tests/database/ref/issueSpecificTests.js b/tests/src/tests/database/ref/issueSpecificTests.js index d2d457d7..1752e727 100644 --- a/tests/src/tests/database/ref/issueSpecificTests.js +++ b/tests/src/tests/database/ref/issueSpecificTests.js @@ -315,6 +315,34 @@ function issueTests({ describe, it, context, firebase }) { }); }); }); + + describe('issue_679', () => { + context('path from snapshot reference', () => { + it('should match web SDK', async () => { + // Setup + const nativeRef = firebase.native.database().ref('tests/issues/679'); + const webRef = firebase.web.database().ref('tests/issues/679'); + const nativeRef2 = firebase.native.database().ref('tests/issues/679/'); + const webRef2 = firebase.web.database().ref('tests/issues/679/'); + + // Test + + webRef.toString().should.equal(nativeRef.toString()); + webRef2.toString().should.equal(nativeRef2.toString()); + }); + + it('should be correct when returned from native', async () => { + // Setup + const nativeRef = firebase.native.database().ref('tests/issues/679/'); + const webRef = firebase.web.database().ref('tests/issues/679/'); + + const nativeSnapshot = await nativeRef.once('value'); + const webSnapshot = await webRef.once('value'); + + webSnapshot.ref.toString().should.equal(nativeSnapshot.ref.toString()); + }); + }); + }); } export default issueTests;