From fdd8df6966b55216617680e5eb1664fb78dfa174 Mon Sep 17 00:00:00 2001 From: Chris Bianca Date: Thu, 1 Mar 2018 17:28:04 +0000 Subject: [PATCH] Add missing firestore tests --- tests/ios/Podfile.lock | 4 +- .../firestore/collectionReferenceTests.js | 152 ++++++++++++++++-- .../tests/firestore/documentReferenceTests.js | 135 ++++++++++++++++ tests/src/tests/firestore/fieldPathTests.js | 7 + tests/src/tests/firestore/firestoreTests.js | 62 +++++++ 5 files changed, 344 insertions(+), 16 deletions(-) diff --git a/tests/ios/Podfile.lock b/tests/ios/Podfile.lock index 22babbd1..fe1e3160 100644 --- a/tests/ios/Podfile.lock +++ b/tests/ios/Podfile.lock @@ -164,7 +164,7 @@ PODS: - React/Core - React/fishhook - React/RCTBlob - - RNFirebase (3.2.4): + - RNFirebase (3.2.7): - React - yoga (0.52.0.React) @@ -228,7 +228,7 @@ SPEC CHECKSUMS: nanopb: 5601e6bca2dbf1ed831b519092ec110f66982ca3 Protobuf: 8a9838fba8dae3389230e1b7f8c104aa32389c03 React: 61a6bdf17a9ff16875c230e6ff278d9de274e16c - RNFirebase: 011e47909cf54070f72d50b8d61eb7b347774d29 + RNFirebase: 3a141a97041ea0757e2036c2bb18acbe9f0e105d yoga: 646606bf554d54a16711f35596178522fbc00480 PODFILE CHECKSUM: 67c98bcb203cb992da590bcab6f690f727653ca5 diff --git a/tests/src/tests/firestore/collectionReferenceTests.js b/tests/src/tests/firestore/collectionReferenceTests.js index 6b46fd07..780b595e 100644 --- a/tests/src/tests/firestore/collectionReferenceTests.js +++ b/tests/src/tests/firestore/collectionReferenceTests.js @@ -26,6 +26,15 @@ function collectionReferenceTests({ })); }); + context('parent', () => { + it('should return parent document', () => { + const collection = firebase.native + .firestore() + .collection('collection/document/subcollection'); + collection.parent.path.should.equal('collection/document'); + }); + }); + context('add()', () => { it('should create Document', () => firebase.native @@ -51,6 +60,15 @@ function collectionReferenceTests({ should.equal(docRef.path, 'collection-tests/doc'); resolve(); })); + + it('should error when supplied an incorrect path', () => { + (() => { + firebase.native + .firestore() + .collection('collection') + .doc('invalid/doc'); + }).should.throw('Argument "documentPath" must point to a document.'); + }); }); context('get()', () => { @@ -68,6 +86,47 @@ function collectionReferenceTests({ }); context('onSnapshot()', () => { + it('QuerySnapshot has correct properties', async () => { + const snapshot = await firebase.native + .firestore() + .collection('collection-tests') + .get(); + + snapshot.docChanges.should.be.an.Array(); + snapshot.empty.should.equal(false); + snapshot.metadata.should.be.an.Object(); + snapshot.query.should.be.an.Object(); + }); + + it('DocumentChange has correct properties', async () => { + const collectionRef = firebase.native + .firestore() + .collection('collection-tests'); + + // Test + + let unsubscribe; + let changes; + await new Promise(resolve2 => { + unsubscribe = collectionRef.onSnapshot(snapshot => { + changes = snapshot.docChanges; + resolve2(); + }); + }); + + // Assertions + + changes.should.be.a.Array(); + changes[0].doc.should.be.an.Object(); + changes[0].newIndex.should.be.a.Number(); + changes[0].oldIndex.should.be.a.Number(); + changes[0].type.should.be.a.String(); + + // Tear down + + unsubscribe(); + }); + it('calls callback with the initial data and then when document changes', async () => { const collectionRef = firebase.native .firestore() @@ -104,9 +163,7 @@ function collectionReferenceTests({ unsubscribe(); }); - }); - context('onSnapshot()', () => { it('calls callback with the initial data and then when document is added', async () => { const collectionRef = firebase.native .firestore() @@ -144,9 +201,7 @@ function collectionReferenceTests({ unsubscribe(); }); - }); - context('onSnapshot()', () => { it("doesn't call callback when the ref is updated with the same value", async () => { const collectionRef = firebase.native .firestore() @@ -181,9 +236,7 @@ function collectionReferenceTests({ unsubscribe(); }); - }); - context('onSnapshot()', () => { it('allows binding multiple callbacks to the same ref', async () => { // Setup const collectionRef = firebase.native @@ -234,9 +287,7 @@ function collectionReferenceTests({ unsubscribeA(); unsubscribeB(); }); - }); - context('onSnapshot()', () => { it('listener stops listening when unsubscribed', async () => { // Setup const collectionRef = firebase.native @@ -310,9 +361,7 @@ function collectionReferenceTests({ callbackA.should.be.calledTwice(); callbackB.should.be.calledThrice(); }); - }); - context('onSnapshot()', () => { it('supports options and callback', async () => { const collectionRef = firebase.native .firestore() @@ -354,9 +403,7 @@ function collectionReferenceTests({ unsubscribe(); }); - }); - context('onSnapshot()', () => { it('supports observer', async () => { const collectionRef = firebase.native .firestore() @@ -396,9 +443,7 @@ function collectionReferenceTests({ unsubscribe(); }); - }); - context('onSnapshot()', () => { it('supports options and observer', async () => { const collectionRef = firebase.native .firestore() @@ -416,6 +461,7 @@ function collectionReferenceTests({ snapshot.forEach(doc => callback(doc.data())); resolve2(); }, + error: () => {}, }; unsubscribe = collectionRef.onSnapshot( { @@ -443,6 +489,84 @@ function collectionReferenceTests({ unsubscribe(); }); + + it('errors when invalid parameters supplied', async () => { + const colRef = firebase.native + .firestore() + .collection('collection-tests'); + + (() => { + colRef.onSnapshot(() => {}, 'error'); + }).should.throw( + 'Query.onSnapshot failed: Second argument must be a valid function.' + ); + (() => { + colRef.onSnapshot({ + next: () => {}, + error: 'error', + }); + }).should.throw( + 'Query.onSnapshot failed: Observer.error must be a valid function.' + ); + (() => { + colRef.onSnapshot( + { + includeQueryMetadataChanges: true, + }, + () => {}, + 'error' + ); + }).should.throw( + 'Query.onSnapshot failed: Third argument must be a valid function.' + ); + (() => { + colRef.onSnapshot( + { + includeQueryMetadataChanges: true, + }, + { + next: () => {}, + error: 'error', + } + ); + }).should.throw( + 'Query.onSnapshot failed: Observer.error must be a valid function.' + ); + (() => { + colRef.onSnapshot( + { + includeQueryMetadataChanges: true, + }, + { + next: 'error', + } + ); + }).should.throw( + 'Query.onSnapshot failed: Observer.next must be a valid function.' + ); + (() => { + colRef.onSnapshot( + { + includeQueryMetadataChanges: true, + }, + 'error' + ); + }).should.throw( + 'Query.onSnapshot failed: Second argument must be a function or observer.' + ); + (() => { + colRef.onSnapshot({ + error: 'error', + }); + }).should.throw( + 'Query.onSnapshot failed: First argument must be a function, observer or options.' + ); + (() => { + colRef.onSnapshot(); + }).should.throw( + 'Query.onSnapshot failed: Called with invalid arguments.' + ); + }); }); // Where diff --git a/tests/src/tests/firestore/documentReferenceTests.js b/tests/src/tests/firestore/documentReferenceTests.js index 4686c888..bd14e3d8 100644 --- a/tests/src/tests/firestore/documentReferenceTests.js +++ b/tests/src/tests/firestore/documentReferenceTests.js @@ -17,6 +17,28 @@ function documentReferenceTests({ describe, it, context, firebase }) { })); }); + context('id', () => { + it('should return document id', () => { + const document = firebase.native.firestore().doc('documents/doc1'); + document.id.should.equal('doc1'); + }); + }); + + context('parent', () => { + it('should return parent collection', () => { + const document = firebase.native.firestore().doc('documents/doc1'); + document.parent.id.should.equal('documents'); + }); + }); + + context('collection()', () => { + it('should return a child collection', () => { + const document = firebase.native.firestore().doc('documents/doc1'); + const collection = document.collection('pages'); + collection.id.should.equal('pages'); + }); + }); + context('delete()', () => { it('should delete Document', () => firebase.native @@ -32,6 +54,17 @@ function documentReferenceTests({ describe, it, context, firebase }) { })); }); + context('get()', () => { + it('DocumentSnapshot should have correct properties', async () => { + const snapshot = await firebase.native + .firestore() + .doc('document-tests/doc1') + .get(); + snapshot.id.should.equal('doc1'); + snapshot.metadata.should.be.an.Object(); + }); + }); + context('onSnapshot()', () => { it('calls callback with the initial data and then when value changes', async () => { const docRef = firebase.native.firestore().doc('document-tests/doc1'); @@ -321,6 +354,7 @@ function documentReferenceTests({ describe, it, context, firebase }) { callback(snapshot.data()); resolve2(); }, + error: () => {}, }; unsubscribe = docRef.onSnapshot( { includeMetadataChanges: true }, @@ -346,6 +380,88 @@ function documentReferenceTests({ describe, it, context, firebase }) { unsubscribe(); }); + + it('errors when invalid parameters supplied', async () => { + const docRef = firebase.native.firestore().doc('document-tests/doc1'); + (() => { + docRef.onSnapshot(() => {}, 'error'); + }).should.throw( + 'DocumentReference.onSnapshot failed: Second argument must be a valid function.' + ); + (() => { + docRef.onSnapshot({ + next: () => {}, + error: 'error', + }); + }).should.throw( + 'DocumentReference.onSnapshot failed: Observer.error must be a valid function.' + ); + (() => { + docRef.onSnapshot({ + next: 'error', + }); + }).should.throw( + 'DocumentReference.onSnapshot failed: Observer.next must be a valid function.' + ); + (() => { + docRef.onSnapshot( + { + includeMetadataChanges: true, + }, + () => {}, + 'error' + ); + }).should.throw( + 'DocumentReference.onSnapshot failed: Third argument must be a valid function.' + ); + (() => { + docRef.onSnapshot( + { + includeMetadataChanges: true, + }, + { + next: () => {}, + error: 'error', + } + ); + }).should.throw( + 'DocumentReference.onSnapshot failed: Observer.error must be a valid function.' + ); + (() => { + docRef.onSnapshot( + { + includeMetadataChanges: true, + }, + { + next: 'error', + } + ); + }).should.throw( + 'DocumentReference.onSnapshot failed: Observer.next must be a valid function.' + ); + (() => { + docRef.onSnapshot( + { + includeMetadataChanges: true, + }, + 'error' + ); + }).should.throw( + 'DocumentReference.onSnapshot failed: Second argument must be a function or observer.' + ); + (() => { + docRef.onSnapshot({ + error: 'error', + }); + }).should.throw( + 'DocumentReference.onSnapshot failed: First argument must be a function, observer or options.' + ); + (() => { + docRef.onSnapshot(); + }).should.throw( + 'DocumentReference.onSnapshot failed: Called with invalid arguments.' + ); + }); }); context('set()', () => { @@ -464,6 +580,25 @@ function documentReferenceTests({ describe, it, context, firebase }) { doc.data().nested.firstname.should.equal('First Name'); doc.data().nested.lastname.should.equal('Last Name'); })); + + it('errors when invalid parameters supplied', async () => { + const docRef = firebase.native.firestore().doc('document-tests/doc1'); + (() => { + docRef.update('error'); + }).should.throw( + 'DocumentReference.update failed: If using a single argument, it must be an object.' + ); + (() => { + docRef.update('error1', 'error2', 'error3'); + }).should.throw( + 'DocumentReference.update failed: Must have either a single object argument, or equal numbers of key/value pairs.' + ); + (() => { + docRef.update(0, 'error'); + }).should.throw( + 'DocumentReference.update failed: Argument at index 0 must be a string or FieldPath' + ); + }); }); context('types', () => { diff --git a/tests/src/tests/firestore/fieldPathTests.js b/tests/src/tests/firestore/fieldPathTests.js index 99d49eb6..aba24123 100644 --- a/tests/src/tests/firestore/fieldPathTests.js +++ b/tests/src/tests/firestore/fieldPathTests.js @@ -2,6 +2,13 @@ import should from 'should'; function fieldPathTests({ describe, it, context, firebase }) { describe('FieldPath', () => { + context('documentId', () => { + it('should be a FieldPath', () => { + const documentId = firebase.native.firestore.FieldPath.documentId(); + documentId.should.be.instanceof(firebase.native.firestore.FieldPath); + }); + }); + context('DocumentSnapshot.get()', () => { it('should get the correct values', () => firebase.native diff --git a/tests/src/tests/firestore/firestoreTests.js b/tests/src/tests/firestore/firestoreTests.js index b88cd477..bde6a43d 100644 --- a/tests/src/tests/firestore/firestoreTests.js +++ b/tests/src/tests/firestore/firestoreTests.js @@ -11,6 +11,14 @@ function firestoreTests({ describe, it, context, firebase }) { should.equal(collectionRef.id, 'collection2'); resolve(); })); + + it('should error if invalid collection path supplied', () => { + (() => { + firebase.native.firestore().collection('collection1/doc1'); + }).should.throw( + 'Argument "collectionPath" must point to a collection.' + ); + }); }); context('doc()', () => { @@ -22,6 +30,12 @@ function firestoreTests({ describe, it, context, firebase }) { should.equal(docRef.path, 'collection1/doc1/collection2/doc2'); resolve(); })); + + it('should error if invalid document path supplied', () => { + (() => { + firebase.native.firestore().doc('collection1'); + }).should.throw('Argument "documentPath" must point to a document.'); + }); }); context('batch()', () => { @@ -90,6 +104,54 @@ function firestoreTests({ describe, it, context, firebase }) { sfDoc.data().nested.lastname.should.equal('Last Name'); }); }); + + it('errors when invalid parameters supplied', async () => { + const ref = firebase.native.firestore().doc('collection/doc'); + const batch = firebase.native.firestore().batch(); + (() => { + batch.update(ref, 'error'); + }).should.throw( + 'WriteBatch.update failed: If using two arguments, the second must be an object.' + ); + (() => { + batch.update(ref, 'error1', 'error2', 'error3'); + }).should.throw( + 'WriteBatch.update failed: Must have a document reference, followed by either a single object argument, or equal numbers of key/value pairs.' + ); + (() => { + batch.update(ref, 0, 'error'); + }).should.throw( + 'WriteBatch.update failed: Argument at index 0 must be a string or FieldPath' + ); + }); + }); + + context('enablePersistence()', () => { + it('should throw an unsupported error', () => { + (() => { + firebase.native.firestore().enablePersistence(); + }).should.throw( + 'Persistence is enabled by default on the Firestore SDKs' + ); + }); + }); + + context('setLogLevel()', () => { + it('should throw an unsupported error', () => { + (() => { + firebase.native.firestore().setLogLevel(); + }).should.throw( + 'firebase.firestore().setLogLevel() is unsupported by the native Firebase SDKs.' + ); + }); + }); + + context('settings()', () => { + it('should throw an unsupported error', () => { + (() => { + firebase.native.firestore().settings(); + }).should.throw('firebase.firestore().settings() coming soon'); + }); }); }); }