mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-05 17:30:46 +08:00
[database] Fix DatabaseQuery/DatabaseQueryModifiers incorrectly mutating previous query instances when chaining
This commit is contained in:
30
packages/database/e2e/query/query.e2e.js
Normal file
30
packages/database/e2e/query/query.e2e.js
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2016-present Invertase Limited & Contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this library except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
describe('DatabaseQuery/DatabaseQueryModifiers', () => {
|
||||
it('should not mutate previous queries (#2691)', async () => {
|
||||
const queryBefore = firebase.database().ref();
|
||||
queryBefore._modifiers._modifiers.length.should.equal(0);
|
||||
|
||||
const queryAfter = queryBefore.orderByChild('age');
|
||||
queryBefore._modifiers._modifiers.length.should.equal(0);
|
||||
queryAfter._modifiers._modifiers.length.should.equal(1);
|
||||
|
||||
const queryAfterAfter = queryAfter.equalTo(30);
|
||||
queryAfter._modifiers._modifiers.length.should.equal(1);
|
||||
queryAfterAfter._modifiers._modifiers.length.should.equal(3); // adds startAt endAt internally
|
||||
});
|
||||
});
|
||||
@@ -80,7 +80,7 @@ export default class DatabaseQuery extends ReferenceBase {
|
||||
);
|
||||
}
|
||||
|
||||
const modifiers = this._modifiers.endAt(value, key);
|
||||
const modifiers = this._modifiers._copy().endAt(value, key);
|
||||
modifiers.validateModifiers('firebase.database().ref().endAt()');
|
||||
|
||||
return new DatabaseQuery(this._database, this.path, modifiers);
|
||||
@@ -155,7 +155,11 @@ export default class DatabaseQuery extends ReferenceBase {
|
||||
);
|
||||
}
|
||||
|
||||
return new DatabaseQuery(this._database, this.path, this._modifiers.limitToFirst(limit));
|
||||
return new DatabaseQuery(
|
||||
this._database,
|
||||
this.path,
|
||||
this._modifiers._copy().limitToFirst(limit),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,7 +180,11 @@ export default class DatabaseQuery extends ReferenceBase {
|
||||
);
|
||||
}
|
||||
|
||||
return new DatabaseQuery(this._database, this.path, this._modifiers.limitToLast(limit));
|
||||
return new DatabaseQuery(
|
||||
this._database,
|
||||
this.path,
|
||||
this._modifiers._copy().limitToLast(limit),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -369,7 +377,7 @@ export default class DatabaseQuery extends ReferenceBase {
|
||||
);
|
||||
}
|
||||
|
||||
const modifiers = this._modifiers.toArray();
|
||||
const modifiers = this._modifiers._copy().toArray();
|
||||
|
||||
return this._database.native
|
||||
.once(this.path, modifiers, eventType)
|
||||
@@ -425,7 +433,7 @@ export default class DatabaseQuery extends ReferenceBase {
|
||||
);
|
||||
}
|
||||
|
||||
const modifiers = this._modifiers.orderByChild(path);
|
||||
const modifiers = this._modifiers._copy().orderByChild(path);
|
||||
modifiers.validateModifiers('firebase.database().ref().orderByChild()');
|
||||
|
||||
return new DatabaseQuery(this._database, this.path, modifiers);
|
||||
@@ -441,7 +449,7 @@ export default class DatabaseQuery extends ReferenceBase {
|
||||
);
|
||||
}
|
||||
|
||||
const modifiers = this._modifiers.orderByKey();
|
||||
const modifiers = this._modifiers._copy().orderByKey();
|
||||
modifiers.validateModifiers('firebase.database().ref().orderByKey()');
|
||||
|
||||
return new DatabaseQuery(this._database, this.path, modifiers);
|
||||
@@ -457,7 +465,7 @@ export default class DatabaseQuery extends ReferenceBase {
|
||||
);
|
||||
}
|
||||
|
||||
const modifiers = this._modifiers.orderByPriority();
|
||||
const modifiers = this._modifiers._copy().orderByPriority();
|
||||
modifiers.validateModifiers('firebase.database().ref().orderByPriority()');
|
||||
|
||||
return new DatabaseQuery(this._database, this.path, modifiers);
|
||||
@@ -473,7 +481,7 @@ export default class DatabaseQuery extends ReferenceBase {
|
||||
);
|
||||
}
|
||||
|
||||
const modifiers = this._modifiers.orderByValue();
|
||||
const modifiers = this._modifiers._copy().orderByValue();
|
||||
modifiers.validateModifiers('firebase.database().ref().orderByValue()');
|
||||
|
||||
return new DatabaseQuery(this._database, this.path, modifiers);
|
||||
@@ -498,7 +506,7 @@ export default class DatabaseQuery extends ReferenceBase {
|
||||
);
|
||||
}
|
||||
|
||||
const modifiers = this._modifiers.startAt(value, key);
|
||||
const modifiers = this._modifiers._copy().startAt(value, key);
|
||||
modifiers.validateModifiers('firebase.database().ref().startAt()');
|
||||
|
||||
return new DatabaseQuery(this._database, this.path, modifiers);
|
||||
|
||||
@@ -31,6 +31,16 @@ export default class DatabaseQueryModifiers {
|
||||
this._modifiers = [];
|
||||
}
|
||||
|
||||
_copy() {
|
||||
const newInstance = new DatabaseQueryModifiers();
|
||||
newInstance._limit = this._limit;
|
||||
newInstance._orderBy = this._orderBy;
|
||||
newInstance._startAt = this._startAt;
|
||||
newInstance._endAt = this._endAt;
|
||||
newInstance._modifiers = [...this._modifiers];
|
||||
return newInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* LIMIT
|
||||
|
||||
Reference in New Issue
Block a user