2018-04-21 23:52:58 +00:00
|
|
|
const { setDatabaseContents } = TestHelpers.database;
|
|
|
|
|
2018-04-22 01:04:18 +00:00
|
|
|
// TODO use testRunId in refs to prevent multiple test instances interfering with each other
|
2018-04-21 23:52:58 +00:00
|
|
|
describe('database()', () => {
|
|
|
|
before(() => setDatabaseContents());
|
|
|
|
describe('ref.transaction()', () => {
|
|
|
|
it('increments a value', async () => {
|
|
|
|
let valueBefore = 1;
|
|
|
|
const ref = firebase.database().ref('tests/transaction');
|
|
|
|
|
|
|
|
const { committed, snapshot } = await ref.transaction(currentData => {
|
|
|
|
if (currentData === null) {
|
|
|
|
return valueBefore + 10;
|
|
|
|
}
|
|
|
|
valueBefore = currentData;
|
|
|
|
return valueBefore + 10;
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
should.equal(committed, true, 'Transaction did not commit.');
|
|
|
|
snapshot.val().should.equal(valueBefore + 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('aborts if undefined returned', async () => {
|
|
|
|
const ref = firebase.database().ref('tests/transaction');
|
|
|
|
|
|
|
|
const { committed } = await ref.transaction(() => undefined, true);
|
|
|
|
should.equal(
|
|
|
|
committed,
|
|
|
|
false,
|
|
|
|
'Transaction committed and did not abort.'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|