diff --git a/packages/build/src/build/writeConnections.js b/packages/build/src/build/writeConnections.js index 5323437d8..69778051d 100644 --- a/packages/build/src/build/writeConnections.js +++ b/packages/build/src/build/writeConnections.js @@ -17,18 +17,18 @@ import { type } from '@lowdefy/helpers'; async function writeConnections({ components, context }) { - let writePromises = []; - if (type.isArray(components.connections)) { - writePromises = components.connections.map(async (connection) => { - await context.artifactSetter.set({ - filePath: `connections/${connection.connectionId}.json`, - content: JSON.stringify(connection, null, 2), - }); - await context.logger.info(`Updated connection: ${connection.connectionId}`); - }); + if (type.isNone(components.connections)) return; + if (!type.isArray(components.connections)) { + throw new Error(`Connections is not an array.`); } - - await Promise.all(writePromises); + const writePromises = components.connections.map(async (connection) => { + await context.artifactSetter.set({ + filePath: `connections/${connection.connectionId}.json`, + content: JSON.stringify(connection, null, 2), + }); + await context.logger.info(`Updated connection ${connection.connectionId}`); + }); + return Promise.all(writePromises); } export default writeConnections; diff --git a/packages/build/src/build/writeConnections.test.js b/packages/build/src/build/writeConnections.test.js index aa2dae7ef..f94f11964 100644 --- a/packages/build/src/build/writeConnections.test.js +++ b/packages/build/src/build/writeConnections.test.js @@ -35,7 +35,7 @@ beforeEach(() => { mockSet.mockReset(); }); -test('writeConnections add connection', async () => { +test('writeConnections write connection', async () => { const components = { connections: [ { @@ -62,7 +62,7 @@ test('writeConnections add connection', async () => { }, ], ]); - expect(mockLogInfo.mock.calls).toEqual([['Updated connection: connection1']]); + expect(mockLogInfo.mock.calls).toEqual([['Updated connection connection1']]); }); test('writeConnections multiple connection', async () => { @@ -100,8 +100,8 @@ test('writeConnections multiple connection', async () => { ], ]); expect(mockLogInfo.mock.calls).toEqual([ - ['Updated connection: connection1'], - ['Updated connection: connection2'], + ['Updated connection connection1'], + ['Updated connection connection2'], ]); }); @@ -125,7 +125,7 @@ test('writeConnections connections not an array', async () => { const components = { connections: 'connections', }; - await writeConnections({ components, context }); - expect(mockSet.mock.calls).toEqual([]); - expect(mockLogInfo.mock.calls).toEqual([]); + await expect(writeConnections({ components, context })).rejects.toThrow( + 'Connections is not an array.' + ); }); diff --git a/packages/build/src/build/writePages.js b/packages/build/src/build/writePages.js new file mode 100644 index 000000000..d6439062e --- /dev/null +++ b/packages/build/src/build/writePages.js @@ -0,0 +1,39 @@ +/* + Copyright 2020 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import { type } from '@lowdefy/helpers'; + +async function writePage({ page, context }) { + if (!type.isObject(page)) { + throw new Error(`Page is not an object.`); + } + await context.artifactSetter.set({ + filePath: `pages/${page.pageId}/${page.pageId}.json`, + content: JSON.stringify(page, null, 2), + }); + await context.logger.info(`Updated page ${page.pageId}`); +} + +async function writePages({ components, context }) { + if (type.isNone(components.pages)) return; + if (!type.isArray(components.pages)) { + throw new Error(`Pages is not an array.`); + } + const writePromises = components.pages.map((page) => writePage({ page, context })); + return Promise.all(writePromises); +} + +export default writePages; diff --git a/packages/build/src/build/writePages.test.js b/packages/build/src/build/writePages.test.js new file mode 100644 index 000000000..126ab1522 --- /dev/null +++ b/packages/build/src/build/writePages.test.js @@ -0,0 +1,138 @@ +/* + Copyright 2020 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import writePages from './writePages'; +import testContext from '../test/testContext'; + +const mockLogInfo = jest.fn(); +const mockSet = jest.fn(); + +const logger = { + info: mockLogInfo, +}; + +const artifactSetter = { + set: mockSet, +}; + +const context = testContext({ logger, artifactSetter }); + +beforeEach(() => { + mockLogInfo.mockReset(); + mockSet.mockReset(); +}); + +test('writePages write page', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + blockId: 'page1', + requests: [], + mutations: [], + }, + ], + }; + await writePages({ components, context }); + expect(mockSet.mock.calls).toEqual([ + [ + { + filePath: 'pages/page1/page1.json', + content: `{ + "id": "page:page1", + "pageId": "page1", + "blockId": "page1", + "requests": [], + "mutations": [] +}`, + }, + ], + ]); + expect(mockLogInfo.mock.calls).toEqual([['Updated page page1']]); +}); + +test('writePages multiple pages', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + blockId: 'page1', + requests: [], + mutations: [], + }, + { + id: 'page:page2', + pageId: 'page2', + blockId: 'page2', + requests: [], + mutations: [], + }, + ], + }; + await writePages({ components, context }); + expect(mockSet.mock.calls).toEqual([ + [ + { + filePath: 'pages/page1/page1.json', + content: `{ + "id": "page:page1", + "pageId": "page1", + "blockId": "page1", + "requests": [], + "mutations": [] +}`, + }, + ], + [ + { + filePath: 'pages/page2/page2.json', + content: `{ + "id": "page:page2", + "pageId": "page2", + "blockId": "page2", + "requests": [], + "mutations": [] +}`, + }, + ], + ]); + expect(mockLogInfo.mock.calls).toEqual([['Updated page page1'], ['Updated page page2']]); +}); + +test('writePages no pages', async () => { + const components = { + pages: [], + }; + await writePages({ components, context }); + expect(mockSet.mock.calls).toEqual([]); + expect(mockLogInfo.mock.calls).toEqual([]); +}); + +test('writePages pages undefined', async () => { + const components = {}; + await writePages({ components, context }); + expect(mockSet.mock.calls).toEqual([]); + expect(mockLogInfo.mock.calls).toEqual([]); +}); + +test('writePages pages not an array', async () => { + const components = { + pages: 'pages', + }; + await expect(writePages({ components, context })).rejects.toThrow('Pages is not an array.'); +}); diff --git a/packages/build/src/build/writeRequests.js b/packages/build/src/build/writeRequests.js new file mode 100644 index 000000000..193b4b3c4 --- /dev/null +++ b/packages/build/src/build/writeRequests.js @@ -0,0 +1,93 @@ +/* + Copyright 2020 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import { serializer, type } from '@lowdefy/helpers'; + +function getRequestsAndMutationsOnBlock({ block, requests, mutations, pageId }) { + if (!type.isObject(block)) { + throw new Error(`Block is not an object on page "${pageId}".`); + } + if (!type.isNone(block.requests)) { + if (!type.isArray(block.requests)) { + throw new Error(`Requests is not an array on page "${pageId}".`); + } + block.requests.forEach((request) => { + requests.push(serializer.copy(request)); + delete request.properties; + }); + } + if (!type.isNone(block.mutations)) { + if (!type.isArray(block.mutations)) { + throw new Error(`Mutations is not an array on page "${pageId}".`); + } + block.mutations.forEach((mutation) => { + mutations.push(serializer.copy(mutation)); + delete mutation.properties; + }); + } + if (type.isObject(block.areas)) { + Object.keys(block.areas).forEach((key) => { + if (type.isNone(block.areas[key].blocks)) return; + if (!type.isArray(block.areas[key].blocks)) { + throw new Error( + `Blocks is not an array on page "${pageId}", block "${block.blockId}", area "${key}".` + ); + } + block.areas[key].blocks.forEach((blk) => { + getRequestsAndMutationsOnBlock({ block: blk, requests, mutations, pageId }); + }); + }); + } +} + +async function updateRequestsOnPage({ page, context }) { + if (!type.isObject(page)) { + throw new Error(`Page is not an object.`); + } + const requests = []; + const mutations = []; + getRequestsAndMutationsOnBlock({ block: page, requests, mutations, pageId: page.pageId }); + + const writeRequestPromises = requests.map(async (request) => { + await context.artifactSetter.set({ + filePath: `pages/${page.pageId}/requests/${request.requestId}.json`, + content: JSON.stringify(request, null, 2), + }); + await context.logger.info(`Updated request ${request.requestId} on page ${page.pageId}`); + }); + const writeMutationPromises = mutations.map(async (mutation) => { + await context.artifactSetter.set({ + filePath: `pages/${page.pageId}/mutations/${mutation.mutationId}.json`, + content: JSON.stringify(mutation, null, 2), + }); + await context.logger.info(`Updated mutation ${mutation.mutationId} on page ${page.pageId}`); + }); + + return Promise.all([...writeRequestPromises, ...writeMutationPromises]); +} + +async function writeRequests({ components, context }) { + if (type.isNone(components.pages)) return; + if (!type.isArray(components.pages)) { + throw new Error(`Pages is not an array.`); + } + const writePromises = components.pages.map((page) => updateRequestsOnPage({ page, context })); + return Promise.all(writePromises); +} + +export { getRequestsAndMutationsOnBlock }; + +export default writeRequests; diff --git a/packages/build/src/build/writeRequests.test.js b/packages/build/src/build/writeRequests.test.js new file mode 100644 index 000000000..edd1eafdb --- /dev/null +++ b/packages/build/src/build/writeRequests.test.js @@ -0,0 +1,423 @@ +/* + Copyright 2020 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import writeRequests from './writeRequests'; +import testContext from '../test/testContext'; + +const mockLogInfo = jest.fn(); +const mockSet = jest.fn(); + +const logger = { + info: mockLogInfo, +}; + +const artifactSetter = { + set: mockSet, +}; + +const context = testContext({ logger, artifactSetter }); + +beforeEach(() => { + mockLogInfo.mockReset(); + mockSet.mockReset(); +}); + +test('writeRequests write request', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + requests: [ + { + id: 'request:page1:page1:request1', + requestId: 'request1', + connectionId: 'connection1', + properties: { key: 'value' }, + }, + ], + mutations: [], + }, + ], + }; + await writeRequests({ components, context }); + expect(mockSet.mock.calls).toEqual([ + [ + { + filePath: 'pages/page1/requests/request1.json', + content: `{ + "id": "request:page1:page1:request1", + "requestId": "request1", + "connectionId": "connection1", + "properties": { + "key": "value" + } +}`, + }, + ], + ]); + expect(mockLogInfo.mock.calls).toEqual([['Updated request request1 on page page1']]); +}); + +test('writeRequests write nested request', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + areas: { + content: { + blocks: [ + { + id: 'block:block1', + blockId: 'block1', + requests: [ + { + id: 'request:page1:page1:request1', + requestId: 'request1', + connectionId: 'connection1', + properties: { key: 'value' }, + }, + ], + }, + ], + }, + }, + mutations: [], + }, + ], + }; + await writeRequests({ components, context }); + expect(mockSet.mock.calls).toEqual([ + [ + { + filePath: 'pages/page1/requests/request1.json', + content: `{ + "id": "request:page1:page1:request1", + "requestId": "request1", + "connectionId": "connection1", + "properties": { + "key": "value" + } +}`, + }, + ], + ]); + expect(mockLogInfo.mock.calls).toEqual([['Updated request request1 on page page1']]); +}); + +test('writeRequests add mutation', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + requests: [], + mutations: [ + { + id: 'mutation:page1:page1:mutation1', + mutationId: 'mutation1', + connectionId: 'connection1', + properties: { key: 'value' }, + }, + ], + }, + ], + }; + await writeRequests({ components, context }); + expect(mockSet.mock.calls).toEqual([ + [ + { + filePath: 'pages/page1/mutations/mutation1.json', + content: `{ + "id": "mutation:page1:page1:mutation1", + "mutationId": "mutation1", + "connectionId": "connection1", + "properties": { + "key": "value" + } +}`, + }, + ], + ]); + expect(mockLogInfo.mock.calls).toEqual([['Updated mutation mutation1 on page page1']]); +}); + +test('writeRequests add nested mutation', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + areas: { + content: { + blocks: [ + { + id: 'block:block1', + blockId: 'block1', + mutations: [ + { + id: 'mutation:page1:page1:mutation1', + mutationId: 'mutation1', + connectionId: 'connection1', + properties: { key: 'value' }, + }, + ], + }, + ], + }, + }, + }, + ], + }; + await writeRequests({ components, context }); + expect(mockSet.mock.calls).toEqual([ + [ + { + filePath: 'pages/page1/mutations/mutation1.json', + content: `{ + "id": "mutation:page1:page1:mutation1", + "mutationId": "mutation1", + "connectionId": "connection1", + "properties": { + "key": "value" + } +}`, + }, + ], + ]); + expect(mockLogInfo.mock.calls).toEqual([['Updated mutation mutation1 on page page1']]); +}); + +test('writeRequests requests is not an array', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + requests: 'requests', + }, + ], + }; + await expect(writeRequests({ components, context })).rejects.toThrow( + 'Requests is not an array on page "page1"' + ); +}); + +test('writeRequests mutations is not an array', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + mutations: 'mutations', + }, + ], + }; + await expect(writeRequests({ components, context })).rejects.toThrow( + 'Mutations is not an array on page "page1"' + ); +}); + +test('writeRequests empty pages array', async () => { + const components = { + pages: [], + }; + await writeRequests({ components, context }); + expect(mockSet.mock.calls).toEqual([]); + expect(mockLogInfo.mock.calls).toEqual([]); +}); + +test('writeRequests no pages array', async () => { + const components = {}; + await writeRequests({ components, context }); + expect(mockSet.mock.calls).toEqual([]); + expect(mockLogInfo.mock.calls).toEqual([]); +}); + +test('writeRequests pages not an array', async () => { + const components = { + pages: 'pages', + }; + await expect(writeRequests({ components, context })).rejects.toThrow('Pages is not an array.'); +}); + +test('writeRequests page is not a object', async () => { + const components = { + pages: ['page'], + }; + await expect(writeRequests({ components, context })).rejects.toThrow('Page is not an object.'); +}); + +test('writeRequests to throw when blocks is not a array', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + blockId: 'page1', + areas: { + content: { + blocks: 'blocks', + }, + }, + }, + ], + }; + await expect(writeRequests({ components, context })).rejects.toThrow( + 'Blocks is not an array on page "page1", block "page1", area "content".' + ); +}); + +test('writeRequests deletes request properties', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + requests: [ + { + id: 'request:page1:page1:request1', + requestId: 'request1', + connectionId: 'connection1', + properties: { key: 'value' }, + }, + ], + areas: { + content: { + blocks: [ + { + id: 'block:block1', + blockId: 'block1', + requests: [ + { + id: 'request:request2', + requestId: 'request1', + connectionId: 'connection1', + properties: { key: 'value' }, + }, + ], + }, + ], + }, + }, + }, + ], + }; + await writeRequests({ components, context }); + expect(components).toEqual({ + pages: [ + { + id: 'page:page1', + pageId: 'page1', + requests: [ + { + id: 'request:page1:page1:request1', + requestId: 'request1', + connectionId: 'connection1', + }, + ], + areas: { + content: { + blocks: [ + { + id: 'block:block1', + blockId: 'block1', + requests: [ + { + id: 'request:request2', + requestId: 'request1', + connectionId: 'connection1', + }, + ], + }, + ], + }, + }, + }, + ], + }); +}); + +test('writeRequests deletes mutation properties', async () => { + const components = { + pages: [ + { + id: 'page:page1', + pageId: 'page1', + mutations: [ + { + id: 'mutation:page1:page1:mutation1', + mutationId: 'mutation1', + connectionId: 'connection1', + properties: { key: 'value' }, + }, + ], + areas: { + content: { + blocks: [ + { + id: 'block:block1', + blockId: 'block1', + mutations: [ + { + id: 'mutation:mutation2', + mutationId: 'mutation2', + connectionId: 'connection1', + properties: { key: 'value' }, + }, + ], + }, + ], + }, + }, + }, + ], + }; + await writeRequests({ components, context }); + expect(components).toEqual({ + pages: [ + { + id: 'page:page1', + pageId: 'page1', + mutations: [ + { + id: 'mutation:page1:page1:mutation1', + mutationId: 'mutation1', + connectionId: 'connection1', + }, + ], + areas: { + content: { + blocks: [ + { + id: 'block:block1', + blockId: 'block1', + mutations: [ + { + id: 'mutation:mutation2', + mutationId: 'mutation2', + connectionId: 'connection1', + }, + ], + }, + ], + }, + }, + }, + ], + }); +}); diff --git a/packages/build/src/utils/getRemovedFromArray.js b/packages/build/src/utils/getRemovedFromArray.js deleted file mode 100644 index 0a3b0cc64..000000000 --- a/packages/build/src/utils/getRemovedFromArray.js +++ /dev/null @@ -1,21 +0,0 @@ -/* - Copyright 2020 Lowdefy, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -function getRemovedFromArray(oldArray = [], newArray = []) { - return oldArray.filter((old) => !newArray.includes(old)); -} - -export default getRemovedFromArray; diff --git a/packages/build/src/utils/getRemovedFromArray.test.js b/packages/build/src/utils/getRemovedFromArray.test.js deleted file mode 100644 index 3acd515fa..000000000 --- a/packages/build/src/utils/getRemovedFromArray.test.js +++ /dev/null @@ -1,42 +0,0 @@ -/* - Copyright 2020 Lowdefy, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -import getRemovedFromArray from './getRemovedFromArray'; - -test('getRemovedFromArray defaults', () => { - const res = getRemovedFromArray(); - expect(res).toEqual([]); -}); - -test('getRemovedFromArray both empty', () => { - const res = getRemovedFromArray([], []); - expect(res).toEqual([]); -}); - -test('getRemovedFromArray nothing removed', () => { - const res = getRemovedFromArray(['1'], ['1', '2']); - expect(res).toEqual([]); -}); - -test('getRemovedFromArray one item removed', () => { - const res = getRemovedFromArray(['1', '2'], ['1']); - expect(res).toEqual(['2']); -}); - -test('getRemovedFromArray multiple items removed', () => { - const res = getRemovedFromArray(['1', '2', '3', '4'], ['1', '3', '5', '6']); - expect(res).toEqual(['2', '4']); -}); diff --git a/packages/build/src/utils/getRemovedObjects.js b/packages/build/src/utils/getRemovedObjects.js deleted file mode 100644 index 2d150c65a..000000000 --- a/packages/build/src/utils/getRemovedObjects.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - Copyright 2020 Lowdefy, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -import { get } from '@lowdefy/helpers'; - -function getRemovedObjects({ oldObjects = [], newObjects = [], objectId }) { - let id; - if (objectId) { - id = objectId; - } else { - id = 'id'; - } - return oldObjects.filter((oldObject) => { - if (!oldObject) { - return false; - } - if ( - newObjects.find( - (object) => get(object, id, { default: 'n' }) === get(oldObject, id, { default: 'o' }) - ) - ) { - return false; - } - return true; - }); -} - -export default getRemovedObjects; diff --git a/packages/build/src/utils/getRemovedObjects.test.js b/packages/build/src/utils/getRemovedObjects.test.js deleted file mode 100644 index e19002407..000000000 --- a/packages/build/src/utils/getRemovedObjects.test.js +++ /dev/null @@ -1,204 +0,0 @@ -/* - Copyright 2020 Lowdefy, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -import getRemovedObjects from './getRemovedObjects'; - -describe('getRemovedObjects', () => { - test('getRemovedObjects both empty', () => { - const oldObjects = []; - const newObjects = []; - const res = getRemovedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - - test('getRemovedObjects defaults', () => { - const oldObjects = undefined; - const newObjects = undefined; - const res = getRemovedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - - test('getRemovedObjects no new objects', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const newObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const res = getRemovedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - - test('getRemovedObjects object changed', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const newObjects = [ - { - id: '1', - properties: { - prop: '2', - }, - }, - ]; - const res = getRemovedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - - test('getRemovedObjects new object', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const newObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - { - id: '2', - properties: { - prop: '2', - }, - }, - ]; - const res = getRemovedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - test('getRemovedObjects object removed', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - { - id: '2', - properties: { - prop: '2', - }, - }, - ]; - const newObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const res = getRemovedObjects({ oldObjects, newObjects }); - expect(res).toEqual([ - { - id: '2', - properties: { - prop: '2', - }, - }, - ]); - }); - - test('getRemovedObjects objectId', () => { - const oldObjects = [ - { - name: '1', - properties: { - prop: '1', - }, - }, - { - name: '2', - properties: { - prop: '2', - }, - }, - ]; - const newObjects = [ - { - name: '1', - properties: { - prop: '1', - }, - }, - ]; - const res = getRemovedObjects({ oldObjects, newObjects, objectId: 'name' }); - expect(res).toEqual([ - { - name: '2', - properties: { - prop: '2', - }, - }, - ]); - }); - - test('getRemovedObjects old object null', () => { - const oldObjects = [null]; - const newObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const res = getRemovedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - - test('getRemovedObjects new object null', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const newObjects = [null]; - const res = getRemovedObjects({ oldObjects, newObjects }); - expect(res).toEqual([ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]); - }); -}); diff --git a/packages/build/src/utils/getUpdatedObjects.js b/packages/build/src/utils/getUpdatedObjects.js deleted file mode 100644 index 28ba3a3f7..000000000 --- a/packages/build/src/utils/getUpdatedObjects.js +++ /dev/null @@ -1,38 +0,0 @@ -/* - Copyright 2020 Lowdefy, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -import util from 'util'; -import { get } from '@lowdefy/helpers'; - -function getUpdatedObjects({ oldObjects = [], newObjects = [], objectId }) { - let id; - if (objectId) { - id = objectId; - } else { - id = 'id'; - } - return newObjects.filter((newObject) => { - if (!newObject) { - return false; - } - const oldObject = oldObjects.find( - (object) => get(object, id, { default: 'o' }) === get(newObject, id, { default: 'n' }) - ); - return !util.isDeepStrictEqual(oldObject, newObject); - }); -} - -export default getUpdatedObjects; diff --git a/packages/build/src/utils/getUpdatedObjects.test.js b/packages/build/src/utils/getUpdatedObjects.test.js deleted file mode 100644 index b451579b8..000000000 --- a/packages/build/src/utils/getUpdatedObjects.test.js +++ /dev/null @@ -1,206 +0,0 @@ -/* - Copyright 2020 Lowdefy, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -import getUpdatedObjects from './getUpdatedObjects'; - -describe('getUpdatedObjects', () => { - test('getUpdatedObjects both empty', () => { - const oldObjects = []; - const newObjects = []; - const res = getUpdatedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - - test('getUpdatedObjects defaults', () => { - const oldObjects = undefined; - const newObjects = undefined; - const res = getUpdatedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - - test('getUpdatedObjects no new objects', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const newObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const res = getUpdatedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - - test('getUpdatedObjects object changed', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const newObjects = [ - { - id: '1', - properties: { - prop: '2', - }, - }, - ]; - const res = getUpdatedObjects({ oldObjects, newObjects }); - expect(res).toEqual([ - { - id: '1', - properties: { - prop: '2', - }, - }, - ]); - }); - - test('getUpdatedObjects objectId', () => { - const oldObjects = [ - { - name: '1', - properties: { - prop: '1', - }, - }, - ]; - const newObjects = [ - { - name: '1', - properties: { - prop: '2', - }, - }, - ]; - const res = getUpdatedObjects({ oldObjects, newObjects, objectId: 'name' }); - expect(res).toEqual([ - { - name: '1', - properties: { - prop: '2', - }, - }, - ]); - }); - - test('getUpdatedObjects new object', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const newObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - { - id: '2', - properties: { - prop: '2', - }, - }, - ]; - const res = getUpdatedObjects({ oldObjects, newObjects }); - expect(res).toEqual([ - { - id: '2', - properties: { - prop: '2', - }, - }, - ]); - }); - - test('getUpdatedObjects object removed', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - { - id: '2', - properties: { - prop: '2', - }, - }, - ]; - const newObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const res = getUpdatedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); - - test('getUpdatedObjects old object null', () => { - const oldObjects = [null]; - const newObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const res = getUpdatedObjects({ oldObjects, newObjects }); - expect(res).toEqual([ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]); - }); - - test('getUpdatedObjects new object null', () => { - const oldObjects = [ - { - id: '1', - properties: { - prop: '1', - }, - }, - ]; - const newObjects = [null]; - const res = getUpdatedObjects({ oldObjects, newObjects }); - expect(res).toEqual([]); - }); -});