diff --git a/__mocks__/__tests__/gatsby-plugin-firebase.test.js b/__mocks__/__tests__/gatsby-plugin-firebase.test.js index aff8a2c8..8ba2b89c 100644 --- a/__mocks__/__tests__/gatsby-plugin-firebase.test.js +++ b/__mocks__/__tests__/gatsby-plugin-firebase.test.js @@ -255,5 +255,30 @@ describe('FirebaseStub', () => { expect(resume.user).toEqual(DatabaseConstants.user1.uid), ); }); + + it('previously set query parameters are not kept when retrieving reference again', async () => { + let reference = null; + + reference = FirebaseStub.database().ref(DatabaseConstants.resumesPath); + expect(reference).toBeTruthy(); + const { uuid } = reference; + expect(reference.orderByChildPath).toHaveLength(0); + expect(reference.equalToValue).toHaveLength(0); + + reference = FirebaseStub.database() + .ref(DatabaseConstants.resumesPath) + .orderByChild('user') + .equalTo('testuser1'); + expect(reference).toBeTruthy(); + expect(reference.uuid).toBe(uuid); + expect(reference.orderByChildPath).toBe('user'); + expect(reference.equalToValue).toBe('testuser1'); + + reference = FirebaseStub.database().ref(DatabaseConstants.resumesPath); + expect(reference).toBeTruthy(); + expect(reference.uuid).toBe(uuid); + expect(reference.orderByChildPath).toHaveLength(0); + expect(reference.equalToValue).toHaveLength(0); + }); }); }); diff --git a/__mocks__/gatsby-plugin-firebase/database/database.js b/__mocks__/gatsby-plugin-firebase/database/database.js index c746858a..58cc0cb8 100644 --- a/__mocks__/gatsby-plugin-firebase/database/database.js +++ b/__mocks__/gatsby-plugin-firebase/database/database.js @@ -78,6 +78,7 @@ class Database { const newRef = new Reference(referencePath, () => this._data); const existingRef = this._references[newRef.path]; if (existingRef) { + existingRef.initializeQueryParameters(); return existingRef; } diff --git a/__mocks__/gatsby-plugin-firebase/database/reference.js b/__mocks__/gatsby-plugin-firebase/database/reference.js index 7427c678..b5a5466e 100644 --- a/__mocks__/gatsby-plugin-firebase/database/reference.js +++ b/__mocks__/gatsby-plugin-firebase/database/reference.js @@ -29,8 +29,7 @@ class Reference { this._getDatabaseData = getDatabaseData; - this._orderByChildPath = ''; - this._equalToValue = ''; + this.initializeQueryParameters(); } get path() { @@ -41,6 +40,14 @@ class Reference { return this._uuid; } + get orderByChildPath() { + return this._orderByChildPath; + } + + get equalToValue() { + return this._equalToValue; + } + getData() { const databaseData = this._getDatabaseData(); @@ -59,10 +66,10 @@ class Reference { ) { data = this.path in databaseData ? databaseData[this.path] : null; - if (data && this._orderByChildPath && this._equalToValue) { + if (data && this.orderByChildPath && this.equalToValue) { return Object.fromEntries( Object.entries(data).filter( - ([, value]) => value[this._orderByChildPath] === this._equalToValue, + ([, value]) => value[this.orderByChildPath] === this.equalToValue, ), ); } @@ -92,6 +99,11 @@ class Reference { return null; } + initializeQueryParameters() { + this._orderByChildPath = ''; + this._equalToValue = ''; + } + off() { return this !== null; }