diff --git a/tests/src/tests/firestore/collectionReferenceTests.js b/tests/src/tests/firestore/collectionReferenceTests.js index 7234b9d6..fd879acb 100644 --- a/tests/src/tests/firestore/collectionReferenceTests.js +++ b/tests/src/tests/firestore/collectionReferenceTests.js @@ -2,9 +2,10 @@ import sinon from 'sinon'; import 'should-sinon'; import should from 'should'; -import { COL_1, cleanCollection } from './index'; +import { cleanCollection, COL_DOC_1 } from './data'; function collectionReferenceTests({ + beforeEach, describe, it, context, @@ -13,6 +14,18 @@ function collectionReferenceTests({ after, }) { describe('CollectionReference', () => { + let collectionTestsCollection; + beforeEach(async () => { + collectionTestsCollection = firebase.native + .firestore() + .collection('collection-tests'); + + // We clean as part of initialisation in case a test errors + // We don't clean after the test as it slows tests significantly + await cleanCollection(collectionTestsCollection); + await collectionTestsCollection.doc('col1').set(COL_DOC_1); + }); + context('class', () => { it('should return instance methods', () => new Promise(resolve => { @@ -131,7 +144,7 @@ function collectionReferenceTests({ const collectionRef = firebase.native .firestore() .collection('collection-tests'); - const newDocValue = { ...COL_1, foo: 'updated' }; + const newDocValue = { ...COL_DOC_1, foo: 'updated' }; const callback = sinon.spy(); @@ -145,7 +158,7 @@ function collectionReferenceTests({ }); }); - callback.should.be.calledWith(COL_1); + callback.should.be.calledWith(COL_DOC_1); const docRef = firebase.native.firestore().doc('collection-tests/col1'); await docRef.set(newDocValue); @@ -182,7 +195,7 @@ function collectionReferenceTests({ }); }); - callback.should.be.calledWith(COL_1); + callback.should.be.calledWith(COL_DOC_1); const docRef = firebase.native.firestore().doc('collection-tests/col2'); await docRef.set(newDocValue); @@ -193,7 +206,7 @@ function collectionReferenceTests({ // Assertions - callback.should.be.calledWith(COL_1); + callback.should.be.calledWith(COL_DOC_1); callback.should.be.calledWith(newDocValue); callback.should.be.calledThrice(); @@ -219,10 +232,10 @@ function collectionReferenceTests({ }); }); - callback.should.be.calledWith(COL_1); + callback.should.be.calledWith(COL_DOC_1); const docRef = firebase.native.firestore().doc('collection-tests/col1'); - await docRef.set(COL_1); + await docRef.set(COL_DOC_1); await new Promise(resolve2 => { setTimeout(() => resolve2(), 5); @@ -242,7 +255,7 @@ function collectionReferenceTests({ const collectionRef = firebase.native .firestore() .collection('collection-tests'); - const newDocValue = { ...COL_1, foo: 'updated' }; + const newDocValue = { ...COL_DOC_1, foo: 'updated' }; const callbackA = sinon.spy(); const callbackB = sinon.spy(); @@ -263,10 +276,10 @@ function collectionReferenceTests({ }); }); - callbackA.should.be.calledWith(COL_1); + callbackA.should.be.calledWith(COL_DOC_1); callbackA.should.be.calledOnce(); - callbackB.should.be.calledWith(COL_1); + callbackB.should.be.calledWith(COL_DOC_1); callbackB.should.be.calledOnce(); const docRef = firebase.native.firestore().doc('collection-tests/col1'); @@ -293,7 +306,7 @@ function collectionReferenceTests({ const collectionRef = firebase.native .firestore() .collection('collection-tests'); - const newDocValue = { ...COL_1, foo: 'updated' }; + const newDocValue = { ...COL_DOC_1, foo: 'updated' }; const callbackA = sinon.spy(); const callbackB = sinon.spy(); @@ -314,10 +327,10 @@ function collectionReferenceTests({ }); }); - callbackA.should.be.calledWith(COL_1); + callbackA.should.be.calledWith(COL_DOC_1); callbackA.should.be.calledOnce(); - callbackB.should.be.calledWith(COL_1); + callbackB.should.be.calledWith(COL_DOC_1); callbackB.should.be.calledOnce(); const docRef = firebase.native.firestore().doc('collection-tests/col1'); @@ -337,13 +350,13 @@ function collectionReferenceTests({ unsubscribeA(); - await docRef.set(COL_1); + await docRef.set(COL_DOC_1); await new Promise(resolve2 => { setTimeout(() => resolve2(), 5); }); - callbackB.should.be.calledWith(COL_1); + callbackB.should.be.calledWith(COL_DOC_1); callbackA.should.be.calledTwice(); callbackB.should.be.calledThrice(); @@ -366,7 +379,7 @@ function collectionReferenceTests({ const collectionRef = firebase.native .firestore() .collection('collection-tests'); - const newDocValue = { ...COL_1, foo: 'updated' }; + const newDocValue = { ...COL_DOC_1, foo: 'updated' }; const callback = sinon.spy(); @@ -386,7 +399,7 @@ function collectionReferenceTests({ ); }); - callback.should.be.calledWith(COL_1); + callback.should.be.calledWith(COL_DOC_1); const docRef = firebase.native.firestore().doc('collection-tests/col1'); await docRef.set(newDocValue); @@ -408,7 +421,7 @@ function collectionReferenceTests({ const collectionRef = firebase.native .firestore() .collection('collection-tests'); - const newDocValue = { ...COL_1, foo: 'updated' }; + const newDocValue = { ...COL_DOC_1, foo: 'updated' }; const callback = sinon.spy(); @@ -425,7 +438,7 @@ function collectionReferenceTests({ unsubscribe = collectionRef.onSnapshot(observer); }); - callback.should.be.calledWith(COL_1); + callback.should.be.calledWith(COL_DOC_1); const docRef = firebase.native.firestore().doc('collection-tests/col1'); await docRef.set(newDocValue); @@ -448,7 +461,7 @@ function collectionReferenceTests({ const collectionRef = firebase.native .firestore() .collection('collection-tests'); - const newDocValue = { ...COL_1, foo: 'updated' }; + const newDocValue = { ...COL_DOC_1, foo: 'updated' }; const callback = sinon.spy(); @@ -472,7 +485,7 @@ function collectionReferenceTests({ ); }); - callback.should.be.calledWith(COL_1); + callback.should.be.calledWith(COL_DOC_1); const docRef = firebase.native.firestore().doc('collection-tests/col1'); await docRef.set(newDocValue); @@ -621,7 +634,7 @@ function collectionReferenceTests({ firebase.native .firestore() .collection('collection-tests') - .where('timestamp', '==', COL_1.timestamp) + .where('timestamp', '==', COL_DOC_1.timestamp) .get() .then(querySnapshot => { should.equal(querySnapshot.size, 1); @@ -631,7 +644,7 @@ function collectionReferenceTests({ firebase.native .firestore() .collection('collection-tests') - .where('geopoint', '==', COL_1.geopoint) + .where('geopoint', '==', COL_DOC_1.geopoint) .get() .then(querySnapshot => { should.equal(querySnapshot.size, 1); @@ -698,11 +711,11 @@ function collectionReferenceTests({ .firestore() .collection('collection-tests2'); await Promise.all([ - collectionTests.doc('col1').set(COL_1), - collectionTests.doc('col2').set({ ...COL_1, daz: 234 }), - collectionTests.doc('col3').set({ ...COL_1, daz: 234 }), - collectionTests.doc('col4').set({ ...COL_1, daz: 234 }), - collectionTests.doc('col5').set({ ...COL_1, daz: 234 }), + collectionTests.doc('col1').set(COL_DOC_1), + collectionTests.doc('col2').set({ ...COL_DOC_1, daz: 234 }), + collectionTests.doc('col3').set({ ...COL_DOC_1, daz: 234 }), + collectionTests.doc('col4').set({ ...COL_DOC_1, daz: 234 }), + collectionTests.doc('col5').set({ ...COL_DOC_1, daz: 234 }), ]); }); @@ -748,30 +761,30 @@ function collectionReferenceTests({ .firestore() .collection('collection-tests2'); await Promise.all([ - collectionTests.doc('col1').set({ ...COL_1, foo: 'bar0' }), + collectionTests.doc('col1').set({ ...COL_DOC_1, foo: 'bar0' }), collectionTests.doc('col2').set({ - ...COL_1, + ...COL_DOC_1, foo: 'bar1', daz: 234, object: { daz: 234 }, timestamp: new Date(2017, 2, 11, 10, 0, 0), }), collectionTests.doc('col3').set({ - ...COL_1, + ...COL_DOC_1, foo: 'bar2', daz: 345, object: { daz: 345 }, timestamp: new Date(2017, 2, 12, 10, 0, 0), }), collectionTests.doc('col4').set({ - ...COL_1, + ...COL_DOC_1, foo: 'bar3', daz: 456, object: { daz: 456 }, timestamp: new Date(2017, 2, 13, 10, 0, 0), }), collectionTests.doc('col5').set({ - ...COL_1, + ...COL_DOC_1, foo: 'bar4', daz: 567, object: { daz: 567 }, @@ -1191,7 +1204,7 @@ function collectionReferenceTests({ const collectionRef = collectionTests .orderBy('object.daz') .endAt(345); - const newDocValue = { ...COL_1, object: { daz: 346 } }; + const newDocValue = { ...COL_DOC_1, object: { daz: 346 } }; const callback = sinon.spy(); @@ -1230,7 +1243,7 @@ function collectionReferenceTests({ const collectionRef = collectionTests .where('baz', '==', true) .orderBy('daz'); - const newDocValue = { ...COL_1, daz: 678 }; + const newDocValue = { ...COL_DOC_1, daz: 678 }; const callback = sinon.spy(); diff --git a/tests/src/tests/firestore/data/index.js b/tests/src/tests/firestore/data/index.js new file mode 100644 index 00000000..e848fab4 --- /dev/null +++ b/tests/src/tests/firestore/data/index.js @@ -0,0 +1,24 @@ +import firebase from '../../../firebase'; + +export const COL_DOC_1 = { + baz: true, + daz: 123, + foo: 'bar', + gaz: 12.1234567, + geopoint: new firebase.native.firestore.GeoPoint(0, 0), + naz: null, + object: { + daz: 123, + }, + timestamp: new Date(2017, 2, 10, 10, 0, 0), +}; +export const DOC_1 = { name: 'doc1' }; +export const DOC_2 = { name: 'doc2', title: 'Document 2' }; + +/* HELPER FUNCTIONS */ +export async function cleanCollection(collection) { + const collectionTestsDocs = await collection.get(); + const tasks = []; + collectionTestsDocs.forEach(doc => tasks.push(doc.ref.delete())); + await Promise.all(tasks); +} diff --git a/tests/src/tests/firestore/documentReferenceTests.js b/tests/src/tests/firestore/documentReferenceTests.js index 417c6166..2587f14e 100644 --- a/tests/src/tests/firestore/documentReferenceTests.js +++ b/tests/src/tests/firestore/documentReferenceTests.js @@ -1,9 +1,28 @@ import sinon from 'sinon'; import 'should-sinon'; import should from 'should'; +import { cleanCollection, DOC_1 } from './data'; -function documentReferenceTests({ describe, it, context, firebase }) { +function documentReferenceTests({ + beforeEach, + describe, + it, + context, + firebase, +}) { describe('DocumentReference', () => { + let documentTestsCollection; + beforeEach(async () => { + documentTestsCollection = firebase.native + .firestore() + .collection('document-tests'); + + // We clean as part of initialisation in case a test errors + // We don't clean after the test as it slows tests significantly + await cleanCollection(documentTestsCollection); + await documentTestsCollection.doc('doc1').set(DOC_1); + }); + context('class', () => { it('should return instance methods', () => new Promise(resolve => { diff --git a/tests/src/tests/firestore/fieldPathTests.js b/tests/src/tests/firestore/fieldPathTests.js index aba24123..f7d321fd 100644 --- a/tests/src/tests/firestore/fieldPathTests.js +++ b/tests/src/tests/firestore/fieldPathTests.js @@ -1,6 +1,7 @@ import should from 'should'; +import { cleanCollection, COL_DOC_1 } from './data'; -function fieldPathTests({ describe, it, context, firebase }) { +function fieldPathTests({ before, describe, it, context, firebase }) { describe('FieldPath', () => { context('documentId', () => { it('should be a FieldPath', () => { @@ -10,6 +11,18 @@ function fieldPathTests({ describe, it, context, firebase }) { }); context('DocumentSnapshot.get()', () => { + let collectionTestsCollection; + before(async () => { + collectionTestsCollection = firebase.native + .firestore() + .collection('collection-tests'); + + // We clean as part of initialisation in case a test errors + // We don't clean after the test as it slows tests significantly + await cleanCollection(collectionTestsCollection); + await collectionTestsCollection.doc('col1').set(COL_DOC_1); + }); + it('should get the correct values', () => firebase.native .firestore() diff --git a/tests/src/tests/firestore/fieldValueTests.js b/tests/src/tests/firestore/fieldValueTests.js index ff145f0d..b9812f53 100644 --- a/tests/src/tests/firestore/fieldValueTests.js +++ b/tests/src/tests/firestore/fieldValueTests.js @@ -1,7 +1,20 @@ import should from 'should'; +import { cleanCollection, DOC_2 } from './data'; -function fieldValueTests({ describe, it, context, firebase }) { +function fieldValueTests({ beforeEach, describe, it, context, firebase }) { describe('FieldValue', () => { + let documentTestsCollection; + beforeEach(async () => { + documentTestsCollection = firebase.native + .firestore() + .collection('document-tests'); + + // We clean as part of initialisation in case a test errors + // We don't clean after the test as it slows tests significantly + await cleanCollection(documentTestsCollection); + await documentTestsCollection.doc('doc2').set(DOC_2); + }); + context('delete()', () => { it('should delete field', () => firebase.native diff --git a/tests/src/tests/firestore/firestoreTests.js b/tests/src/tests/firestore/firestoreTests.js index 1445069a..a6dc4e7c 100644 --- a/tests/src/tests/firestore/firestoreTests.js +++ b/tests/src/tests/firestore/firestoreTests.js @@ -1,6 +1,7 @@ import should from 'should'; +import { cleanCollection } from './data'; -function firestoreTests({ describe, it, context, fcontext, firebase }) { +function firestoreTests({ before, describe, it, context, firebase }) { describe('firestore()', () => { context('collection()', () => { it('should create CollectionReference with the right id', () => @@ -39,23 +40,22 @@ function firestoreTests({ describe, it, context, fcontext, firebase }) { }); context('batch()', () => { + let firestoreTestsCollection; + before(async () => { + firestoreTestsCollection = firebase.native + .firestore() + .collection('firestore-tests'); + + // We clean as part of initialisation in case a test errors + // We don't clean after the test as it slows tests significantly + await cleanCollection(firestoreTestsCollection); + }); + it('should create / update / delete as expected', () => { - const ayRef = firebase.native - .firestore() - .collection('firestore-tests') - .doc('AY'); - const lRef = firebase.native - .firestore() - .collection('firestore-tests') - .doc('LON'); - const nycRef = firebase.native - .firestore() - .collection('firestore-tests') - .doc('NYC'); - const sfRef = firebase.native - .firestore() - .collection('firestore-tests') - .doc('SF'); + const ayRef = firestoreTestsCollection.doc('AY'); + const lRef = firestoreTestsCollection.doc('LON'); + const nycRef = firestoreTestsCollection.doc('NYC'); + const sfRef = firestoreTestsCollection.doc('SF'); return firebase.native .firestore() diff --git a/tests/src/tests/firestore/index.js b/tests/src/tests/firestore/index.js index 7b75bcb5..554eb5e6 100644 --- a/tests/src/tests/firestore/index.js +++ b/tests/src/tests/firestore/index.js @@ -23,9 +23,6 @@ export const COL_1 = { timestamp: new Date(2017, 2, 10, 10, 0, 0), }; -export const DOC_1 = { name: 'doc1' }; -export const DOC_2 = { name: 'doc2', title: 'Document 2' }; - const suite = new TestSuite('Firestore', 'firebase.firestore()', firebase); const testGroups = [ @@ -38,31 +35,11 @@ const testGroups = [ function firestoreTestSuite(testSuite) { testSuite.beforeEach(async () => { - this.collectionTestsCollection = testSuite.firebase.native - .firestore() - .collection('collection-tests'); - this.documentTestsCollection = testSuite.firebase.native - .firestore() - .collection('document-tests'); - this.firestoreTestsCollection = testSuite.firebase.native - .firestore() - .collection('firestore-tests'); - // Make sure the collections are cleaned and initialised correctly - await cleanCollection(this.collectionTestsCollection); - await cleanCollection(this.documentTestsCollection); - await cleanCollection(this.firestoreTestsCollection); - - const tasks = []; - tasks.push(this.collectionTestsCollection.doc('col1').set(COL_1)); - tasks.push(this.documentTestsCollection.doc('doc1').set(DOC_1)); - tasks.push(this.documentTestsCollection.doc('doc2').set(DOC_2)); - - await Promise.all(tasks); + // Do nothing }); testSuite.afterEach(async () => { - // All data will be cleaned an re-initialised before each test - // Adding a clean here slows down the test suite dramatically + // Do nothing }); testGroups.forEach(testGroup => { @@ -76,11 +53,3 @@ function firestoreTestSuite(testSuite) { suite.addTests(firestoreTestSuite); export default suite; - -/* HELPER FUNCTIONS */ -export async function cleanCollection(collection) { - const collectionTestsDocs = await collection.get(); - const tasks = []; - collectionTestsDocs.forEach(doc => tasks.push(doc.ref.delete())); - await Promise.all(tasks); -}