Skip to content

Commit df3cb20

Browse files
committed
onSnapshot Observer and Error testing
1 parent 697b6d4 commit df3cb20

File tree

1 file changed

+190
-48
lines changed

1 file changed

+190
-48
lines changed

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

Lines changed: 190 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ import { DEFAULT_SETTINGS, DEFAULT_PROJECT_ID } from '../util/settings';
8686

8787
use(chaiAsPromised);
8888

89-
const SNAPSHOT_TEST_TIMEOUT = 5000;
90-
9189
apiDescribe('Database', persistence => {
9290
it('can set a document', () => {
9391
return withTestDoc(persistence, docRef => {
@@ -1198,7 +1196,7 @@ apiDescribe('Database', persistence => {
11981196
onSnapshot(docA, () => deferred2.resolve());
11991197
});
12001198
});
1201-
return Promise.all([deferred1.promise, deferred2.promise]).then(() => { });
1199+
return Promise.all([deferred1.promise, deferred2.promise]).then(() => {});
12021200
});
12031201
});
12041202

@@ -1264,36 +1262,187 @@ apiDescribe('Database', persistence => {
12641262
);
12651263
});
12661264

1265+
it('DocumentSnapshot observer events for snapshot created by a bundle', async () => {
1266+
const initialData = { a: 0 };
1267+
const finalData = { a: 1 };
1268+
await withTestDocAndInitialData(
1269+
persistence,
1270+
initialData,
1271+
async (docRef, db) => {
1272+
const doc = await getDoc(docRef);
1273+
const accumulator = new EventsAccumulator<DocumentSnapshot>();
1274+
const unsubscribe = onSnapshot(db, doc.toJSON(), {
1275+
next: accumulator.storeEvent
1276+
});
1277+
await accumulator
1278+
.awaitEvent()
1279+
.then(snap => {
1280+
expect(snap.exists()).to.be.true;
1281+
expect(snap.data()).to.deep.equal(initialData);
1282+
})
1283+
.then(() => setDoc(docRef, finalData))
1284+
.then(() => accumulator.awaitEvent())
1285+
.then(snap => {
1286+
expect(snap.exists()).to.be.true;
1287+
expect(snap.data()).to.deep.equal(finalData);
1288+
});
1289+
unsubscribe();
1290+
}
1291+
);
1292+
});
1293+
1294+
it('DocumentSnapshot error events for snapshot created by a bundle', async () => {
1295+
const initialData = { a: 0 };
1296+
await withTestDocAndInitialData(
1297+
persistence,
1298+
initialData,
1299+
async (docRef, db) => {
1300+
const doc = await getDoc(docRef);
1301+
const json = doc.toJSON();
1302+
json.bundle = 'BadData';
1303+
const deferred = new Deferred();
1304+
const unsubscribe = onSnapshot(
1305+
db,
1306+
json,
1307+
ds => {
1308+
expect(ds).to.not.exist;
1309+
deferred.resolve();
1310+
},
1311+
err => {
1312+
expect(err.name).to.exist;
1313+
expect(err.message).to.exist;
1314+
deferred.resolve();
1315+
}
1316+
);
1317+
await deferred.promise;
1318+
unsubscribe();
1319+
}
1320+
);
1321+
});
1322+
1323+
it('DocumentSnapshot observer error events for snapshot created by a bundle', async () => {
1324+
const initialData = { a: 0 };
1325+
await withTestDocAndInitialData(
1326+
persistence,
1327+
initialData,
1328+
async (docRef, db) => {
1329+
const doc = await getDoc(docRef);
1330+
const json = doc.toJSON();
1331+
json.bundle = 'BadData';
1332+
const deferred = new Deferred();
1333+
const unsubscribe = onSnapshot(db, json, {
1334+
next: ds => {
1335+
expect(ds).to.not.exist;
1336+
deferred.resolve();
1337+
},
1338+
error: err => {
1339+
expect(err.name).to.exist;
1340+
expect(err.message).to.exist;
1341+
deferred.resolve();
1342+
}
1343+
});
1344+
await deferred.promise;
1345+
unsubscribe();
1346+
}
1347+
);
1348+
});
1349+
12671350
it('Querysnapshot events for snapshot created by a bundle', async () => {
12681351
const testDocs = {
12691352
a: { foo: 1 },
12701353
b: { bar: 2 }
12711354
};
12721355
await withTestCollection(persistence, testDocs, async (coll, db) => {
1273-
const q = query(coll, orderBy(documentId()));
1274-
const querySnap = await getDocs(q);
1275-
const json = querySnap.toJSON();
1356+
const querySnap = await getDocs(query(coll, orderBy(documentId())));
12761357
const accumulator = new EventsAccumulator<QuerySnapshot>();
12771358
const unsubscribe = onSnapshot(
12781359
db,
1279-
json,
1360+
querySnap.toJSON(),
12801361
accumulator.storeEvent
12811362
);
1282-
await accumulator
1283-
.awaitEvent()
1284-
.then(snap => {
1285-
const docs = snap.docs;
1286-
expect(docs).not.to.be.null;
1287-
console.error("DEDB lenght: ", docs.length);
1288-
console.error("Doc0 data: ", docs[0].data());
1289-
console.error("Doc1 data: ", docs[1].data());
1290-
expect(docs.length).to.equal(2);
1291-
expect(docs[0].data()).to.deep.equal(testDocs.a);
1292-
expect(docs[1].data()).to.deep.equal(testDocs.b);
1293-
})
1363+
await accumulator.awaitEvent().then(snap => {
1364+
expect(snap.docs).not.to.be.null;
1365+
expect(snap.docs.length).to.equal(2);
1366+
expect(snap.docs[0].data()).to.deep.equal(testDocs.a);
1367+
expect(snap.docs[1].data()).to.deep.equal(testDocs.b);
1368+
});
12941369
unsubscribe();
1295-
}
1296-
);
1370+
});
1371+
});
1372+
1373+
it('Querysnapshot observer events for snapshot created by a bundle', async () => {
1374+
const testDocs = {
1375+
a: { foo: 1 },
1376+
b: { bar: 2 }
1377+
};
1378+
await withTestCollection(persistence, testDocs, async (coll, db) => {
1379+
const querySnap = await getDocs(query(coll, orderBy(documentId())));
1380+
const accumulator = new EventsAccumulator<QuerySnapshot>();
1381+
const unsubscribe = onSnapshot(db, querySnap.toJSON(), {
1382+
next: accumulator.storeEvent
1383+
});
1384+
await accumulator.awaitEvent().then(snap => {
1385+
expect(snap.docs).not.to.be.null;
1386+
expect(snap.docs.length).to.equal(2);
1387+
expect(snap.docs[0].data()).to.deep.equal(testDocs.a);
1388+
expect(snap.docs[1].data()).to.deep.equal(testDocs.b);
1389+
});
1390+
unsubscribe();
1391+
});
1392+
});
1393+
1394+
it('QuerySnapshot error events for snapshot created by a bundle', async () => {
1395+
const testDocs = {
1396+
a: { foo: 1 },
1397+
b: { bar: 2 }
1398+
};
1399+
await withTestCollection(persistence, testDocs, async (coll, db) => {
1400+
const querySnap = await getDocs(query(coll, orderBy(documentId())));
1401+
const deferred = new Deferred();
1402+
const json = querySnap.toJSON();
1403+
json.bundle = 'BadData';
1404+
const unsubscribe = onSnapshot(
1405+
db,
1406+
json,
1407+
qs => {
1408+
expect(qs).to.not.exist;
1409+
deferred.resolve();
1410+
},
1411+
err => {
1412+
expect(err.name).to.exist;
1413+
expect(err.message).to.exist;
1414+
deferred.resolve();
1415+
}
1416+
);
1417+
await deferred.promise;
1418+
unsubscribe();
1419+
});
1420+
});
1421+
1422+
it('QuerySnapshot observer error events for snapshot created by a bundle', async () => {
1423+
const testDocs = {
1424+
a: { foo: 1 },
1425+
b: { bar: 2 }
1426+
};
1427+
await withTestCollection(persistence, testDocs, async (coll, db) => {
1428+
const querySnap = await getDocs(query(coll, orderBy(documentId())));
1429+
const deferred = new Deferred();
1430+
const json = querySnap.toJSON();
1431+
json.bundle = 'BadData';
1432+
const unsubscribe = onSnapshot(db, json, {
1433+
next: qs => {
1434+
expect(qs).to.not.exist;
1435+
deferred.resolve();
1436+
},
1437+
error: err => {
1438+
expect(err.name).to.exist;
1439+
expect(err.message).to.exist;
1440+
deferred.resolve();
1441+
}
1442+
});
1443+
await deferred.promise;
1444+
unsubscribe();
1445+
});
12971446
});
12981447

12991448
it('QuerySnapshot updated doc events in snapshot created by a bundle', async () => {
@@ -1302,39 +1451,32 @@ apiDescribe('Database', persistence => {
13021451
b: { bar: 2 }
13031452
};
13041453
await withTestCollection(persistence, testDocs, async (coll, db) => {
1305-
const q = query(coll, orderBy(documentId()));
1306-
const querySnap = await getDocs(q);
1454+
const querySnap = await getDocs(query(coll, orderBy(documentId())));
13071455
const refForDocA = querySnap.docs[0].ref;
1308-
const json = querySnap.toJSON();
13091456
const accumulator = new EventsAccumulator<QuerySnapshot>();
13101457
const unsubscribe = onSnapshot(
13111458
db,
1312-
json,
1459+
querySnap.toJSON(),
13131460
accumulator.storeEvent
13141461
);
13151462
await accumulator
13161463
.awaitEvent()
13171464
.then(snap => {
1318-
const docs = snap.docs;
1319-
console.error("DEDB: docs: ", docs);
1320-
expect(docs).not.to.be.null;
1321-
expect(docs.length).to.equal(2);
1322-
expect(docs[0].data()).to.deep.equal(testDocs.a);
1323-
expect(docs[1].data()).to.deep.equal(testDocs.b);
1465+
expect(snap.docs).not.to.be.null;
1466+
expect(snap.docs.length).to.equal(2);
1467+
expect(snap.docs[0].data()).to.deep.equal(testDocs.a);
1468+
expect(snap.docs[1].data()).to.deep.equal(testDocs.b);
13241469
})
1325-
.then(() => setDoc(refForDocA, {foo: 0}))
1470+
.then(() => setDoc(refForDocA, { foo: 0 }))
13261471
.then(() => accumulator.awaitEvent())
13271472
.then(snap => {
1328-
const docs = snap.docs;
1329-
console.error("DEDB: docs: ", docs);
1330-
expect(docs).not.to.be.null;
1331-
expect(docs.length).to.equal(2);
1332-
expect(docs[0].data()).to.deep.equal({foo: 0});
1333-
expect(docs[1].data()).to.deep.equal(testDocs.b);
1473+
expect(snap.docs).not.to.be.null;
1474+
expect(snap.docs.length).to.equal(2);
1475+
expect(snap.docs[0].data()).to.deep.equal({ foo: 0 });
1476+
expect(snap.docs[1].data()).to.deep.equal(testDocs.b);
13341477
});
13351478
unsubscribe();
1336-
}
1337-
);
1479+
});
13381480
});
13391481

13401482
it('Metadata only changes are not fired when no options provided', () => {
@@ -1374,7 +1516,7 @@ apiDescribe('Database', persistence => {
13741516
const queryForRejection = collection(db, 'a/__badpath__/b');
13751517
onSnapshot(
13761518
queryForRejection,
1377-
() => { },
1519+
() => {},
13781520
(err: Error) => {
13791521
expect(err.name).to.exist;
13801522
expect(err.message).to.exist;
@@ -1391,13 +1533,13 @@ apiDescribe('Database', persistence => {
13911533
const queryForRejection = collection(db, 'a/__badpath__/b');
13921534
onSnapshot(
13931535
queryForRejection,
1394-
() => { },
1536+
() => {},
13951537
(err: Error) => {
13961538
expect(err.name).to.exist;
13971539
expect(err.message).to.exist;
13981540
onSnapshot(
13991541
queryForRejection,
1400-
() => { },
1542+
() => {},
14011543
(err2: Error) => {
14021544
expect(err2.name).to.exist;
14031545
expect(err2.message).to.exist;
@@ -1812,7 +1954,7 @@ apiDescribe('Database', persistence => {
18121954
it('can query after firestore restart', async () => {
18131955
return withTestDoc(persistence, async (docRef, firestore) => {
18141956
const deferred: Deferred<FirestoreError> = new Deferred();
1815-
const unsubscribe = onSnapshot(docRef, snapshot => { }, deferred.resolve);
1957+
const unsubscribe = onSnapshot(docRef, snapshot => {}, deferred.resolve);
18161958

18171959
await firestore._restart();
18181960

@@ -1832,7 +1974,7 @@ apiDescribe('Database', persistence => {
18321974
it('query listener throws error on termination', async () => {
18331975
return withTestDoc(persistence, async (docRef, firestore) => {
18341976
const deferred: Deferred<FirestoreError> = new Deferred();
1835-
const unsubscribe = onSnapshot(docRef, snapshot => { }, deferred.resolve);
1977+
const unsubscribe = onSnapshot(docRef, snapshot => {}, deferred.resolve);
18361978

18371979
await terminate(firestore);
18381980

@@ -1879,7 +2021,7 @@ apiDescribe('Database', persistence => {
18792021
readonly title: string,
18802022
readonly author: string,
18812023
readonly ref: DocumentReference | null = null
1882-
) { }
2024+
) {}
18832025
byline(): string {
18842026
return this.title + ', by ' + this.author;
18852027
}
@@ -2009,8 +2151,8 @@ apiDescribe('Database', persistence => {
20092151
batch.set(ref, { title: 'olive' }, { merge: true })
20102152
).to.throw(
20112153
'Function WriteBatch.set() called with invalid ' +
2012-
'data (via `toFirestore()`). Unsupported field value: undefined ' +
2013-
'(found in field author in document posts/some-post)'
2154+
'data (via `toFirestore()`). Unsupported field value: undefined ' +
2155+
'(found in field author in document posts/some-post)'
20142156
);
20152157
});
20162158
});

0 commit comments

Comments
 (0)