[database][tests] Add tests for issue 521

This commit is contained in:
Chris Bianca 2017-10-23 12:23:00 +01:00
parent 9e8281c39a
commit a42194a32a
2 changed files with 205 additions and 0 deletions

View File

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

View File

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