Skip to content

Commit

Permalink
Handle arrays of non-pointers (#81)
Browse files Browse the repository at this point in the history
It looks like we were assuming that any array stored on an object would be an array of pointers. But in fact, we sometimes have arrays of strings, and Parse handles that correctly but parse-mockdb doesn't.
  • Loading branch information
Tyler Breisacher authored Jul 1, 2019
1 parent b33a113 commit 8def956
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/parse-mockdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,14 @@ function includePaths(object, pathsRemaining) {

if (target) {
if (Array.isArray(target)) {
object[path] = target.map(pointer => {
const fetched = fetchObjectByPointer(pointer);
includePaths(fetched, _.cloneDeep(pathsRemaining));
return fetched;
object[path] = target.map(item => {
if (item.className) {
// This is a pointer or an object
const fetched = fetchObjectByPointer(item);
includePaths(fetched, _.cloneDeep(pathsRemaining));
return fetched;
}
return item;
});
} else {
if (object[path].__type === 'Pointer') {
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,21 @@ describe('ParseMock', () => {
});
});

it('should handle includes for array of string', () => {
const item = new Item({ alternateNames: ['item1', 'originalItem'] });
return Parse.Object.saveAll([item]).then(() => {
const brand = new Brand({
item,
});
return brand.save();
}).then(() => {
const q = new Parse.Query(Brand).include('item,item.alternateNames');
return q.first();
}).then((brand) => {
assert.deepEqual(brand.get('item').get('alternateNames'), ['item1', 'originalItem']);
});
});

it('should handle includes where item is missing', () => {
const item = new Item({ cool: true });
const brand1 = new Brand({});
Expand Down

0 comments on commit 8def956

Please sign in to comment.