Skip to content

Commit 05e0c80

Browse files
Add Converter tests to Lite client (firebase#3199)
1 parent 1bc2ccc commit 05e0c80

File tree

6 files changed

+93
-12
lines changed

6 files changed

+93
-12
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ export function collection(
303303
}
304304
}
305305

306+
// TODO(firestorelite): Consider using ErrorFactory -
307+
// https://github.com/firebase/firebase-js-sdk/blob/0131e1f/packages/util/src/errors.ts#L106
306308
export function collectionGroup(
307309
firestore: firestore.FirebaseFirestore,
308310
collectionId: string
@@ -364,10 +366,10 @@ export function doc<T>(
364366
}
365367

366368
export function parent(
367-
reference: CollectionReference<unknown>
369+
reference: firestore.CollectionReference<unknown>
368370
): DocumentReference<firestore.DocumentData> | null;
369371
export function parent<T>(
370-
reference: DocumentReference<T>
372+
reference: firestore.DocumentReference<T>
371373
): CollectionReference<T>;
372374
export function parent<T>(
373375
child: firestore.CollectionReference<unknown> | firestore.DocumentReference<T>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ export class QueryDocumentSnapshot<T = firestore.DocumentData>
113113
export class QuerySnapshot<T = firestore.DocumentData>
114114
implements firestore.QuerySnapshot<T> {
115115
constructor(
116-
public readonly query: firestore.Query<T>,
117-
public readonly _docs: Array<QueryDocumentSnapshot<T>>
116+
readonly query: firestore.Query<T>,
117+
readonly _docs: Array<QueryDocumentSnapshot<T>>
118118
) {}
119119

120120
get docs(): Array<firestore.QueryDocumentSnapshot<T>> {

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,6 @@ describe('DocumentSnapshot', () => {
615615
});
616616
});
617617

618-
// TODO(firestorelite): Add converter tests
619618
describe('deleteDoc()', () => {
620619
it('can delete a non-existing document', () => {
621620
return withTestDoc(docRef => deleteDoc(docRef));
@@ -967,3 +966,83 @@ describe('equality', () => {
967966
);
968967
});
969968
});
969+
970+
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+
it('for DocumentReference.withConverter()', () => {
989+
return withTestDoc(async docRef => {
990+
docRef = docRef.withConverter(postConverter);
991+
await setDoc(docRef, new Post('post', 'author'));
992+
const postData = await getDoc(docRef);
993+
const post = postData.data();
994+
expect(post).to.not.equal(undefined);
995+
expect(post!.byline()).to.equal('post, by author');
996+
});
997+
});
998+
999+
it('for CollectionReference.withConverter()', () => {
1000+
return withTestCollection(async coll => {
1001+
coll = coll.withConverter(postConverter);
1002+
const docRef = await addDoc(coll, new Post('post', 'author'));
1003+
const postData = await getDoc(docRef);
1004+
const post = postData.data();
1005+
expect(post).to.not.equal(undefined);
1006+
expect(post!.byline()).to.equal('post, by author');
1007+
});
1008+
});
1009+
1010+
it('for Query.withConverter()', () => {
1011+
return withTestCollection(async coll => {
1012+
coll = coll.withConverter(postConverter);
1013+
await setDoc(doc(coll, 'post1'), new Post('post1', 'author1'));
1014+
const posts = await getQuery(coll);
1015+
expect(posts.size).to.equal(1);
1016+
expect(posts.docs[0].data()!.byline()).to.equal('post1, by author1');
1017+
});
1018+
});
1019+
1020+
it('drops the converter when calling parent() with a CollectionReference', () => {
1021+
return withTestDb(async db => {
1022+
const coll = collection(db, 'root/doc/parent').withConverter(
1023+
postConverter
1024+
);
1025+
const untypedDoc = parent(coll)!;
1026+
expect(refEqual(untypedDoc, doc(db, 'root/doc'))).to.be.true;
1027+
});
1028+
});
1029+
1030+
it('checks converter when comparing with isEqual()', () => {
1031+
return withTestDb(async db => {
1032+
const postConverter2 = { ...postConverter };
1033+
1034+
const postsCollection = collection(db, 'users/user1/posts').withConverter(
1035+
postConverter
1036+
);
1037+
const postsCollection2 = collection(
1038+
db,
1039+
'users/user1/posts'
1040+
).withConverter(postConverter2);
1041+
expect(refEqual(postsCollection, postsCollection2)).to.be.false;
1042+
1043+
const docRef = doc(db, 'some/doc').withConverter(postConverter);
1044+
const docRef2 = doc(db, 'some/doc').withConverter(postConverter2);
1045+
expect(refEqual(docRef, docRef2)).to.be.false;
1046+
});
1047+
});
1048+
});

packages/firestore/src/api/user_data_reader.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ export interface UntypedFirestoreDataConverter<T> {
6363
*/
6464
export class DocumentKeyReference<T> {
6565
constructor(
66-
public readonly _databaseId: DatabaseId,
67-
public readonly _key: DocumentKey,
68-
public readonly _converter: UntypedFirestoreDataConverter<T> | null
66+
readonly _databaseId: DatabaseId,
67+
readonly _key: DocumentKey,
68+
readonly _converter: UntypedFirestoreDataConverter<T> | null
6969
) {}
7070
}
7171

packages/firestore/src/model/object_value.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const enum TypeOrder {
4949
* ability to add and remove fields (via the ObjectValueBuilder).
5050
*/
5151
export class ObjectValue {
52-
constructor(public readonly proto: { mapValue: api.MapValue }) {
52+
constructor(readonly proto: { mapValue: api.MapValue }) {
5353
debugAssert(
5454
!isServerTimestamp(proto),
5555
'ServerTimestamps should be converted to ServerTimestampValue'

packages/firestore/src/remote/datastore.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class DatastoreImpl extends Datastore {
5252
terminated = false;
5353

5454
constructor(
55-
public readonly connection: Connection,
56-
public readonly credentials: CredentialsProvider,
57-
public readonly serializer: JsonProtoSerializer
55+
readonly connection: Connection,
56+
readonly credentials: CredentialsProvider,
57+
readonly serializer: JsonProtoSerializer
5858
) {
5959
super();
6060
}

0 commit comments

Comments
 (0)