@@ -1813,6 +1813,67 @@ apiDescribe('Database', persistence => {
1813
1813
expect ( refEqual ( untypedDocRef , ref ) ) . to . be . true ;
1814
1814
} ) ;
1815
1815
} ) ;
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
+ } ) ;
1816
1877
} ) ;
1817
1878
1818
1879
// TODO(b/196858864): This test regularly times out on CI.
0 commit comments