@@ -39,6 +39,8 @@ const newTestFirestore = firebaseExport.newTestFirestore;
39
39
const Timestamp = firebaseExport . Timestamp ;
40
40
const FieldPath = firebaseExport . FieldPath ;
41
41
const FieldValue = firebaseExport . FieldValue ;
42
+ const DocumentReference = firebaseExport . DocumentReference ;
43
+ const QueryDocumentSnapshot = firebaseExport . QueryDocumentSnapshot ;
42
44
43
45
const MEMORY_ONLY_BUILD =
44
46
typeof process !== 'undefined' &&
@@ -1326,7 +1328,11 @@ apiDescribe('Database', (persistence: boolean) => {
1326
1328
// only to web.
1327
1329
apiDescribe ( 'withConverter() support' , ( persistence : boolean ) => {
1328
1330
class Post {
1329
- constructor ( readonly title : string , readonly author : string ) { }
1331
+ constructor (
1332
+ readonly title : string ,
1333
+ readonly author : string ,
1334
+ readonly ref : firestore . DocumentReference | null = null
1335
+ ) { }
1330
1336
byline ( ) : string {
1331
1337
return this . title + ', by ' + this . author ;
1332
1338
}
@@ -1340,8 +1346,9 @@ apiDescribe('Database', (persistence: boolean) => {
1340
1346
snapshot : firestore . QueryDocumentSnapshot ,
1341
1347
options : firestore . SnapshotOptions
1342
1348
) : Post {
1349
+ expect ( snapshot ) . to . be . an . instanceof ( QueryDocumentSnapshot ) ;
1343
1350
const data = snapshot . data ( options ) ;
1344
- return new Post ( data . title , data . author ) ;
1351
+ return new Post ( data . title , data . author , snapshot . ref ) ;
1345
1352
}
1346
1353
} ;
1347
1354
@@ -1369,7 +1376,7 @@ apiDescribe('Database', (persistence: boolean) => {
1369
1376
options : firestore . SnapshotOptions
1370
1377
) : Post {
1371
1378
const data = snapshot . data ( ) ;
1372
- return new Post ( data . title , data . author ) ;
1379
+ return new Post ( data . title , data . author , snapshot . ref ) ;
1373
1380
}
1374
1381
} ;
1375
1382
@@ -1552,7 +1559,7 @@ apiDescribe('Database', (persistence: boolean) => {
1552
1559
expect ( options ) . to . deep . equal ( { serverTimestamps : 'estimate' } ) ;
1553
1560
1554
1561
const data = snapshot . data ( options ) ;
1555
- return new Post ( data . title , data . author ) ;
1562
+ return new Post ( data . title , data . author , snapshot . ref ) ;
1556
1563
}
1557
1564
} ) ;
1558
1565
@@ -1590,6 +1597,53 @@ apiDescribe('Database', (persistence: boolean) => {
1590
1597
expect ( docRef . isEqual ( docRef2 ) ) . to . be . false ;
1591
1598
} ) ;
1592
1599
} ) ;
1600
+
1601
+ it ( 'Correct snapshot specified to fromFirestore() when registered with DocumentReference' , ( ) => {
1602
+ return withTestDb ( persistence , async db => {
1603
+ const untypedDocRef = db . collection ( '/models' ) . doc ( ) ;
1604
+ const docRef = untypedDocRef . withConverter ( postConverter ) ;
1605
+ await docRef . set ( new Post ( 'post' , 'author' ) ) ;
1606
+ const docSnapshot = await docRef . get ( ) ;
1607
+ const ref = docSnapshot . data ( ) ! . ref ! ;
1608
+ expect ( ref ) . to . be . an . instanceof ( DocumentReference ) ;
1609
+ expect ( untypedDocRef . isEqual ( ref ) ) . to . be . true ;
1610
+ } ) ;
1611
+ } ) ;
1612
+
1613
+ it ( 'Correct snapshot specified to fromFirestore() when registered with CollectionReference' , ( ) => {
1614
+ return withTestDb ( persistence , async db => {
1615
+ const untypedCollection = db
1616
+ . collection ( '/models' )
1617
+ . doc ( )
1618
+ . collection ( 'sub' ) ;
1619
+ const collection = untypedCollection . withConverter ( postConverter ) ;
1620
+ const docRef = collection . doc ( ) ;
1621
+ await docRef . set ( new Post ( 'post' , 'author' , docRef ) ) ;
1622
+ const querySnapshot = await collection . get ( ) ;
1623
+ expect ( querySnapshot . size ) . to . equal ( 1 ) ;
1624
+ const ref = querySnapshot . docs [ 0 ] . data ( ) . ref ! ;
1625
+ expect ( ref ) . to . be . an . instanceof ( DocumentReference ) ;
1626
+ const untypedDocRef = untypedCollection . doc ( docRef . id ) ;
1627
+ expect ( untypedDocRef . isEqual ( ref ) ) . to . be . true ;
1628
+ } ) ;
1629
+ } ) ;
1630
+
1631
+ it ( 'Correct snapshot specified to fromFirestore() when registered with Query' , ( ) => {
1632
+ return withTestDb ( persistence , async db => {
1633
+ const untypedCollection = db . collection ( '/models' ) ;
1634
+ const untypedDocRef = untypedCollection . doc ( ) ;
1635
+ const docRef = untypedDocRef . withConverter ( postConverter ) ;
1636
+ await docRef . set ( new Post ( 'post' , 'author' , docRef ) ) ;
1637
+ const query = untypedCollection
1638
+ . where ( FieldPath . documentId ( ) , '==' , docRef . id )
1639
+ . withConverter ( postConverter ) ;
1640
+ const querySnapshot = await query . get ( ) ;
1641
+ expect ( querySnapshot . size ) . to . equal ( 1 ) ;
1642
+ const ref = querySnapshot . docs [ 0 ] . data ( ) . ref ! ;
1643
+ expect ( ref ) . to . be . an . instanceof ( DocumentReference ) ;
1644
+ expect ( untypedDocRef . isEqual ( ref ) ) . to . be . true ;
1645
+ } ) ;
1646
+ } ) ;
1593
1647
} ) ;
1594
1648
1595
1649
it ( 'can set and get data with auto detect long polling enabled' , ( ) => {
0 commit comments