Skip to content

Commit fe78f89

Browse files
committed
fix(firestore): do not return idField on valueChanges if document does not exists
1 parent a26676c commit fe78f89

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

src/firestore/document/document.spec.ts

+23-27
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ describe('AngularFirestoreDocument', () => {
3434

3535
describe('valueChanges()', () => {
3636

37-
it('should get unwrapped snapshot', async (done: any) => {
37+
it('should get unwrapped snapshot', async (done: DoneFn) => {
3838
const randomCollectionName = afs.firestore.collection('a').doc().id;
39-
const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as firebase.firestore.DocumentReference<Stock>;
40-
const stock = new AngularFirestoreDocument(ref, afs);
39+
const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as DocumentReference<Stock>;
40+
const stock = new AngularFirestoreDocument<Stock>(ref, afs);
4141
await stock.set(FAKE_STOCK_DATA);
4242
const obs$ = stock.valueChanges();
4343
obs$.pipe(take(1)).subscribe(async data => {
@@ -46,10 +46,9 @@ describe('AngularFirestoreDocument', () => {
4646
});
4747
});
4848

49-
/* TODO(jamesdaniels): test is flaking, look into this
50-
it('should optionally map the doc ID to the emitted data object', async (done: any) => {
49+
it('should optionally map the doc ID to the emitted data object if doc exists', async (done: DoneFn) => {
5150
const randomCollectionName = afs.firestore.collection('a').doc().id;
52-
const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`);
51+
const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as DocumentReference<Stock>;
5352
const stock = new AngularFirestoreDocument<Stock>(ref, afs);
5453
await stock.set(FAKE_STOCK_DATA);
5554
const idField = 'myCustomID';
@@ -59,36 +58,33 @@ describe('AngularFirestoreDocument', () => {
5958
expect(data).toEqual(jasmine.objectContaining(FAKE_STOCK_DATA));
6059
stock.delete().then(done).catch(done.fail);
6160
});
62-
});*/
61+
});
62+
63+
it('should not optionally map the doc ID to the emitted data object if doc does not exists', async (done: DoneFn) => {
64+
const randomCollectionName = afs.firestore.collection('a').doc().id;
65+
const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as DocumentReference<Stock>;
66+
const stock = new AngularFirestoreDocument<Stock>(ref, afs);
67+
// await stock.set(FAKE_STOCK_DATA);
68+
const idField = 'myCustomID';
69+
const obs$ = stock.valueChanges({ idField });
70+
obs$.pipe(take(1)).subscribe(async data => {
71+
expect(data).toBeUndefined();
72+
stock.delete().then(done).catch(done.fail);
73+
});
74+
});
6375

6476
});
6577

6678
describe('snapshotChanges()', () => {
6779

68-
it('should get action updates', async (done: any) => {
80+
it('should get action updates', async (done: DoneFn) => {
6981
const randomCollectionName = randomName(afs.firestore);
7082
const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as DocumentReference<Stock>;
7183
const stock = new AngularFirestoreDocument<Stock>(ref, afs);
7284
await stock.set(FAKE_STOCK_DATA);
73-
const sub = stock
74-
.snapshotChanges()
75-
.subscribe(async a => {
76-
sub.unsubscribe();
77-
if (a.payload.exists) {
78-
expect(a.payload.data()).toEqual(FAKE_STOCK_DATA);
79-
stock.delete().then(done).catch(done.fail);
80-
}
81-
});
82-
});
83-
84-
it('should get unwrapped snapshot', async (done: any) => {
85-
const randomCollectionName = afs.firestore.collection('a').doc().id;
86-
const ref = afs.firestore.doc(`${randomCollectionName}/FAKE`) as DocumentReference<Stock>;
87-
const stock = new AngularFirestoreDocument<Stock>(ref, afs);
88-
await stock.set(FAKE_STOCK_DATA);
89-
const obs$ = stock.valueChanges();
90-
obs$.pipe(take(1)).subscribe(async data => {
91-
expect(data).toEqual(FAKE_STOCK_DATA);
85+
const obs$ = stock.snapshotChanges();
86+
obs$.pipe(take(1)).subscribe(async a => {
87+
expect(a.payload.data()).toEqual(FAKE_STOCK_DATA);
9288
stock.delete().then(done).catch(done.fail);
9389
});
9490
});

src/firestore/document/document.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class AngularFirestoreDocument<T = DocumentData> {
8888
valueChanges<K extends string>(options: { idField?: K } = {}): Observable<T | undefined> {
8989
return this.snapshotChanges().pipe(
9090
map(({ payload }) =>
91-
options.idField ? {
91+
options.idField && payload.exists ? {
9292
...payload.data(),
9393
...{ [options.idField]: payload.id }
9494
} as T & { [T in K]: string } : payload.data()

0 commit comments

Comments
 (0)