1- import { cleanup , render } from '@testing-library/react' ;
21import { renderHook , act } from '@testing-library/react-hooks' ;
32import { render , waitForElement , cleanup } from '@testing-library/react' ;
43
@@ -8,19 +7,22 @@ import * as firebase from '@firebase/testing';
87import {
98 useFirestoreDoc ,
109 useFirestoreCollection ,
11- FirebaseAppProvider
10+ FirebaseAppProvider ,
11+ useFirestoreCollectionData ,
12+ useFirestoreDocData ,
1213} from '..' ;
1314import { firestore } from 'firebase/app' ;
1415
1516describe ( 'Firestore' , ( ) => {
16- let app ;
17+ let app : import ( 'firebase' ) . app . App ;
1718
1819 beforeAll ( async ( ) => {
1920 app = firebase . initializeTestApp ( {
2021 projectId : '12345' ,
2122 databaseName : 'my-database' ,
2223 auth : { uid : 'alice' }
23- } ) ;
24+ } ) as import ( 'firebase' ) . app . App ;
25+ // TODO(davideast): Wait for rc and analytics to get included in test app
2426 } ) ;
2527
2628 afterEach ( async ( ) => {
@@ -50,9 +52,7 @@ describe('Firestore', () => {
5052 await ref . set ( mockData ) ;
5153
5254 const ReadFirestoreDoc = ( ) => {
53- const doc = useFirestoreDoc (
54- ( ref as unknown ) as firestore . DocumentReference
55- ) ;
55+ const doc = useFirestoreDoc ( ref ) ;
5656
5757 return (
5858 < h1 data-testid = "readSuccess" >
@@ -73,6 +73,41 @@ describe('Firestore', () => {
7373 expect ( getByTestId ( 'readSuccess' ) ) . toContainHTML ( mockData . a ) ;
7474 } ) ;
7575 } ) ;
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+ } ) ;
76111
77112 // THIS TEST CAUSES A REACT `act` WARNING
78113 // IT WILL BE FIXED IN REACT 16.9
@@ -88,9 +123,7 @@ describe('Firestore', () => {
88123 await ref . add ( mockData2 ) ;
89124
90125 const ReadFirestoreCollection = ( ) => {
91- const collection = useFirestoreCollection (
92- ( ref as unknown ) as firestore . CollectionReference
93- ) ;
126+ const collection = useFirestoreCollection ( ref ) ;
94127
95128 return (
96129 < ul data-testid = "readSuccess" >
@@ -115,5 +148,45 @@ describe('Firestore', () => {
115148 expect ( getAllByTestId ( 'listItem' ) . length ) . toEqual ( 2 ) ;
116149 } ) ;
117150 } ) ;
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+ } ) ;
118191
119192} ) ;
0 commit comments