From 2f6189b6405ccaa1e0ffbf055669970f60300476 Mon Sep 17 00:00:00 2001 From: Sandile Date: Fri, 11 Feb 2022 10:58:58 +0200 Subject: [PATCH] fix(actions-core): Added testContext for action testing. --- .pnp.cjs | 2 + .../plugins/actions/actions-core/package.json | 2 + .../actions-core/src/actions/testContext.js | 71 +++++++++++++++++++ yarn.lock | 2 + 4 files changed, 77 insertions(+) create mode 100644 packages/plugins/actions/actions-core/src/actions/testContext.js diff --git a/.pnp.cjs b/.pnp.cjs index 160643831..865f0b23f 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -2792,7 +2792,9 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "packageLocation": "./packages/plugins/actions/actions-core/", "packageDependencies": [ ["@lowdefy/actions-core", "workspace:packages/plugins/actions/actions-core"], + ["@lowdefy/engine", "workspace:packages/engine"], ["@lowdefy/helpers", "workspace:packages/utils/helpers"], + ["@lowdefy/operators", "workspace:packages/operators"], ["@swc/cli", "virtual:babee6e81435a5d101529cd67f2c6b175f4db37a4ab0b58df15adf73dd11be8917ac14caf44ab4e6882a92c61661055072365b349016e85173e049f006fc2305#npm:0.1.55"], ["@swc/core", "npm:1.2.135"], ["@swc/jest", "virtual:babee6e81435a5d101529cd67f2c6b175f4db37a4ab0b58df15adf73dd11be8917ac14caf44ab4e6882a92c61661055072365b349016e85173e049f006fc2305#npm:0.2.17"], diff --git a/packages/plugins/actions/actions-core/package.json b/packages/plugins/actions/actions-core/package.json index 3d231e02d..cc68c5d06 100644 --- a/packages/plugins/actions/actions-core/package.json +++ b/packages/plugins/actions/actions-core/package.json @@ -49,6 +49,8 @@ "@lowdefy/helpers": "4.0.0-alpha.6" }, "devDependencies": { + "@lowdefy/engine": "4.0.0-alpha.6", + "@lowdefy/operators": "4.0.0-alpha.6", "@swc/cli": "0.1.55", "@swc/core": "1.2.135", "@swc/jest": "0.2.17", diff --git a/packages/plugins/actions/actions-core/src/actions/testContext.js b/packages/plugins/actions/actions-core/src/actions/testContext.js new file mode 100644 index 000000000..dd8a1b196 --- /dev/null +++ b/packages/plugins/actions/actions-core/src/actions/testContext.js @@ -0,0 +1,71 @@ +/* + Copyright 2020-2021 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 { WebParser } from '@lowdefy/operators'; + +import { Actions } from '@lowdefy/engine'; +import { Blocks } from '@lowdefy/engine'; +import { Requests } from '@lowdefy/engine'; +import { State } from '@lowdefy/engine'; + +const testContext = async ({ lowdefy, operators, rootBlock, initState = {} }) => { + const testLowdefy = { + inputs: { test: {} }, + urlQuery: {}, + ...lowdefy, + _internal: { + displayMessage: () => () => undefined, + updateBlock: () => {}, + ...lowdefy._internal, + }, + }; + const ctx = { + id: 'test', + pageId: rootBlock.blockId, + eventLog: [], + requests: {}, + state: {}, + _internal: { + lowdefy: testLowdefy, + rootBlock, + }, + }; + const _internal = ctx._internal; + _internal.parser = new WebParser({ context: ctx, contexts: {}, operators: operators || {} }); + await _internal.parser.init(); + _internal.State = new State(ctx); + _internal.Actions = new Actions(ctx); + _internal.Requests = new Requests(ctx); + _internal.RootBlocks = new Blocks({ + areas: _internal.rootBlock.areas, + context: ctx, + }); + _internal.RootBlocks.init(); + _internal.update = () => { + _internal.RootBlocks.update(); + }; + if (initState) { + Object.keys(initState).forEach((key) => { + _internal.State.set(key, initState[key]); + }); + _internal.RootBlocks.reset(); + } + _internal.update(); + _internal.State.freezeState(); + return ctx; +}; + +export default testContext; diff --git a/yarn.lock b/yarn.lock index 90afdda82..9cbca4ed8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1956,7 +1956,9 @@ __metadata: version: 0.0.0-use.local resolution: "@lowdefy/actions-core@workspace:packages/plugins/actions/actions-core" dependencies: + "@lowdefy/engine": 4.0.0-alpha.6 "@lowdefy/helpers": 4.0.0-alpha.6 + "@lowdefy/operators": 4.0.0-alpha.6 "@swc/cli": 0.1.55 "@swc/core": 1.2.135 "@swc/jest": 0.2.17