Skip to content

Commit 9b07bba

Browse files
committed
types.test.ts: add some negative tests for UpdateData
1 parent d7ace80 commit 9b07bba

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

packages/firestore/test/unit/lite-api/types.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,4 +695,79 @@ describe('FirestoreTypeConverter', () => {
695695
expect(data.numberProperty).to.equal(43);
696696
}
697697
});
698+
699+
it('setDoc() fails to compile if `data` argument is missing properties', () => {
700+
async function _(docRef: DocumentReference): Promise<void> {
701+
const converter = new ThrowingConverter<
702+
{ foo: string },
703+
{ bar: number }
704+
>();
705+
const docRefWithConverter = docRef.withConverter(converter);
706+
// @ts-expect-error `data` argument is missing `foo` property.
707+
await setDoc(docRefWithConverter, { bar: 42 });
708+
}
709+
});
710+
711+
it('setDoc() fails to compile if `data` argument has incorrect type for a property', () => {
712+
async function _(docRef: DocumentReference): Promise<void> {
713+
const converter = new ThrowingConverter<{ foo: string }, {}>();
714+
const docRefWithConverter = docRef.withConverter(converter);
715+
// @ts-expect-error The `data` argument has the wrong type for `foo`.
716+
await setDoc(docRefWithConverter, { foo: 42 });
717+
}
718+
});
719+
720+
it('updateDoc() fails to compile if `data` argument is missing properties', () => {
721+
async function _(docRef: DocumentReference): Promise<void> {
722+
const converter = new ThrowingConverter<
723+
{ foo: string },
724+
{ bar: number }
725+
>();
726+
const docRefWithConverter = docRef.withConverter(converter);
727+
// @ts-expect-error `data` argument is missing `bar` property.
728+
await updateDoc(docRefWithConverter, { foo: 'foo' });
729+
}
730+
});
731+
732+
it('updateDoc() fails to compile if `data` argument has incorrect type for a property', () => {
733+
async function _(docRef: DocumentReference): Promise<void> {
734+
const converter = new ThrowingConverter<{}, { bar: number }>();
735+
const docRefWithConverter = docRef.withConverter(converter);
736+
// @ts-expect-error The `data` argument has the wrong type for `bar`.
737+
await updateDoc(docRefWithConverter, { bar: 'bar' });
738+
}
739+
});
740+
741+
it('getDoc() returns AppModelType', () => {
742+
async function _(docRef: DocumentReference): Promise<void> {
743+
const converter = new ThrowingConverter<
744+
{ foo: string },
745+
{ bar: number }
746+
>();
747+
const docRefWithConverter = docRef.withConverter(converter);
748+
const snapshot = await getDoc(docRefWithConverter);
749+
const data: { foo: string } = snapshot.data()!;
750+
expect(data.foo).to.equal('foo');
751+
}
752+
});
753+
754+
/**
755+
* An implementation of FirestoreDataConverter whose methods simply throw an
756+
* exception. Instances of this class may be useful for tests that only desire
757+
* to check the compile-time type checking but not actually invoke the
758+
* converter at runtime.
759+
*/
760+
class ThrowingConverter<AppModelType, DbModelType extends DocumentData>
761+
implements FirestoreDataConverter<AppModelType, DbModelType>
762+
{
763+
toFirestore(
764+
modelObject: WithFieldValue<AppModelType>
765+
): WithFieldValue<DbModelType> {
766+
throw new Error('ThrowingConverter.toFirestore() should not be called');
767+
}
768+
769+
fromFirestore(snapshot: QueryDocumentSnapshot): AppModelType {
770+
throw new Error('ThrowingConverter.fromFirestore() should not be called');
771+
}
772+
}
698773
});

0 commit comments

Comments
 (0)