@@ -7,19 +7,22 @@ import * as firebase from '@firebase/testing';
77import {
88 useFirestoreDoc ,
99 useFirestoreCollection ,
10- FirebaseAppProvider
10+ FirebaseAppProvider ,
11+ useFirestoreCollectionData ,
12+ useFirestoreDocData ,
1113} from '..' ;
1214import { firestore } from 'firebase/app' ;
1315
1416describe ( 'Firestore' , ( ) => {
15- let app ;
17+ let app : import ( 'firebase' ) . app . App ;
1618
1719 beforeAll ( async ( ) => {
1820 app = firebase . initializeTestApp ( {
1921 projectId : '12345' ,
2022 databaseName : 'my-database' ,
2123 auth : { uid : 'alice' }
22- } ) ;
24+ } ) as import ( 'firebase' ) . app . App ;
25+ // TODO(davideast): Wait for rc and analytics to get included in test app
2326 } ) ;
2427
2528 afterEach ( async ( ) => {
@@ -49,9 +52,7 @@ describe('Firestore', () => {
4952 await ref . set ( mockData ) ;
5053
5154 const ReadFirestoreDoc = ( ) => {
52- const doc = useFirestoreDoc (
53- ( ref as unknown ) as firestore . DocumentReference
54- ) ;
55+ const doc = useFirestoreDoc ( ref ) ;
5556
5657 return (
5758 < h1 data-testid = "readSuccess" >
@@ -72,6 +73,41 @@ describe('Firestore', () => {
7273 expect ( getByTestId ( 'readSuccess' ) ) . toContainHTML ( mockData . a ) ;
7374 } ) ;
7475 } ) ;
76+
77+ describe ( 'useFirestoreDocData' , ( ) => {
78+ it ( 'can get a Firestore document [TEST REQUIRES EMULATOR]' , async ( ) => {
79+ const mockData = { a : 'hello' } ;
80+
81+ const ref = app
82+ . firestore ( )
83+ . collection ( 'testDoc' )
84+ // 'readSuccess' is set to the data-testid={data.id} attribute
85+ . doc ( 'readSuccess' ) ;
86+
87+ await ref . set ( mockData ) ;
88+
89+ const ReadFirestoreDoc = ( ) => {
90+ const data = useFirestoreDocData < any > ( ref , { idField : 'id' } ) ;
91+
92+ return (
93+ < h1 data-testid = { data . id } >
94+ { data . a }
95+ </ h1 >
96+ ) ;
97+ } ;
98+ const { getByTestId } = render (
99+ < FirebaseAppProvider firebase = { app } >
100+ < React . Suspense fallback = { < h1 data-testid = "fallback" > Fallback</ h1 > } >
101+ < ReadFirestoreDoc />
102+ </ React . Suspense >
103+ </ FirebaseAppProvider >
104+ ) ;
105+
106+ await waitForElement ( ( ) => getByTestId ( 'readSuccess' ) ) ;
107+
108+ expect ( getByTestId ( 'readSuccess' ) ) . toContainHTML ( mockData . a ) ;
109+ } ) ;
110+ } ) ;
75111
76112 // THIS TEST CAUSES A REACT `act` WARNING
77113 // IT WILL BE FIXED IN REACT 16.9
@@ -87,9 +123,7 @@ describe('Firestore', () => {
87123 await ref . add ( mockData2 ) ;
88124
89125 const ReadFirestoreCollection = ( ) => {
90- const collection = useFirestoreCollection (
91- ( ref as unknown ) as firestore . CollectionReference
92- ) ;
126+ const collection = useFirestoreCollection ( ref ) ;
93127
94128 return (
95129 < ul data-testid = "readSuccess" >
@@ -114,4 +148,45 @@ describe('Firestore', () => {
114148 expect ( getAllByTestId ( 'listItem' ) . length ) . toEqual ( 2 ) ;
115149 } ) ;
116150 } ) ;
151+
152+ // THIS TEST CAUSES A REACT `act` WARNING
153+ // IT WILL BE FIXED IN REACT 16.9
154+ // More info here: https://github.com/testing-library/react-testing-library/issues/281
155+ describe ( 'useFirestoreCollectionData' , ( ) => {
156+ it ( 'can get a Firestore collection [TEST REQUIRES EMULATOR]' , async ( ) => {
157+ const mockData1 = { a : 'hello' } ;
158+ const mockData2 = { a : 'goodbye' } ;
159+
160+ const ref = app . firestore ( ) . collection ( 'testCollection' ) ;
161+
162+ await ref . add ( mockData1 ) ;
163+ await ref . add ( mockData2 ) ;
164+
165+ const ReadFirestoreCollection = ( ) => {
166+ const list = useFirestoreCollectionData < any > ( ref , { idField : 'id' } ) ;
167+
168+ return (
169+ < ul data-testid = "readSuccess" >
170+ { list . map ( item => (
171+ < li key = { item . id } data-testid = "listItem" >
172+ { item . a }
173+ </ li >
174+ ) ) }
175+ </ ul >
176+ ) ;
177+ } ;
178+ const { getAllByTestId } = render (
179+ < FirebaseAppProvider firebase = { app } >
180+ < React . Suspense fallback = { < h1 data-testid = "fallback" > Fallback</ h1 > } >
181+ < ReadFirestoreCollection />
182+ </ React . Suspense >
183+ </ FirebaseAppProvider >
184+ ) ;
185+
186+ await waitForElement ( ( ) => getAllByTestId ( 'listItem' ) ) ;
187+
188+ expect ( getAllByTestId ( 'listItem' ) . length ) . toEqual ( 2 ) ;
189+ } ) ;
190+ } ) ;
191+
117192} ) ;
0 commit comments