Skip to content

Commit

Permalink
fix: handle missing nested values
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoSantoro94 committed Dec 3, 2024
1 parent db2eb92 commit ef7cbca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions __tests__/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/mocks/helpers/buildDocFromHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

0 comments on commit ef7cbca

Please sign in to comment.