From 7bd8833e5dc75de6ba77e436429347059acb16de Mon Sep 17 00:00:00 2001 From: Salakar Date: Sat, 1 Sep 2018 22:20:29 +0100 Subject: [PATCH] [tests] misc config change for promise testing environment --- tests/e2e/init.js | 5 --- tests/e2e/mocha.opts | 2 -- tests/helpers/a2a.js | 80 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 tests/helpers/a2a.js diff --git a/tests/e2e/init.js b/tests/e2e/init.js index bcdb3519..5326ee52 100755 --- a/tests/e2e/init.js +++ b/tests/e2e/init.js @@ -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(); }); diff --git a/tests/e2e/mocha.opts b/tests/e2e/mocha.opts index 240c1ee9..df34280f 100755 --- a/tests/e2e/mocha.opts +++ b/tests/e2e/mocha.opts @@ -1,8 +1,6 @@ ---recursive --timeout 120000 --reporter list --slow 600 --bail --exit --require jet/platform/node ---require ./helpers diff --git a/tests/helpers/a2a.js b/tests/helpers/a2a.js new file mode 100644 index 00000000..4053b5be --- /dev/null +++ b/tests/helpers/a2a.js @@ -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 + * @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]); +};