diff --git a/.gitignore b/.gitignore index 0677600b..c9f9e5b5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ sample-complex/.cache/** sample-complex/src/firebase-key.json reactfire/firestore-debug.log reactfire/pub/** -pub \ No newline at end of file +pub +yarn-error.log diff --git a/reactfire/firestore/firestore.test.tsx b/reactfire/firestore/firestore.test.tsx index 28bfc665..f90bcc28 100644 --- a/reactfire/firestore/firestore.test.tsx +++ b/reactfire/firestore/firestore.test.tsx @@ -7,19 +7,22 @@ import * as firebase from '@firebase/testing'; import { useFirestoreDoc, useFirestoreCollection, - FirebaseAppProvider + FirebaseAppProvider, + useFirestoreCollectionData, + useFirestoreDocData, } from '..'; import { firestore } from 'firebase/app'; describe('Firestore', () => { - let app; + let app: import('firebase').app.App; beforeAll(async () => { app = firebase.initializeTestApp({ projectId: '12345', databaseName: 'my-database', auth: { uid: 'alice' } - }); + }) as import('firebase').app.App; + // TODO(davideast): Wait for rc and analytics to get included in test app }); afterEach(async () => { @@ -49,9 +52,7 @@ describe('Firestore', () => { await ref.set(mockData); const ReadFirestoreDoc = () => { - const doc = useFirestoreDoc( - (ref as unknown) as firestore.DocumentReference - ); + const doc = useFirestoreDoc(ref); return (

@@ -72,6 +73,41 @@ describe('Firestore', () => { expect(getByTestId('readSuccess')).toContainHTML(mockData.a); }); }); + + describe('useFirestoreDocData', () => { + it('can get a Firestore document [TEST REQUIRES EMULATOR]', async () => { + const mockData = { a: 'hello' }; + + const ref = app + .firestore() + .collection('testDoc') + // 'readSuccess' is set to the data-testid={data.id} attribute + .doc('readSuccess'); + + await ref.set(mockData); + + const ReadFirestoreDoc = () => { + const data = useFirestoreDocData(ref, { idField: 'id' }); + + return ( +

+ {data.a} +

+ ); + }; + const { getByTestId } = render( + + Fallback

}> + + + + ); + + await waitForElement(() => getByTestId('readSuccess')); + + expect(getByTestId('readSuccess')).toContainHTML(mockData.a); + }); + }); // THIS TEST CAUSES A REACT `act` WARNING // IT WILL BE FIXED IN REACT 16.9 @@ -87,9 +123,7 @@ describe('Firestore', () => { await ref.add(mockData2); const ReadFirestoreCollection = () => { - const collection = useFirestoreCollection( - (ref as unknown) as firestore.CollectionReference - ); + const collection = useFirestoreCollection(ref); return (