[tests] misc config change for promise testing environment

This commit is contained in:
Salakar 2018-09-01 22:20:29 +01:00
parent 1bc26cfdb6
commit 7bd8833e5d
3 changed files with 80 additions and 7 deletions

View File

@ -3,13 +3,8 @@ const config = require('../package.json').detox;
before(async () => {
await detox.init(config);
// needs to be called before any usage of firestore
await firebase.firestore().settings({ persistence: true });
await firebase.firestore().settings({ persistence: false });
});
after(async () => {
console.log('Cleaning up...');
await TestHelpers.firestore.cleanup();
await detox.cleanup();
});

View File

@ -1,8 +1,6 @@
--recursive
--timeout 120000
--reporter list
--slow 600
--bail
--exit
--require jet/platform/node
--require ./helpers

80
tests/helpers/a2a.js Normal file
View File

@ -0,0 +1,80 @@
/**
* Async/Await to Array [error, result]
*
* Examples:
*
* // 0 - standard usage
*
* const [dbError, dbResult] = await A2A(TodoModel.find({ id: 1 }));
*
* // 1) if an error isn't needed:
*
* const [ , dbResult ] = await A2A(TodoModel.find({ id: 1 }));
* // if (!dbResult) return somethingSomethingHandleResult();
*
* // 2) if a result isn't needed:
*
* const [ dbError ] = await A2A(TodoModel.destroy({ id: 1 }));
* if (dbError) return somethingSomethingHandleError();
*
* // 3) if neither error or result are needed:
*
* await A2A(TodoModel.destroy({ id: 1 }));
*
* // 4) multiple promises
*
* const promises = [];
*
* promises.push(Promise.resolve(1));
* promises.push(Promise.resolve(2));
* promises.push(Promise.resolve(3));
*
* const [ , results ] = await A2A(promises);
*
* // 5) non promise values
*
* const [ error, nothingHere ] = await A2A(new Error('Just a foo in a bar world'));
* if (error) profit(); // ?
*
* const [ nothingHere, myThing ] = await A2A('A thing string');
*
*
* @param oOrP Object or Primitive
* @returns {*} Promise<Array>
* @constructor
*/
global.A2A = function A2A(oOrP) {
if (!oOrP) return Promise.resolve([null, oOrP]);
// single promise
if (oOrP.then) {
return oOrP.then(r => [null, r]).catch(e => [e, undefined]);
}
// function that returns a single promise
if (typeof oOrP === 'function') {
return oOrP()
.then(r => [null, r])
.catch(e => [e, undefined]);
}
// array of promises
if (Array.isArray(oOrP) && oOrP.length && oOrP[0].then) {
return Promise.all(oOrP)
.then(r => [null, r])
.catch(e => [e, undefined]);
}
// array of functions that returns a single promise
if (Array.isArray(oOrP) && oOrP.length && typeof oOrP[0] === 'function') {
return Promise.all(oOrP.map(f => f()))
.then(r => [null, r])
.catch(e => [e, undefined]);
}
// non promise values - error
if (oOrP instanceof Error) return Promise.resolve([oOrP, undefined]);
// non promise values - any other value
return Promise.resolve([null, oOrP]);
};