forked from FirebaseExtended/rxfire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore-lite.test.ts
228 lines (194 loc) · 7.38 KB
/
firestore-lite.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @jest-environment node
*/
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable @typescript-eslint/no-floating-promises */
// app is used as namespaces to access types
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import {
collection,
docData,
collectionData,
} from '../dist/firestore/lite';
import { map } from 'rxjs/operators';
import { default as TEST_PROJECT, firestoreEmulatorPort } from './config';
import { doc as firestoreDoc, getDocs, collection as firestoreCollection, getDoc, Firestore as FirebaseFirestore, CollectionReference, getFirestore, DocumentReference, connectFirestoreEmulator, doc, setDoc, collection as baseCollection, QueryDocumentSnapshot } from 'firebase/firestore/lite';
import { initializeApp, deleteApp, FirebaseApp } from 'firebase/app';
const createId = (): string => Math.random().toString(36).substring(5);
/**
* Create a collection with a random name. This helps sandbox offline tests and
* makes sure tests don't interfere with each other as they run.
*/
const createRandomCol = (
firestore: FirebaseFirestore,
): CollectionReference => baseCollection(firestore, createId());
/**
* Create an environment for the tests to run in. The information is returned
* from the function for use within the test.
*/
const seedTest = async (firestore: FirebaseFirestore) => {
const colRef = createRandomCol(firestore);
const davidDoc = doc(colRef, 'david');
await setDoc(davidDoc, {name: 'David'});
const shannonDoc = doc(colRef, 'shannon');
await setDoc(shannonDoc, {name: 'Shannon'});
const expectedNames = ['David', 'Shannon'];
const expectedEvents = [
{name: 'David', type: 'added'},
{name: 'Shannon', type: 'added'},
];
return {colRef, davidDoc, shannonDoc, expectedNames, expectedEvents};
};
describe('RxFire firestore/lite', () => {
let app: FirebaseApp;
let firestore: FirebaseFirestore;
/**
* Each test runs inside it's own app instance and the app
* is deleted after the test runs.
*
* Each test is responsible for seeding and removing data. Helper
* functions are useful if the process becomes brittle or tedious.
* Note that removing is less necessary since the tests are run
* against the emulator
*/
beforeEach(() => {
app = initializeApp(TEST_PROJECT, createId());
firestore = getFirestore(app);
connectFirestoreEmulator(firestore, 'localhost', firestoreEmulatorPort);
});
afterEach(() => {
deleteApp(app).catch(() => undefined);
});
describe('collection', () => {
/**
* This is a simple test to see if the collection() method
* correctly emits snapshots.
*
* The test seeds two "people" into the collection. RxFire
* creats an observable with the `collection()` method and
* asserts that the two "people" are in the array.
*/
it('should emit snapshots', async (done: jest.DoneCallback) => {
const {colRef, expectedNames} = await seedTest(firestore);
collection(colRef)
.pipe(map((docs) => docs.map((doc) => doc.data().name)))
.subscribe((names) => {
expect(names).toEqual(expectedNames);
done();
});
});
});
describe('collection w/converter', () => {
/**
* This is a simple test to see if the collection() method
* correctly emits snapshots.
*
* The test seeds two "people" into the collection. RxFire
* creats an observable with the `collection()` method and
* asserts that the two "people" are in the array.
*/
it('should emit snapshots', async (done: jest.DoneCallback) => {
const {colRef, expectedNames} = await seedTest(firestore);
class Folk {
constructor(public name: string) { }
static fromFirestore(snap: QueryDocumentSnapshot) {
const name = snap.data().name;
if (name !== 'Shannon') {
return new Folk(`${snap.data().name}!`);
} else {
return undefined;
}
}
static toFirestore() {
return {};
}
static collection = colRef.withConverter(Folk);
}
collection(Folk.collection)
.subscribe(docs => {
const names = docs.map(doc => doc.data()?.name);
const classes = docs.map(doc => doc.data()?.constructor?.name);
expect(names).toEqual(['David!', undefined]);
expect(classes).toEqual(['Folk', undefined]);
done();
});
});
});
describe('Data Mapping Functions', () => {
/**
* The `unwrap(id)` method will map a collection to its data payload and map the doc ID to a the specificed key.
*/
it('collectionData should map a QueryDocumentSnapshot[] to an array of plain objects', async (done: jest.DoneCallback) => {
const {colRef} = await seedTest(firestore);
// const unwrapped = collection(colRef).pipe(unwrap('userId'));
const unwrapped = collectionData(colRef, { idField: 'userId' });
unwrapped.subscribe((val) => {
const expectedDoc = {
name: 'David',
userId: 'david',
};
expect(val).toBeInstanceOf(Array);
expect(val[0]).toEqual(expectedDoc);
done();
});
});
it('docData should map a QueryDocumentSnapshot to a plain object', async (done: jest.DoneCallback) => {
const {davidDoc} = await seedTest(firestore);
// const unwrapped = doc(davidDoc).pipe(unwrap('UID'));
const unwrapped = docData(davidDoc, { idField: 'UID'});
unwrapped.subscribe((val) => {
const expectedDoc = {
name: 'David',
UID: 'david',
};
expect(val).toEqual(expectedDoc);
done();
});
});
/**
* TODO(jamesdaniels)
* Having trouble gettings these test green with the emulators
* FIRESTORE (8.5.0) INTERNAL ASSERTION FAILED: Unexpected state
*/
it('docData matches the result of docSnapShot.data() when the document doesn\'t exist', async (done) => {
pending('Not working against the emulator');
const {colRef} = await seedTest(firestore);
const nonExistentDoc: DocumentReference = firestoreDoc(colRef,
createId(),
);
const unwrapped = docData(nonExistentDoc);
getDoc(nonExistentDoc).then((snap) => {
unwrapped.subscribe((val) => {
expect(val).toEqual(snap.data());
done();
});
});
});
it('collectionData matches the result of querySnapShot.docs when the collection doesn\'t exist', (done) => {
pending('Not working against the emulator');
const nonExistentCollection = firestoreCollection(firestore, createId());
const unwrapped = collectionData(nonExistentCollection);
getDocs(nonExistentCollection).then((snap) => {
unwrapped.subscribe((val) => {
expect(val).toEqual(snap.docs);
done();
});
});
});
});
});