@@ -28,6 +28,9 @@ import {
28
28
terminate
29
29
} from '../src/api/database' ;
30
30
import {
31
+ Post ,
32
+ postConverter ,
33
+ postConverterMerge ,
31
34
withTestCollection ,
32
35
withTestCollectionAndInitialData ,
33
36
withTestDb ,
@@ -480,6 +483,30 @@ function genericMutationTests(
480
483
} ) ;
481
484
} ) ;
482
485
486
+ it ( 'supports partials with merge' , async ( ) => {
487
+ return withTestDb ( async db => {
488
+ const coll = collection ( db , 'posts' ) ;
489
+ const ref = doc ( coll , 'post' ) . withConverter ( postConverterMerge ) ;
490
+ await setDoc ( ref , new Post ( 'walnut' , 'author' ) ) ;
491
+ await setDoc ( ref , { title : 'olive' } , { merge : true } ) ;
492
+ const postDoc = await getDoc ( ref ) ;
493
+ expect ( postDoc . get ( 'title' ) ) . to . equal ( 'olive' ) ;
494
+ expect ( postDoc . get ( 'author' ) ) . to . equal ( 'author' ) ;
495
+ } ) ;
496
+ } ) ;
497
+
498
+ it ( 'supports partials with mergeFields' , async ( ) => {
499
+ return withTestDb ( async db => {
500
+ const coll = collection ( db , 'posts' ) ;
501
+ const ref = doc ( coll , 'post' ) . withConverter ( postConverterMerge ) ;
502
+ await setDoc ( ref , new Post ( 'walnut' , 'author' ) ) ;
503
+ await setDoc ( ref , { title : 'olive' } , { mergeFields : [ 'title' ] } ) ;
504
+ const postDoc = await getDoc ( ref ) ;
505
+ expect ( postDoc . get ( 'title' ) ) . to . equal ( 'olive' ) ;
506
+ expect ( postDoc . get ( 'author' ) ) . to . equal ( 'author' ) ;
507
+ } ) ;
508
+ } ) ;
509
+
483
510
it ( 'throws when user input fails validation' , ( ) => {
484
511
return withTestDoc ( async docRef => {
485
512
if ( validationUsesPromises ) {
@@ -881,7 +908,8 @@ describe('equality', () => {
881
908
expect ( refEqual ( coll1a , coll2 ) ) . to . be . false ;
882
909
883
910
const coll1c = collection ( firestore , 'a' ) . withConverter ( {
884
- toFirestore : data => data as firestore . DocumentData ,
911
+ toFirestore : ( data : firestore . DocumentData ) =>
912
+ data as firestore . DocumentData ,
885
913
fromFirestore : snap => snap . data ( )
886
914
} ) ;
887
915
expect ( refEqual ( coll1a , coll1c ) ) . to . be . false ;
@@ -900,7 +928,8 @@ describe('equality', () => {
900
928
expect ( refEqual ( doc1a , doc2 ) ) . to . be . false ;
901
929
902
930
const doc1c = collection ( firestore , 'a' ) . withConverter ( {
903
- toFirestore : data => data as firestore . DocumentData ,
931
+ toFirestore : ( data : firestore . DocumentData ) =>
932
+ data as firestore . DocumentData ,
904
933
fromFirestore : snap => snap . data ( )
905
934
} ) ;
906
935
expect ( refEqual ( doc1a , doc1c ) ) . to . be . false ;
@@ -968,23 +997,6 @@ describe('equality', () => {
968
997
} ) ;
969
998
970
999
describe ( 'withConverter() support' , ( ) => {
971
- class Post {
972
- constructor ( readonly title : string , readonly author : string ) { }
973
- byline ( ) : string {
974
- return this . title + ', by ' + this . author ;
975
- }
976
- }
977
-
978
- const postConverter = {
979
- toFirestore ( post : Post ) : firestore . DocumentData {
980
- return { title : post . title , author : post . author } ;
981
- } ,
982
- fromFirestore ( snapshot : firestore . QueryDocumentSnapshot ) : Post {
983
- const data = snapshot . data ( ) ;
984
- return new Post ( data . title , data . author ) ;
985
- }
986
- } ;
987
-
988
1000
it ( 'for DocumentReference.withConverter()' , ( ) => {
989
1001
return withTestDoc ( async docRef => {
990
1002
docRef = docRef . withConverter ( postConverter ) ;
@@ -1045,4 +1057,19 @@ describe('withConverter() support', () => {
1045
1057
expect ( refEqual ( docRef , docRef2 ) ) . to . be . false ;
1046
1058
} ) ;
1047
1059
} ) ;
1060
+
1061
+ it ( 'requires the correct converter for Partial usage' , async ( ) => {
1062
+ return withTestDb ( async db => {
1063
+ const coll = collection ( db , 'posts' ) ;
1064
+ const ref = doc ( coll , 'post' ) . withConverter ( postConverter ) ;
1065
+ const batch = writeBatch ( db ) ;
1066
+ expect ( ( ) =>
1067
+ batch . set ( ref , { title : 'olive' } , { merge : true } )
1068
+ ) . to . throw (
1069
+ 'Function WriteBatch.set() called with invalid data ' +
1070
+ '(via `toFirestore()`). Unsupported field value: undefined ' +
1071
+ '(found in field author in document posts/post)'
1072
+ ) ;
1073
+ } ) ;
1074
+ } ) ;
1048
1075
} ) ;
0 commit comments