2
0
mirror of synced 2025-01-20 03:09:51 +00:00

[tests][firestore] transaction tests

This commit is contained in:
Salakar 2018-04-16 17:45:32 +01:00
parent ead1edff4e
commit 391403a7ec

View File

@ -1,77 +1,161 @@
const should = require('should'); const { testDocRef } = TestHelpers.firestore;
describe('firestore()', () => { describe('firestore()', () => {
describe('runTransaction()', () => { describe('runTransaction()', () => {
it('should set, update and delete transactionally and allow a return value', async () => { it('should set() values', async () => {
let deleteMe = false;
const firestore = firebase.firestore(); const firestore = firebase.firestore();
const docRef = testDocRef('tSet');
const docRef = firestore
.collection('transactions')
.doc(Date.now().toString());
const updateFunction = async transaction => { const updateFunction = async transaction => {
const doc = await transaction.get(docRef); const doc = await transaction.get(docRef);
if (doc.exists && deleteMe) {
transaction.delete(docRef);
return 'bye';
}
if (!doc.exists) { if (!doc.exists) {
transaction.set(docRef, { value: 1 }); transaction.set(docRef, { value: 1, somethingElse: 'set' });
return 1; return 1;
} }
const newValue = doc.data().value + 1; return 0;
if (newValue > 2) {
return Promise.reject(
new Error('Value should not be greater than 2!')
);
}
transaction.update(docRef, {
value: newValue,
somethingElse: 'update',
});
return newValue;
}; };
// set tests const result = await firestore.runTransaction(updateFunction);
const val1 = await firestore.runTransaction(updateFunction); should.equal(result, 1);
should.equal(val1, 1); const finalDoc = await docRef.get();
const doc1 = await docRef.get(); finalDoc.data().value.should.equal(1);
doc1.data().value.should.equal(1); finalDoc.data().somethingElse.should.equal('set');
should.equal(doc1.data().somethingElse, undefined); });
// update it('should update() values', async () => {
const val2 = await firestore.runTransaction(updateFunction); const firestore = firebase.firestore();
should.equal(val2, 2); const docRef = testDocRef('tUpdate');
const doc2 = await docRef.get();
doc2.data().value.should.equal(2);
doc2.data().somethingElse.should.equal('update');
// rejecting / cancelling transaction await docRef.set({ value: 1 });
const updateFunction = async transaction => {
const doc = await transaction.get(docRef);
if (doc.exists) {
transaction.update(docRef, {
value: doc.data().value + 1,
somethingElse: 'update',
});
return 1;
}
return 0;
};
const result = await firestore.runTransaction(updateFunction);
should.equal(result, 1);
const finalDoc = await docRef.get();
finalDoc.data().value.should.equal(2);
finalDoc.data().somethingElse.should.equal('update');
});
it('should delete() values', async () => {
const firestore = firebase.firestore();
const docRef = testDocRef('tDelete');
await docRef.set({ value: 1, somethingElse: 'delete' });
const updateFunction = async transaction => {
const doc = await transaction.get(docRef);
if (doc.exists) {
transaction.delete(docRef);
return 1;
}
return 0;
};
const result = await firestore.runTransaction(updateFunction);
should.equal(result, 1);
const finalDoc = await docRef.get();
finalDoc.exists.should.equal(false);
});
it('error if updateFn does return a promise', async () => {
const firestore = firebase.firestore();
// test async functions - they always return a promise in JS
let didReject = false; let didReject = false;
let updateFunction = async () => 1;
try { try {
await firestore.runTransaction(updateFunction); await firestore.runTransaction(updateFunction);
} catch (e) { } catch (e) {
didReject = true; didReject = true;
} }
should.equal(didReject, false);
// should not error as a promise returned
didReject = false;
updateFunction = () => Promise.resolve();
try {
await firestore.runTransaction(updateFunction);
} catch (e) {
didReject = true;
}
should.equal(didReject, false);
// should error as no promise returned
didReject = false;
updateFunction = () => '123456';
try {
await firestore.runTransaction(updateFunction);
} catch (e) {
didReject = true;
e.message.includes('must return a Promise');
}
should.equal(didReject, true); should.equal(didReject, true);
const doc3 = await docRef.get(); });
doc3.data().value.should.equal(2);
doc3.data().somethingElse.should.equal('update');
// delete it('updateFn promise rejections / js exceptions handled', async () => {
deleteMe = true; const firestore = firebase.firestore();
const val4 = await firestore.runTransaction(updateFunction);
should.equal(val4, 'bye');
const doc4 = await docRef.get();
should.equal(doc4.exists, false);
return Promise.resolve('Test Completed'); // rejections
let didReject = false;
let updateFunction = () => Promise.reject('shoop');
try {
await firestore.runTransaction(updateFunction);
} catch (e) {
didReject = true;
should.equal(e, 'shoop');
}
should.equal(didReject, true);
// exceptions
didReject = false;
updateFunction = () => {
// eslint-disable-next-line no-throw-literal
throw 'doop';
};
try {
await firestore.runTransaction(updateFunction);
} catch (e) {
didReject = true;
should.equal(e, 'doop');
}
should.equal(didReject, true);
});
it('handle native exceptions', async () => {
const firestore = firebase.firestore();
const docRef = testDocRef('tSet');
const blockedRef = firestore.doc('blocked-collection/foo');
const updateFunction = async transaction => {
await transaction.get(docRef);
transaction.set(blockedRef, { value: 1, somethingElse: 'set' });
return 1;
};
// rejections
let didReject = false;
try {
await firestore.runTransaction(updateFunction);
} catch (e) {
e.message.should.containEql('firestore/failed-precondition');
didReject = true;
}
should.equal(didReject, true);
}); });
}); });
}); });