diff --git a/bridge/e2e/init.js b/bridge/e2e/init.js index bf1be32c..bda32f3d 100755 --- a/bridge/e2e/init.js +++ b/bridge/e2e/init.js @@ -7,5 +7,7 @@ before(async () => { }); after(async () => { + console.log('Cleaning up...'); + await TestHelpers.firestore.cleanup(); await detox.cleanup(); }); diff --git a/bridge/helpers/firestore.js b/bridge/helpers/firestore.js index 4e3b2b81..c25c8358 100644 --- a/bridge/helpers/firestore.js +++ b/bridge/helpers/firestore.js @@ -1,10 +1,27 @@ +const TEST_COLLECTION_NAME = 'collection-tests'; + +let shouldCleanup = false; +const ONE_HOUR = 60 * 60 * 1000; + module.exports = { + async cleanup() { + if (!shouldCleanup) return Promise.resolve(); + await module.exports.cleanCollection(TEST_COLLECTION_NAME); + // TODO add any others? + return Promise.resolve(); + }, + + TEST_COLLECTION_NAME, + DOC_1: { name: 'doc1' }, + DOC_1_PATH: `collection-tests/doc1${testRunId}`, DOC_2: { name: 'doc2', title: 'Document 2' }, + DOC_2_PATH: `collection-tests/doc2${testRunId}`, // needs to be a fn as firebase may not yet be available COL_DOC_1() { + shouldCleanup = true; return { baz: true, daz: 123, @@ -19,24 +36,73 @@ module.exports = { }; }, + COL_DOC_1_PATH: `collection-tests/col1${testRunId}`, + COL_DOC_1_ID: `col1${testRunId}`, + /** + * Removes all documents on the collection for the current testId or + * documents older than 24 hours * * @param collectionName * @return {Promise<*>} */ - async cleanCollection(collectionName = 'collection-tests') { + async cleanCollection(collectionName) { const firestore = firebaseAdmin.firestore(); - const batch = firestore.batch(); - const collection = firestore.collection(collectionName); - const docsToDelete = await collection.get(); + const collection = firestore.collection( + collectionName || TEST_COLLECTION_NAME + ); - for (let i = 0, len = docsToDelete.length; i < len; i++) { - const { ref, path } = docsToDelete[i]; - if (path.includes(testRunId)) { - batch.delete(ref); + const docsToDelete = (await collection.get()).docs; + const yesterday = new Date(new Date() - 24 * ONE_HOUR); + + if (docsToDelete.length) { + const batch = firestore.batch(); + + for (let i = 0, len = docsToDelete.length; i < len; i++) { + const { ref } = docsToDelete[i]; + + if ( + ref.path.includes(testRunId) || + new Date(docsToDelete[i].createTime) <= yesterday + ) { + batch.delete(ref); + } } + + return batch.commit(); } - return writeBatch.commit(); + return Promise.resolve(); + }, + + testCollection() { + shouldCleanup = true; + return firebase.firestore().collection(TEST_COLLECTION_NAME); + }, + + testCollectionDoc() { + shouldCleanup = true; + return firebase.firestore().doc(module.exports.COL_DOC_1_PATH); + }, + + testCollectionDocAdmin() { + shouldCleanup = true; + return firebaseAdmin.firestore().doc(module.exports.COL_DOC_1_PATH); + }, + + resetTestCollectionDoc() { + shouldCleanup = true; + return firebase + .firestore() + .doc(module.exports.COL_DOC_1_PATH) + .set(module.exports.COL_DOC_1()); }, }; + +// call a get request without waiting to force firestore to connect +// so the first test isn't delayed whilst connecting +module.exports + .testCollectionDocAdmin() + .get() + .then(() => {}) + .catch(() => {});