1- import { renderHook , act } from '@testing-library/react-hooks' ;
2- import { render , waitForElement , cleanup } from '@testing-library/react' ;
1+ import { render , waitForElement , cleanup , act } from '@testing-library/react' ;
32
43import * as React from 'react' ;
54import '@testing-library/jest-dom/extend-expect' ;
@@ -32,7 +31,7 @@ describe('Firestore', () => {
3231
3332 test ( 'sanity check - emulator is running' , ( ) => {
3433 // IF THIS TEST FAILS, MAKE SURE YOU'RE RUNNING THESE TESTS BY DOING:
35- //
34+ // yarn test
3635
3736 return app
3837 . firestore ( )
@@ -105,18 +104,15 @@ describe('Firestore', () => {
105104 } ) ;
106105 } ) ;
107106
108- // THIS TEST CAUSES A REACT `act` WARNING
109- // IT WILL BE FIXED IN REACT 16.9
110- // More info here: https://github.com/testing-library/react-testing-library/issues/281
111107 describe ( 'useFirestoreCollection' , ( ) => {
112108 it ( 'can get a Firestore collection [TEST REQUIRES EMULATOR]' , async ( ) => {
113109 const mockData1 = { a : 'hello' } ;
114110 const mockData2 = { a : 'goodbye' } ;
115111
116112 const ref = app . firestore ( ) . collection ( 'testCollection' ) ;
117113
118- await ref . add ( mockData1 ) ;
119- await ref . add ( mockData2 ) ;
114+ await act ( ( ) => ref . add ( mockData1 ) ) ;
115+ await act ( ( ) => ref . add ( mockData2 ) ) ;
120116
121117 const ReadFirestoreCollection = ( ) => {
122118 const collection = useFirestoreCollection ( ref ) ;
@@ -143,20 +139,54 @@ describe('Firestore', () => {
143139
144140 expect ( getAllByTestId ( 'listItem' ) . length ) . toEqual ( 2 ) ;
145141 } ) ;
142+
143+ it ( 'Returns different data for different queries on the same path [TEST REQUIRES EMULATOR]' , async ( ) => {
144+ const mockData1 = { a : 'hello' } ;
145+ const mockData2 = { a : 'goodbye' } ;
146+
147+ const ref = app . firestore ( ) . collection ( 'testCollection' ) ;
148+ const filteredRef = ref . where ( 'a' , '==' , 'hello' ) ;
149+
150+ await act ( ( ) => ref . add ( mockData1 ) ) ;
151+ await act ( ( ) => ref . add ( mockData2 ) ) ;
152+
153+ const ReadFirestoreCollection = ( ) => {
154+ const list = ( useFirestoreCollection ( ref ) as firestore . QuerySnapshot )
155+ . docs ;
156+ const filteredList = ( useFirestoreCollection (
157+ filteredRef
158+ ) as firestore . QuerySnapshot ) . docs ;
159+
160+ // filteredList's length should be 1 since we only added one value that matches its query
161+ expect ( filteredList . length ) . toEqual ( 1 ) ;
162+
163+ // the full list should be bigger than the filtered list
164+ expect ( list . length ) . toBeGreaterThan ( filteredList . length ) ;
165+
166+ return < h1 data-testid = "rendered" > Hello</ h1 > ;
167+ } ;
168+
169+ const { getByTestId } = render (
170+ < FirebaseAppProvider firebase = { app } >
171+ < React . Suspense fallback = { < h1 data-testid = "fallback" > Fallback</ h1 > } >
172+ < ReadFirestoreCollection />
173+ </ React . Suspense >
174+ </ FirebaseAppProvider >
175+ ) ;
176+
177+ await waitForElement ( ( ) => getByTestId ( 'rendered' ) ) ;
178+ } ) ;
146179 } ) ;
147180
148- // THIS TEST CAUSES A REACT `act` WARNING
149- // IT WILL BE FIXED IN REACT 16.9
150- // More info here: https://github.com/testing-library/react-testing-library/issues/281
151181 describe ( 'useFirestoreCollectionData' , ( ) => {
152182 it ( 'can get a Firestore collection [TEST REQUIRES EMULATOR]' , async ( ) => {
153183 const mockData1 = { a : 'hello' } ;
154184 const mockData2 = { a : 'goodbye' } ;
155185
156186 const ref = app . firestore ( ) . collection ( 'testCollection' ) ;
157187
158- await ref . add ( mockData1 ) ;
159- await ref . add ( mockData2 ) ;
188+ await act ( ( ) => ref . add ( mockData1 ) ) ;
189+ await act ( ( ) => ref . add ( mockData2 ) ) ;
160190
161191 const ReadFirestoreCollection = ( ) => {
162192 const list = useFirestoreCollectionData < any > ( ref , { idField : 'id' } ) ;
@@ -183,5 +213,41 @@ describe('Firestore', () => {
183213
184214 expect ( getAllByTestId ( 'listItem' ) . length ) . toEqual ( 2 ) ;
185215 } ) ;
216+
217+ it ( 'Returns different data for different queries on the same path [TEST REQUIRES EMULATOR]' , async ( ) => {
218+ const mockData1 = { a : 'hello' } ;
219+ const mockData2 = { a : 'goodbye' } ;
220+
221+ const ref = app . firestore ( ) . collection ( 'testCollection' ) ;
222+ const filteredRef = ref . where ( 'a' , '==' , 'hello' ) ;
223+
224+ await act ( ( ) => ref . add ( mockData1 ) ) ;
225+ await act ( ( ) => ref . add ( mockData2 ) ) ;
226+
227+ const ReadFirestoreCollection = ( ) => {
228+ const list = useFirestoreCollectionData < any > ( ref , { idField : 'id' } ) ;
229+ const filteredList = useFirestoreCollectionData < any > ( filteredRef , {
230+ idField : 'id'
231+ } ) ;
232+
233+ // filteredList's length should be 1 since we only added one value that matches its query
234+ expect ( filteredList . length ) . toEqual ( 1 ) ;
235+
236+ // the full list should be bigger than the filtered list
237+ expect ( list . length ) . toBeGreaterThan ( filteredList . length ) ;
238+
239+ return < h1 data-testid = "rendered" > Hello</ h1 > ;
240+ } ;
241+
242+ const { getByTestId } = render (
243+ < FirebaseAppProvider firebase = { app } >
244+ < React . Suspense fallback = { < h1 data-testid = "fallback" > Fallback</ h1 > } >
245+ < ReadFirestoreCollection />
246+ </ React . Suspense >
247+ </ FirebaseAppProvider >
248+ ) ;
249+
250+ await waitForElement ( ( ) => getByTestId ( 'rendered' ) ) ;
251+ } ) ;
186252 } ) ;
187253} ) ;
0 commit comments