Skip to content

Commit 8820711

Browse files
committed
tests
1 parent 037ffc3 commit 8820711

File tree

5 files changed

+89
-24
lines changed

5 files changed

+89
-24
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ sample-complex/.cache/**
1313
sample-complex/src/firebase-key.json
1414
reactfire/firestore-debug.log
1515
reactfire/pub/**
16-
pub
16+
pub
17+
reactfire/firestore-debug.log

reactfire/firestore-debug.log

Lines changed: 0 additions & 9 deletions
This file was deleted.

reactfire/firestore/firestore.test.tsx

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { cleanup, render } from '@testing-library/react';
21
import { renderHook, act } from '@testing-library/react-hooks';
32
import { render, waitForElement, cleanup } from '@testing-library/react';
43

@@ -8,19 +7,22 @@ import * as firebase from '@firebase/testing';
87
import {
98
useFirestoreDoc,
109
useFirestoreCollection,
11-
FirebaseAppProvider
10+
FirebaseAppProvider,
11+
useFirestoreCollectionData,
12+
useFirestoreDocData,
1213
} from '..';
1314
import { firestore } from 'firebase/app';
1415

1516
describe('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
});

reactfire/firestore/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function useFirestoreDoc<T = unknown>(
2828
export function useFirestoreDocData<T = unknown>(
2929
ref: firestore.DocumentReference,
3030
options?: ReactFireOptions<T>
31-
): firestore.DocumentSnapshot | T {
31+
): T {
3232
return useObservable(
3333
docData(ref, checkIdField(options)),
3434
ref.path,
@@ -62,7 +62,7 @@ export function useFirestoreCollection<T = { [key: string]: unknown }>(
6262
export function useFirestoreCollectionData<T = { [key: string]: unknown }>(
6363
ref: firestore.CollectionReference,
6464
options?: ReactFireOptions<T[]>
65-
): firestore.QuerySnapshot | T[] {
65+
): T[] {
6666
return useObservable(
6767
collectionData(ref, checkIdField(options)),
6868
ref.path,

reactfire/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface ReactFireOptions<T = unknown> {
2-
startWithValue: T;
3-
idField: string;
2+
startWithValue?: T;
3+
idField?: string;
44
}
55

66
export * from './auth';

0 commit comments

Comments
 (0)