Firebase Stub: query parameters not kept between ref calls

This commit is contained in:
gianantoniopini
2021-01-14 09:38:46 +01:00
parent b6a0527fbe
commit 36036cc411
3 changed files with 42 additions and 4 deletions

View File

@ -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);
});
});
});

View File

@ -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;
}

View File

@ -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;
}