Skip to content

Commit e33c4f9

Browse files
committed
QuerySnapshot integration tests
1 parent 099e53e commit e33c4f9

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

packages/firestore/test/integration/api/database.test.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,7 @@ apiDescribe('Database', persistence => {
12251225
done();
12261226
});
12271227
});
1228+
12281229
it('DocumentSnapshot updated doc events in snapshot created by a bundle', function (done) {
12291230
this.timeout(SNAPSHOT_TEST_TIMEOUT);
12301231
void withTestDoc(persistence, async (docRef, db) => {
@@ -1289,6 +1290,136 @@ apiDescribe('Database', persistence => {
12891290
});
12901291
});
12911292

1293+
it('Querysnapshot events for snapshot created by a bundle', function (done) {
1294+
this.timeout(SNAPSHOT_TEST_TIMEOUT);
1295+
const testDocs = {
1296+
a: { foo: 1 },
1297+
b: { bar: 2 }
1298+
};
1299+
void withTestCollection(persistence, testDocs, async (coll, db) => {
1300+
const q = query(coll, orderBy(documentId()));
1301+
const querySnap = await getDocs(q);
1302+
const json = querySnap.toJSON();
1303+
const gotInitialSnapshot = new Deferred<void>();
1304+
const unlisten = onSnapshot(db, json, (querySnap: QuerySnapshot) => {
1305+
const docs = querySnap.docs;
1306+
expect(docs).to.not.be.null;
1307+
if (docs) {
1308+
expect(querySnap.docs.length).equals(2);
1309+
expect(docs[0].data()).to.deep.equal({ foo: 1 });
1310+
expect(docs[1].data()).to.deep.equal({ bar: 2 });
1311+
}
1312+
gotInitialSnapshot.resolve();
1313+
});
1314+
await gotInitialSnapshot.promise;
1315+
unlisten();
1316+
done();
1317+
});
1318+
});
1319+
1320+
it('QuerySnapshot updated doc events in snapshot created by a bundle', function (done) {
1321+
this.timeout(SNAPSHOT_TEST_TIMEOUT);
1322+
const testDocs = {
1323+
a: { foo: 1 },
1324+
b: { bar: 2 }
1325+
};
1326+
void withTestCollection(persistence, testDocs, async (coll, db) => {
1327+
const q = query(coll, orderBy(documentId()));
1328+
const querySnap = await getDocs(q);
1329+
const json = querySnap.toJSON();
1330+
const updateFound = new Deferred<void>();
1331+
let count = 0;
1332+
const unlisten = onSnapshot(db, json, (querySnap: QuerySnapshot) => {
1333+
count++;
1334+
const docs = querySnap.docs;
1335+
expect(docs).to.not.be.null;
1336+
if (docs) {
1337+
expect(docs[0].data()).to.deep.equal({ foo: 0 });
1338+
updateFound.resolve();
1339+
}
1340+
});
1341+
await setDoc(querySnap.docs[0].ref, { foo: 0 });
1342+
await updateFound.promise;
1343+
expect(count).to.equal(1);
1344+
unlisten();
1345+
done();
1346+
});
1347+
});
1348+
1349+
it('QuerySnapshot multiple events for snapshot created by a bundle', function (done) {
1350+
this.timeout(SNAPSHOT_TEST_TIMEOUT);
1351+
const testDocs = {
1352+
a: { foo: false },
1353+
b: { bar: false }
1354+
};
1355+
void withTestCollection(persistence, testDocs, async (coll, db) => {
1356+
const q = query(coll, orderBy(documentId()));
1357+
const querySnap = await getDocs(q);
1358+
const json = querySnap.toJSON();
1359+
const updateFound = new Deferred<void>();
1360+
let count = 0;
1361+
const unlisten = onSnapshot(db, json, (querySnap: QuerySnapshot) => {
1362+
const docs = querySnap.docs;
1363+
expect(docs).to.not.be.null;
1364+
if (docs) {
1365+
if (count === 0) {
1366+
expect(docs[0].data()).to.deep.equal({ foo: true });
1367+
expect(docs[1].data()).to.deep.equal({ bar: false });
1368+
} else {
1369+
expect(docs[0].data()).to.deep.equal({ foo: true });
1370+
expect(docs[1].data()).to.deep.equal({ bar: true });
1371+
updateFound.resolve();
1372+
}
1373+
count++;
1374+
}
1375+
});
1376+
await setDoc(querySnap.docs[0].ref, { foo: true });
1377+
await setDoc(querySnap.docs[1].ref, { bar: true });
1378+
await updateFound.promise;
1379+
expect(count).to.equal(2);
1380+
unlisten();
1381+
done();
1382+
});
1383+
});
1384+
1385+
//////////////////
1386+
it('onSnapshotsInSync fires after listeners are in sync', () => {
1387+
const testDocs = {
1388+
a: { foo: 1 }
1389+
};
1390+
return withTestCollection(persistence, testDocs, async (coll, db) => {
1391+
let events: string[] = [];
1392+
const gotInitialSnapshot = new Deferred<void>();
1393+
const docA = doc(coll, 'a');
1394+
1395+
onSnapshot(docA, snap => {
1396+
events.push('doc');
1397+
gotInitialSnapshot.resolve();
1398+
});
1399+
await gotInitialSnapshot.promise;
1400+
events = [];
1401+
1402+
const done = new Deferred<void>();
1403+
onSnapshotsInSync(db, () => {
1404+
events.push('snapshots-in-sync');
1405+
if (events.length === 3) {
1406+
// We should have an initial snapshots-in-sync event, then a snapshot
1407+
// event for set(), then another event to indicate we're in sync
1408+
// again.
1409+
expect(events).to.deep.equal([
1410+
'snapshots-in-sync',
1411+
'doc',
1412+
'snapshots-in-sync'
1413+
]);
1414+
done.resolve();
1415+
}
1416+
});
1417+
1418+
await setDoc(docA, { foo: 3 });
1419+
await done.promise;
1420+
});
1421+
});
1422+
12921423
it('Metadata only changes are not fired when no options provided', () => {
12931424
return withTestDoc(persistence, docRef => {
12941425
const secondUpdateFound = new Deferred();

0 commit comments

Comments
 (0)