diff --git a/tests/src/tests/database/ref/issueSpecificTests.js b/tests/src/tests/database/ref/issueSpecificTests.js index 9abbb868..a1afbd78 100644 --- a/tests/src/tests/database/ref/issueSpecificTests.js +++ b/tests/src/tests/database/ref/issueSpecificTests.js @@ -1,4 +1,6 @@ import should from 'should'; +import sinon from 'sinon'; +import 'should-sinon'; import DatabaseContents from '../../support/DatabaseContents'; function issueTests({ describe, it, context, firebase }) { @@ -81,6 +83,190 @@ function issueTests({ describe, it, context, firebase }) { }); }); }); + + describe('issue_521', () => { + context('orderByChild (numerical field) and limitToLast', () => { + it('once() returns correct results', async () => { + // Setup + + const ref = firebase.native.database().ref('tests/issues/521'); + // Test + + return ref + .orderByChild('number') + .limitToLast(2) + .once('value') + .then((snapshot) => { + const val = snapshot.val(); + // Assertion + val.key2.should.eql(DatabaseContents.ISSUES[521].key2); + val.key3.should.eql(DatabaseContents.ISSUES[521].key3); + should.equal(Object.keys(val).length, 2); + + return Promise.resolve(); + }); + }); + + it('on() returns correct initial results', async () => { + // Setup + + const ref = firebase.native.database().ref('tests/issues/521').orderByChild('number').limitToLast(2); + const callback = sinon.spy(); + + // Test + + await new Promise((resolve) => { + ref.on('value', (snapshot) => { + callback(snapshot.val()); + resolve(); + }); + }); + + callback.should.be.calledWith({ + key2: DatabaseContents.ISSUES[521].key2, + key3: DatabaseContents.ISSUES[521].key3, + }); + callback.should.be.calledOnce(); + + return Promise.resolve(); + }); + + it('on() returns correct subsequent results', async () => { + // Setup + + const ref = firebase.native.database().ref('tests/issues/521').orderByChild('number').limitToLast(2); + const callback = sinon.spy(); + + // Test + + await new Promise((resolve) => { + ref.on('value', (snapshot) => { + callback(snapshot.val()); + resolve(); + }); + }); + + callback.should.be.calledWith({ + key2: DatabaseContents.ISSUES[521].key2, + key3: DatabaseContents.ISSUES[521].key3, + }); + callback.should.be.calledOnce(); + + const newDataValue = { + name: 'Item 4', + number: 4, + string: 'item4', + }; + const newRef = firebase.native.database().ref('tests/issues/521/key4'); + await newRef.set(newDataValue); + + await new Promise((resolve) => { + setTimeout(() => resolve(), 5); + }); + + // Assertions + + callback.should.be.calledWith({ + key3: DatabaseContents.ISSUES[521].key3, + key4: newDataValue, + }); + callback.should.be.calledTwice(); + + return Promise.resolve(); + }); + }); + + context('orderByChild (string field) and limitToLast', () => { + it('once() returns correct results', async () => { + // Setup + + const ref = firebase.native.database().ref('tests/issues/521'); + // Test + + return ref + .orderByChild('string') + .limitToLast(2) + .once('value') + .then((snapshot) => { + const val = snapshot.val(); + // Assertion + val.key2.should.eql(DatabaseContents.ISSUES[521].key2); + val.key3.should.eql(DatabaseContents.ISSUES[521].key3); + should.equal(Object.keys(val).length, 2); + + return Promise.resolve(); + }); + }); + + it('on() returns correct initial results', async () => { + // Setup + + const ref = firebase.native.database().ref('tests/issues/521').orderByChild('string').limitToLast(2); + const callback = sinon.spy(); + + // Test + + await new Promise((resolve) => { + ref.on('value', (snapshot) => { + callback(snapshot.val()); + resolve(); + }); + }); + + callback.should.be.calledWith({ + key2: DatabaseContents.ISSUES[521].key2, + key3: DatabaseContents.ISSUES[521].key3, + }); + callback.should.be.calledOnce(); + + return Promise.resolve(); + }); + + it('on() returns correct subsequent results', async () => { + // Setup + + const ref = firebase.native.database().ref('tests/issues/521').orderByChild('string').limitToLast(2); + const callback = sinon.spy(); + + // Test + + await new Promise((resolve) => { + ref.on('value', (snapshot) => { + callback(snapshot.val()); + resolve(); + }); + }); + + callback.should.be.calledWith({ + key2: DatabaseContents.ISSUES[521].key2, + key3: DatabaseContents.ISSUES[521].key3, + }); + callback.should.be.calledOnce(); + + const newDataValue = { + name: 'Item 4', + number: 4, + string: 'item4', + }; + const newRef = firebase.native.database().ref('tests/issues/521/key4'); + await newRef.set(newDataValue); + + await new Promise((resolve) => { + setTimeout(() => resolve(), 5); + }); + + // Assertions + + callback.should.be.calledWith({ + key3: DatabaseContents.ISSUES[521].key3, + key4: newDataValue, + }); + callback.should.be.calledTwice(); + + return Promise.resolve(); + }); + }); + }); } export default issueTests; diff --git a/tests/src/tests/support/DatabaseContents.js b/tests/src/tests/support/DatabaseContents.js index a4a913c9..bace7f99 100644 --- a/tests/src/tests/support/DatabaseContents.js +++ b/tests/src/tests/support/DatabaseContents.js @@ -91,5 +91,24 @@ export default { uid: 'aNYxLexOb2WsXGOPiEAu47q5bxH3', }, }, + + // https://github.com/invertase/react-native-firebase/issues/521 + 521: { + key1: { + name: 'Item 1', + number: 1, + string: 'item1', + }, + key3: { + name: 'Item 3', + number: 3, + string: 'item3', + }, + key2: { + name: 'Item 2', + number: 2, + string: 'item2', + }, + }, }, };