@@ -54,7 +54,16 @@ import {
5454import { writeBatch } from '../src/api/write_batch' ;
5555import { runTransaction } from '../src/api/transaction' ;
5656import { expectEqual , expectNotEqual } from '../../test/util/helpers' ;
57- import { FieldValue } from '../../src/api/field_value' ;
57+ import {
58+ FieldValue ,
59+ deleteField ,
60+ increment ,
61+ serverTimestamp ,
62+ arrayUnion ,
63+ arrayRemove
64+ } from '../src/api/field_value' ;
65+ import { Timestamp } from '../../src/api/timestamp' ;
66+
5867use ( chaiAsPromised ) ;
5968
6069describe ( 'Firestore' , ( ) => {
@@ -512,6 +521,13 @@ function genericMutationTests(
512521 } ) ;
513522 } ) ;
514523
524+ it ( 'enforces that document exists' , ( ) => {
525+ return withTestDoc ( async docRef => {
526+ await expect ( updateDoc ( docRef , { foo : 2 , baz : 2 } ) ) . to . eventually . be
527+ . rejected ;
528+ } ) ;
529+ } ) ;
530+
515531 it ( 'throws when user input fails validation' , ( ) => {
516532 return withTestDoc ( async docRef => {
517533 if ( validationUsesPromises ) {
@@ -600,21 +616,53 @@ describe('deleteDoc()', () => {
600616 } ) ;
601617} ) ;
602618
603- // TODO(firestorelite): Expand test coverage once we can write docs
604619describe ( 'FieldValue' , ( ) => {
605620 it ( 'support equality checking with isEqual()' , ( ) => {
606- expectEqual ( FieldValue . delete ( ) , FieldValue . delete ( ) ) ;
607- expectEqual ( FieldValue . serverTimestamp ( ) , FieldValue . serverTimestamp ( ) ) ;
608- expectNotEqual ( FieldValue . delete ( ) , FieldValue . serverTimestamp ( ) ) ;
609- // TODO(firestorelite): Add test when field value is available
610- //expectNotEqual(FieldValue.delete(), documentId());
621+ expectEqual ( deleteField ( ) , deleteField ( ) ) ;
622+ expectEqual ( serverTimestamp ( ) , serverTimestamp ( ) ) ;
623+ expectNotEqual ( deleteField ( ) , serverTimestamp ( ) ) ;
611624 } ) ;
612625
613626 it ( 'support instanceof checks' , ( ) => {
614- expect ( FieldValue . delete ( ) ) . to . be . an . instanceOf ( FieldValue ) ;
615- expect ( FieldValue . serverTimestamp ( ) ) . to . be . an . instanceOf ( FieldValue ) ;
616- expect ( FieldValue . increment ( 1 ) ) . to . be . an . instanceOf ( FieldValue ) ;
617- expect ( FieldValue . arrayUnion ( 'a' ) ) . to . be . an . instanceOf ( FieldValue ) ;
618- expect ( FieldValue . arrayRemove ( 'a' ) ) . to . be . an . instanceOf ( FieldValue ) ;
627+ expect ( deleteField ( ) ) . to . be . an . instanceOf ( FieldValue ) ;
628+ expect ( serverTimestamp ( ) ) . to . be . an . instanceOf ( FieldValue ) ;
629+ expect ( increment ( 1 ) ) . to . be . an . instanceOf ( FieldValue ) ;
630+ expect ( arrayUnion ( 'a' ) ) . to . be . an . instanceOf ( FieldValue ) ;
631+ expect ( arrayRemove ( 'a' ) ) . to . be . an . instanceOf ( FieldValue ) ;
632+ } ) ;
633+
634+ it ( 'can apply arrayUnion' , ( ) => {
635+ return withTestDocAndInitialData ( { 'val' : [ 'foo' ] } , async docRef => {
636+ await updateDoc ( docRef , 'val' , arrayUnion ( 'bar' ) ) ;
637+ const snap = await getDoc ( docRef ) ;
638+ expect ( snap . data ( ) ) . to . deep . equal ( { 'val' : [ 'foo' , 'bar' ] } ) ;
639+ } ) ;
640+ } ) ;
641+
642+ it ( 'can apply arrayRemove' , ( ) => {
643+ return withTestDocAndInitialData (
644+ { 'val' : [ 'foo' , 'bar' ] } ,
645+ async docRef => {
646+ await updateDoc ( docRef , 'val' , arrayRemove ( 'bar' ) ) ;
647+ const snap = await getDoc ( docRef ) ;
648+ expect ( snap . data ( ) ) . to . deep . equal ( { 'val' : [ 'foo' ] } ) ;
649+ }
650+ ) ;
651+ } ) ;
652+
653+ it ( 'can apply serverTimestamp' , ( ) => {
654+ return withTestDocAndInitialData ( { 'val' : null } , async docRef => {
655+ await updateDoc ( docRef , 'val' , serverTimestamp ( ) ) ;
656+ const snap = await getDoc ( docRef ) ;
657+ expect ( snap . get ( 'val' ) ) . to . be . an . instanceOf ( Timestamp ) ;
658+ } ) ;
659+ } ) ;
660+
661+ it ( 'can delete field' , ( ) => {
662+ return withTestDocAndInitialData ( { 'val' : 'foo' } , async docRef => {
663+ await updateDoc ( docRef , 'val' , deleteField ( ) ) ;
664+ const snap = await getDoc ( docRef ) ;
665+ expect ( snap . data ( ) ) . to . deep . equal ( { } ) ;
666+ } ) ;
619667 } ) ;
620668} ) ;
0 commit comments