From d5e015d22953b93ef43f7602dd5f0372f26efa14 Mon Sep 17 00:00:00 2001 From: Kasper Mikiewicz Date: Thu, 8 Mar 2018 15:28:05 +0100 Subject: [PATCH 1/3] feat: handle mapped name in nested arrays --- packages/lib-js-core/src/data.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/lib-js-core/src/data.js b/packages/lib-js-core/src/data.js index 2f969454..08fa3479 100644 --- a/packages/lib-js-core/src/data.js +++ b/packages/lib-js-core/src/data.js @@ -275,7 +275,9 @@ class Data extends QueryBuilder { if (Array.isArray(itemField)) { itemField.forEach((arrItem, i) => { const path = `${itemFieldKey}.[${i}].${key.split('.').slice(1)}` - set(all, path, get(item, path)) + const mappedPath = `${itemFieldKey}.[${i}].${(fields[key] || key).split('.').slice(1)}` + + set(all, mappedPath, get(item, path)) }) } else { set(all, fields[key] || key, get(item, key)) From 8a0f4ea14c75d80d4c929fadb0ec33c163ca75d2 Mon Sep 17 00:00:00 2001 From: Kasper Mikiewicz Date: Thu, 8 Mar 2018 16:09:30 +0100 Subject: [PATCH 2/3] chore: add unit and fix array of strings --- packages/lib-js-core/package.json | 1 - packages/lib-js-core/src/data.js | 2 +- packages/lib-js-core/test/unit/data.js | 37 ++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/lib-js-core/package.json b/packages/lib-js-core/package.json index affbc710..df3b4b8e 100644 --- a/packages/lib-js-core/package.json +++ b/packages/lib-js-core/package.json @@ -17,7 +17,6 @@ "test": "npm run test:unit && npm run test:e2e", "test:base": "cross-env NODE_ENV=test mocha --reporter spec --require babel-register", "test:unit": "npm run test:base -- test/unit/**/*.js", - "test:e2e": "npm run test:base -- --timeout 30000 --slow 8000 test/e2e/*.js", "lint": "standard --fix --env mocha", "format": "prettier --write --single-quote --no-semi --no-bracket-spacing *.js {src,test}/**/*.js" }, diff --git a/packages/lib-js-core/src/data.js b/packages/lib-js-core/src/data.js index 08fa3479..1d1866c4 100644 --- a/packages/lib-js-core/src/data.js +++ b/packages/lib-js-core/src/data.js @@ -272,7 +272,7 @@ class Data extends QueryBuilder { const itemFieldKey = key.split('.').shift() const itemField = get(item, itemFieldKey) - if (Array.isArray(itemField)) { + if (Array.isArray(itemField) && typeof itemField[0] === 'object' && itemField[0] !== null) { itemField.forEach((arrItem, i) => { const path = `${itemFieldKey}.[${i}].${key.split('.').slice(1)}` const mappedPath = `${itemFieldKey}.[${i}].${(fields[key] || key).split('.').slice(1)}` diff --git a/packages/lib-js-core/test/unit/data.js b/packages/lib-js-core/test/unit/data.js index 15cd0c93..64f7a2d9 100644 --- a/packages/lib-js-core/test/unit/data.js +++ b/packages/lib-js-core/test/unit/data.js @@ -825,6 +825,43 @@ describe('Data', () => { .should.become([{author: 'John', views: 100}]) }) + it.only('should work with nested array of objects', () => { + api + .get(`/v2/instances/${instanceName}/classes/users/objects/`) + .reply(200, {objects: [ + { + name: 'John', + views: 100, + id: 2, + tags: ['css', 'html'], + documents: [ + { + id: 10, + name: 'Test document' + } + ] + } + ]}) + + return data.users + .fields([ + 'name as author', + 'views', + 'tags', + 'documents.id', + 'documents.name as documents.title' + ]) + .list() + .should.become([{ + tags: ['css', 'html'], + documents: [ + {id: 10, title: 'Test document'} + ], + author: 'John', + views: 100 + }]) + }) + it('should work with create method', () => { api .post(`/v2/instances/${instanceName}/classes/posts/objects/`) From 5c402c7935b622525db6fa93dad4995b63c3730b Mon Sep 17 00:00:00 2001 From: Kasper Mikiewicz Date: Thu, 8 Mar 2018 16:17:10 +0100 Subject: [PATCH 3/3] chore: remove only --- packages/lib-js-core/test/unit/data.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lib-js-core/test/unit/data.js b/packages/lib-js-core/test/unit/data.js index 64f7a2d9..3a206721 100644 --- a/packages/lib-js-core/test/unit/data.js +++ b/packages/lib-js-core/test/unit/data.js @@ -825,7 +825,7 @@ describe('Data', () => { .should.become([{author: 'John', views: 100}]) }) - it.only('should work with nested array of objects', () => { + it('should work with nested array of objects', () => { api .get(`/v2/instances/${instanceName}/classes/users/objects/`) .reply(200, {objects: [