Skip to content

Commit 3ee6db2

Browse files
committed
add tests
1 parent e72d1df commit 3ee6db2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,67 @@ apiDescribe('Database', persistence => {
18131813
expect(refEqual(untypedDocRef, ref)).to.be.true;
18141814
});
18151815
});
1816+
1817+
it('DocumentReference.withConverter() default DbModelType', () => {
1818+
const converter = {
1819+
toFirestore: (value: number) => {
1820+
return { value };
1821+
},
1822+
fromFirestore: (snapshot: QueryDocumentSnapshot) => {
1823+
return snapshot.data()['value'] as number;
1824+
}
1825+
};
1826+
return withTestDoc(persistence, async docRef => {
1827+
// The line below should compile since the DbModelType type parameter of
1828+
// DocumentReference.withConverter() has a default value.
1829+
const typedDocRef = docRef.withConverter<number>(converter);
1830+
await setDoc(typedDocRef, 42);
1831+
const snapshot = await getDoc(typedDocRef);
1832+
expect(snapshot.data()).to.equal(42);
1833+
});
1834+
});
1835+
1836+
it('CollectionReference.withConverter() default DbModelType', () => {
1837+
const converter = {
1838+
toFirestore: (value: number) => {
1839+
return { value };
1840+
},
1841+
fromFirestore: (snapshot: QueryDocumentSnapshot) => {
1842+
return snapshot.data()['value'] as number;
1843+
}
1844+
};
1845+
const testDocs = { doc1: { value: 42 } };
1846+
return withTestCollection(persistence, testDocs, async collectionRef => {
1847+
// The line below should compile since the DbModelType type parameter of
1848+
// CollectionReference.withConverter() has a default value.
1849+
const typedCollectionRef =
1850+
collectionRef.withConverter<number>(converter);
1851+
const snapshot = await getDocs(typedCollectionRef);
1852+
expect(snapshot.size).to.equal(1);
1853+
expect(snapshot.docs[0].data()).to.equal(42);
1854+
});
1855+
});
1856+
1857+
it('Query.withConverter() default DbModelType', () => {
1858+
const converter = {
1859+
toFirestore: (value: number) => {
1860+
return { value };
1861+
},
1862+
fromFirestore: (snapshot: QueryDocumentSnapshot) => {
1863+
return snapshot.data()['value'] as number;
1864+
}
1865+
};
1866+
const testDocs = { doc1: { value: 42 } };
1867+
return withTestCollection(persistence, testDocs, async collectionRef => {
1868+
// The line below should compile since the DbModelType type parameter of
1869+
// Query.withConverter() has a default value.
1870+
const query_ = query(collectionRef, where('value', '==', 42));
1871+
const typedQuery = query_.withConverter<number>(converter);
1872+
const snapshot = await getDocs(typedQuery);
1873+
expect(snapshot.size).to.equal(1);
1874+
expect(snapshot.docs[0].data()).to.equal(42);
1875+
});
1876+
});
18161877
});
18171878

18181879
// TODO(b/196858864): This test regularly times out on CI.

0 commit comments

Comments
 (0)