[tests][database] misc ref tests

This commit is contained in:
Salakar 2018-04-22 16:04:35 +01:00
parent 8b90b5a7b3
commit 3b634a8dfc
4 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,46 @@
const { CONTENTS, setDatabaseContents } = TestHelpers.database;
describe('database()', () => {
before(() => setDatabaseContents());
describe('ref().once()', () => {
it('returns a promise', () => {
const ref = firebase.database().ref('tests/types/number');
const returnValue = ref.once('value');
returnValue.should.be.Promise();
});
it('resolves with the correct value', async () => {
await Promise.all(
Object.keys(CONTENTS.DEFAULT).map(dataRef => {
const dataTypeValue = CONTENTS.DEFAULT[dataRef];
const ref = firebase.database().ref(`tests/types/${dataRef}`);
return ref.once('value').then(snapshot => {
snapshot.val().should.eql(bridge.contextify(dataTypeValue));
});
})
);
});
it('is NOT called when the value is changed', async () => {
const callback = sinon.spy();
const ref = firebase.database().ref('tests/types/number');
ref.once('value').then(callback);
await ref.set(CONTENTS.NEW.number);
callback.should.be.calledOnce();
});
it('errors if permission denied', async () => {
const reference = firebase.database().ref('nope');
try {
await reference.once('value');
} catch (error) {
error.code.includes('database/permission-denied').should.be.true();
return true;
}
throw new Error('No permission denied error');
});
});
});

View File

@ -0,0 +1,52 @@
const { setDatabaseContents, CONTENTS } = TestHelpers.database;
describe('database()', () => {
beforeEach(() => setDatabaseContents());
describe('ref.set()', () => {
it('returns a promise', async () => {
const ref = firebase.database().ref('tests/types/number');
const returnValue = ref.set(CONTENTS.DEFAULT.number);
returnValue.should.be.Promise();
const value = await returnValue;
(value === null).should.be.true();
});
it('changes value', async () => {
await Promise.all(
Object.keys(CONTENTS.DEFAULT).map(async dataRef => {
const previousValue = bridge.contextify(CONTENTS.DEFAULT[dataRef]);
const ref = firebase.database().ref(`tests/types/${dataRef}`);
const snapshot = await ref.once('value');
snapshot.val().should.eql(previousValue);
const newValue = bridge.contextify(CONTENTS.NEW[dataRef]);
await ref.set(newValue);
const snapshot2 = await ref.once('value');
snapshot2.val().should.eql(newValue);
})
);
});
it('can unset values', async () => {
await Promise.all(
Object.keys(CONTENTS.DEFAULT).map(async dataRef => {
const previousValue = bridge.contextify(CONTENTS.DEFAULT[dataRef]);
const ref = firebase.database().ref(`tests/types/${dataRef}`);
const snapshot = await ref.once('value');
snapshot.val().should.eql(previousValue);
await ref.set(null);
const snapshot2 = await ref.once('value');
(snapshot2.val() === null).should.be.true();
})
);
});
});
});

View File

@ -1,6 +1,7 @@
const CONTENTS = require('./content');
module.exports = {
CONTENTS,
setDatabaseContents() {
const database = firebaseAdmin.database();
return Promise.all([

View File

@ -9,6 +9,12 @@ Object.defineProperty(global, 'firebase', {
},
});
global.isObject = function isObject(item) {
return item
? typeof item === 'object' && !Array.isArray(item) && item !== null
: false;
};
global.sleep = duration =>
new Promise(resolve => setTimeout(resolve, duration));