Skip to content

Commit 78626ef

Browse files
committed
fromJSON integration tests
1 parent 8a942f1 commit 78626ef

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

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

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,66 @@ apiDescribe('Database', persistence => {
13411341
});
13421342
});
13431343

1344+
it('DocumentSnapshot updated doc events in snapshot created by fromJSON bundle', async () => {
1345+
const initialData = { a: 0 };
1346+
const finalData = { a: 1 };
1347+
await withTestDocAndInitialData(
1348+
persistence,
1349+
initialData,
1350+
async (docRef, db) => {
1351+
const doc = await getDoc(docRef);
1352+
const fromJsonDoc = DocumentSnapshot.fromJSON(db, doc.toJSON());
1353+
const accumulator = new EventsAccumulator<DocumentSnapshot>();
1354+
const unsubscribe = onSnapshot(
1355+
db,
1356+
fromJsonDoc.toJSON(),
1357+
accumulator.storeEvent
1358+
);
1359+
await accumulator
1360+
.awaitEvent()
1361+
.then(snap => {
1362+
expect(snap.exists()).to.be.true;
1363+
expect(snap.data()).to.deep.equal(initialData);
1364+
})
1365+
.then(() => setDoc(docRef, finalData))
1366+
.then(() => accumulator.awaitEvent())
1367+
.then(snap => {
1368+
expect(snap.exists()).to.be.true;
1369+
expect(snap.data()).to.deep.equal(finalData);
1370+
});
1371+
unsubscribe();
1372+
}
1373+
);
1374+
});
1375+
1376+
it('DocumentSnapshot updated doc events in snapshot created by fromJSON doc ref', async () => {
1377+
const initialData = { a: 0 };
1378+
const finalData = { a: 1 };
1379+
await withTestDocAndInitialData(
1380+
persistence,
1381+
initialData,
1382+
async (docRef, db) => {
1383+
const doc = await getDoc(docRef);
1384+
const fromJsonDoc = DocumentSnapshot.fromJSON(db, doc.toJSON());
1385+
const accumulator = new EventsAccumulator<DocumentSnapshot>();
1386+
const unsubscribe = onSnapshot(fromJsonDoc.ref, accumulator.storeEvent);
1387+
await accumulator
1388+
.awaitEvent()
1389+
.then(snap => {
1390+
expect(snap.exists()).to.be.true;
1391+
expect(snap.data()).to.deep.equal(initialData);
1392+
})
1393+
.then(() => setDoc(docRef, finalData))
1394+
.then(() => accumulator.awaitEvent())
1395+
.then(snap => {
1396+
expect(snap.exists()).to.be.true;
1397+
expect(snap.data()).to.deep.equal(finalData);
1398+
});
1399+
unsubscribe();
1400+
}
1401+
);
1402+
});
1403+
13441404
it('Querysnapshot events for snapshot created by a bundle', async () => {
13451405
const testDocs = {
13461406
a: { foo: 1 },
@@ -1469,6 +1529,75 @@ apiDescribe('Database', persistence => {
14691529
});
14701530
});
14711531

1532+
it('QuerySnapshot updated doc events in snapshot created by fromJSON bundle', async () => {
1533+
const testDocs = {
1534+
a: { foo: 1 },
1535+
b: { bar: 2 }
1536+
};
1537+
await withTestCollection(persistence, testDocs, async (coll, db) => {
1538+
const querySnap = await getDocs(query(coll, orderBy(documentId())));
1539+
const querySnapFromJson = QuerySnapshot.fromJSON(db, querySnap.toJSON());
1540+
const refForDocA = querySnapFromJson.docs[0].ref;
1541+
const accumulator = new EventsAccumulator<QuerySnapshot>();
1542+
const unsubscribe = onSnapshot(
1543+
db,
1544+
querySnapFromJson.toJSON(),
1545+
accumulator.storeEvent
1546+
);
1547+
await accumulator
1548+
.awaitEvent()
1549+
.then(snap => {
1550+
expect(snap.docs).not.to.be.null;
1551+
expect(snap.docs.length).to.equal(2);
1552+
expect(snap.docs[0].data()).to.deep.equal(testDocs.a);
1553+
expect(snap.docs[1].data()).to.deep.equal(testDocs.b);
1554+
})
1555+
.then(() => setDoc(refForDocA, { foo: 0 }))
1556+
.then(() => accumulator.awaitEvent())
1557+
.then(snap => {
1558+
expect(snap.docs).not.to.be.null;
1559+
expect(snap.docs.length).to.equal(2);
1560+
expect(snap.docs[0].data()).to.deep.equal({ foo: 0 });
1561+
expect(snap.docs[1].data()).to.deep.equal(testDocs.b);
1562+
});
1563+
unsubscribe();
1564+
});
1565+
});
1566+
1567+
it('QuerySnapshot updated doc events in snapshot created by fromJSON query ref', async () => {
1568+
const testDocs = {
1569+
a: { foo: 1 },
1570+
b: { bar: 2 }
1571+
};
1572+
await withTestCollection(persistence, testDocs, async (coll, db) => {
1573+
const querySnap = await getDocs(query(coll, orderBy(documentId())));
1574+
const querySnapFromJson = QuerySnapshot.fromJSON(db, querySnap.toJSON());
1575+
const refForDocA = querySnapFromJson.docs[0].ref;
1576+
const accumulator = new EventsAccumulator<QuerySnapshot>();
1577+
const unsubscribe = onSnapshot(
1578+
querySnapFromJson.query,
1579+
accumulator.storeEvent
1580+
);
1581+
await accumulator
1582+
.awaitEvent()
1583+
.then(snap => {
1584+
expect(snap.docs).not.to.be.null;
1585+
expect(snap.docs.length).to.equal(2);
1586+
expect(snap.docs[0].data()).to.deep.equal(testDocs.a);
1587+
expect(snap.docs[1].data()).to.deep.equal(testDocs.b);
1588+
})
1589+
.then(() => setDoc(refForDocA, { foo: 0 }))
1590+
.then(() => accumulator.awaitEvent())
1591+
.then(snap => {
1592+
expect(snap.docs).not.to.be.null;
1593+
expect(snap.docs.length).to.equal(2);
1594+
expect(snap.docs[0].data()).to.deep.equal({ foo: 0 });
1595+
expect(snap.docs[1].data()).to.deep.equal(testDocs.b);
1596+
});
1597+
unsubscribe();
1598+
});
1599+
});
1600+
14721601
it('Metadata only changes are not fired when no options provided', () => {
14731602
return withTestDoc(persistence, docRef => {
14741603
const secondUpdateFound = new Deferred();

0 commit comments

Comments
 (0)