diff --git a/__tests__/query.test.js b/__tests__/query.test.js index 686de73..c73fc72 100644 --- a/__tests__/query.test.js +++ b/__tests__/query.test.js @@ -221,6 +221,18 @@ describe('Queries', () => { expect(color).toEqual('brown'); }); + test('it can handle missing nested values', async () => { + const res = await db + .collection('animals') + .where('id', '==', 'cow') + .select('size.height.shoulder') + .get(); + + expect(res).toHaveProperty('size', 1); + const data = res.docs[0].data(); + expect(data).toHaveProperty('size', {}); + }); + // TODO should add support test.skip('it can select many nested values', async () => { const res = await db diff --git a/src/mocks/helpers/buildDocFromHash.js b/src/mocks/helpers/buildDocFromHash.js index acc273d..50c1fd2 100644 --- a/src/mocks/helpers/buildDocFromHash.js +++ b/src/mocks/helpers/buildDocFromHash.js @@ -62,8 +62,13 @@ module.exports = function buildDocFromHash(hash = {}, id = 'abc123', selectField }; function buildDocFromPath(data, path) { + if (data === undefined || data === null) { + return {}; + } + const [root, ...subPath] = path; + const rootData = data[root]; return { - [root]: subPath.length ? buildDocFromPath(data[root], subPath) : data[root] + [root]: subPath.length ? buildDocFromPath(rootData, subPath) : rootData }; }