[tests][database] fix tests
This commit is contained in:
parent
5ccbd9f369
commit
0737f53dea
|
@ -2,10 +2,9 @@ import sinon from 'sinon';
|
||||||
import 'should-sinon';
|
import 'should-sinon';
|
||||||
import Promise from 'bluebird';
|
import Promise from 'bluebird';
|
||||||
|
|
||||||
import RNFirebase from './../../../../../firebase/firebase';
|
|
||||||
import DatabaseContents from '../../../support/DatabaseContents';
|
import DatabaseContents from '../../../support/DatabaseContents';
|
||||||
|
|
||||||
function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
function onTests({ describe, context, it, firebase, tryCatch }) {
|
||||||
describe('ref().on(\'value\')', () => {
|
describe('ref().on(\'value\')', () => {
|
||||||
// Documented Web API Behaviour
|
// Documented Web API Behaviour
|
||||||
it('returns the success callback', () => {
|
it('returns the success callback', () => {
|
||||||
|
@ -45,6 +44,12 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
|
|
||||||
await ref.set(1);
|
await ref.set(1);
|
||||||
|
|
||||||
|
// wait for the set to register internally, they're events
|
||||||
|
// so not immediately available on the next event loop - only need to this do for tests
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
setTimeout(() => resolve(), 15);
|
||||||
|
});
|
||||||
|
|
||||||
callback.should.be.calledWith(1);
|
callback.should.be.calledWith(1);
|
||||||
|
|
||||||
// Teardown
|
// Teardown
|
||||||
|
@ -76,6 +81,10 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
const newDataValue = DatabaseContents.NEW[dataRef];
|
const newDataValue = DatabaseContents.NEW[dataRef];
|
||||||
await ref.set(newDataValue);
|
await ref.set(newDataValue);
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
setTimeout(() => resolve(), 5);
|
||||||
|
});
|
||||||
|
|
||||||
// Assertions
|
// Assertions
|
||||||
|
|
||||||
callback.should.be.calledWith(newDataValue);
|
callback.should.be.calledWith(newDataValue);
|
||||||
|
@ -84,7 +93,7 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
// Tear down
|
// Tear down
|
||||||
|
|
||||||
ref.off();
|
ref.off();
|
||||||
await ref.set(currentDataValue);
|
return ref.set(currentDataValue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -108,6 +117,9 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
const childRef = firebase.native.database().ref('tests/types/object/foo2');
|
const childRef = firebase.native.database().ref('tests/types/object/foo2');
|
||||||
await childRef.set(newDataValue);
|
await childRef.set(newDataValue);
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
setTimeout(() => resolve(), 5);
|
||||||
|
});
|
||||||
// Assertions
|
// Assertions
|
||||||
|
|
||||||
callback.should.be.calledWith({
|
callback.should.be.calledWith({
|
||||||
|
@ -120,7 +132,7 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
// Tear down
|
// Tear down
|
||||||
|
|
||||||
ref.off();
|
ref.off();
|
||||||
await ref.set(currentDataValue);
|
return ref.set(currentDataValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls callback when child of the ref is added', async () => {
|
it('calls callback when child of the ref is added', async () => {
|
||||||
|
@ -143,26 +155,25 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
const newElementRef = ref.push();
|
const newElementRef = ref.push();
|
||||||
newKey = newElementRef.key;
|
newKey = newElementRef.key;
|
||||||
newElementRef.set(37);
|
newElementRef.set(37);
|
||||||
} else {
|
} else if (!calledTwice) {
|
||||||
if (!calledTwice) {
|
calledTwice = true;
|
||||||
calledTwice = true;
|
callbackAfterSet(snapshot.val());
|
||||||
callbackAfterSet(snapshot.val());
|
const arrayAsObject = currentDataValue.reduce((memo, element, index) => {
|
||||||
const arrayAsObject = currentDataValue.reduce((memo, element, index) => {
|
// eslint-disable-next-line no-param-reassign
|
||||||
memo[index] = element;
|
memo[index] = element;
|
||||||
return memo;
|
return memo;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
// Assertions
|
// Assertions
|
||||||
callbackAfterSet.should.be.calledWith({
|
callbackAfterSet.should.be.calledWith({
|
||||||
...arrayAsObject,
|
...arrayAsObject,
|
||||||
[newKey]: 37,
|
[newKey]: 37,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Tear down
|
// Tear down
|
||||||
ref.off(); // TODO
|
ref.off(); // TODO
|
||||||
ref.set(currentDataValue).then(() => resolve()).catch(() => reject());
|
ref.set(currentDataValue).then(() => resolve()).catch(() => reject());
|
||||||
} // todo throw new Error('On listener called more than two times, expects no more than 2 calls');
|
} // todo throw new Error('On listener called more than two times, expects no more than 2 calls');
|
||||||
}
|
|
||||||
}, reject));
|
}, reject));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -186,6 +197,9 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
|
|
||||||
await ref.set(currentDataValue);
|
await ref.set(currentDataValue);
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
setTimeout(() => resolve(), 5);
|
||||||
|
});
|
||||||
// Assertions
|
// Assertions
|
||||||
|
|
||||||
callback.should.be.calledOnce(); // Callback is not called again
|
callback.should.be.calledOnce(); // Callback is not called again
|
||||||
|
@ -231,6 +245,10 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
const newDataValue = DatabaseContents.NEW[dataRef];
|
const newDataValue = DatabaseContents.NEW[dataRef];
|
||||||
await ref.set(newDataValue);
|
await ref.set(newDataValue);
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
setTimeout(() => resolve(), 5);
|
||||||
|
});
|
||||||
|
|
||||||
callbackA.should.be.calledWith(newDataValue);
|
callbackA.should.be.calledWith(newDataValue);
|
||||||
callbackB.should.be.calledWith(newDataValue);
|
callbackB.should.be.calledWith(newDataValue);
|
||||||
|
|
||||||
|
@ -240,6 +258,7 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
// Tear down
|
// Tear down
|
||||||
|
|
||||||
ref.off();
|
ref.off();
|
||||||
|
return Promise.resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -273,7 +292,7 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
const ref = firebase.native.database().ref(`tests/types/${dataRef}`);
|
const ref = firebase.native.database().ref(`tests/types/${dataRef}`);
|
||||||
const currentDataValue = DatabaseContents.DEFAULT[dataRef];
|
const currentDataValue = DatabaseContents.DEFAULT[dataRef];
|
||||||
|
|
||||||
const context = {
|
const cbContext = {
|
||||||
callCount: 0,
|
callCount: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -284,24 +303,28 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
this.value = snapshot.val();
|
this.value = snapshot.val();
|
||||||
this.callCount += 1;
|
this.callCount += 1;
|
||||||
resolve();
|
resolve();
|
||||||
}, context);
|
}, cbContext);
|
||||||
});
|
});
|
||||||
|
|
||||||
context.value.should.eql(currentDataValue);
|
cbContext.value.should.eql(currentDataValue);
|
||||||
context.callCount.should.eql(1);
|
cbContext.callCount.should.eql(1);
|
||||||
|
|
||||||
const newDataValue = DatabaseContents.NEW[dataRef];
|
const newDataValue = DatabaseContents.NEW[dataRef];
|
||||||
await ref.set(newDataValue);
|
await ref.set(newDataValue);
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
setTimeout(() => resolve(), 5);
|
||||||
|
});
|
||||||
|
|
||||||
// Assertions
|
// Assertions
|
||||||
|
|
||||||
context.value.should.eql(newDataValue);
|
cbContext.value.should.eql(newDataValue);
|
||||||
context.callCount.should.eql(2);
|
cbContext.callCount.should.eql(2);
|
||||||
|
|
||||||
// Tear down
|
// Tear down
|
||||||
|
|
||||||
ref.off();
|
ref.off();
|
||||||
await ref.set(currentDataValue);
|
return ref.set(currentDataValue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -316,11 +339,14 @@ function onTests({ describe, context, it, fit, firebase, tryCatch }) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
invalidRef.on('value', callback, tryCatch((error) => {
|
invalidRef.on('value', callback, tryCatch((error) => {
|
||||||
error.message.should.eql(
|
error.message.should.eql(
|
||||||
`Database: Client doesn't have permission to access the desired data. (database/permission-denied).`,
|
'Database: Client doesn\'t have permission to access the desired data. (database/permission-denied).',
|
||||||
);
|
);
|
||||||
|
|
||||||
error.code.should.eql('DATABASE/PERMISSION-DENIED');
|
error.code.should.eql('DATABASE/PERMISSION-DENIED');
|
||||||
error.path.should.eql('nope');
|
|
||||||
error.appName.should.eql(RNFirebase.DEFAULT_APP_NAME);
|
// test ref matches
|
||||||
|
error.ref.path.should.eql(invalidRef.path);
|
||||||
|
|
||||||
|
|
||||||
callback.should.not.be.called();
|
callback.should.not.be.called();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue