Skip to content

Commit 937c59d

Browse files
Add (or fix) FieldValue tests for lite client (#3174)
1 parent df36a24 commit 937c59d

File tree

3 files changed

+63
-24
lines changed

3 files changed

+63
-24
lines changed

packages/firestore/lite/src/api/reference.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,9 @@ export function updateDoc(
360360
return configureClient.then(datastore =>
361361
invokeCommitRpc(
362362
datastore,
363-
parsed.toMutations(ref._key, Precondition.none())
363+
parsed.toMutations(ref._key, Precondition.exists(true))
364364
)
365365
);
366-
367-
return ref.firestore
368-
._ensureClientConfigured()
369-
.then(datastore =>
370-
invokeCommitRpc(
371-
datastore,
372-
parsed.toMutations(ref._key, Precondition.exists(true))
373-
)
374-
);
375366
}
376367

377368
export function deleteDoc(

packages/firestore/lite/src/api/snapshot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
7272
} else {
7373
const userDataWriter = new UserDataWriter(
7474
this._firestore._databaseId,
75-
/* timestampsInSnapshots= */ false,
75+
/* timestampsInSnapshots= */ true,
7676
/* serverTimestampBehavior=*/ 'none',
7777
key => new DocumentReference(this._firestore, key)
7878
);
@@ -88,7 +88,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
8888
if (value !== null) {
8989
const userDataWriter = new UserDataWriter(
9090
this._firestore._databaseId,
91-
/* timestampsInSnapshots= */ false,
91+
/* timestampsInSnapshots= */ true,
9292
/* serverTimestampBehavior=*/ 'none',
9393
key => new DocumentReference(this._firestore, key, this._converter)
9494
);

packages/firestore/lite/test/integration.test.ts

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ import {
5454
import { writeBatch } from '../src/api/write_batch';
5555
import { runTransaction } from '../src/api/transaction';
5656
import { 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+
5867
use(chaiAsPromised);
5968

6069
describe('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
604619
describe('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

Comments
 (0)