fix(actions-core): Fix actions core tests.

This commit is contained in:
Sam 2022-05-30 15:45:38 +02:00
parent 723c7feaed
commit 8647f915be
No known key found for this signature in database
GPG Key ID: D004126FCD1A6DF0
20 changed files with 337 additions and 1659 deletions

2
.pnp.cjs generated
View File

@ -3020,8 +3020,8 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
"packageLocation": "./packages/plugins/actions/actions-core/",
"packageDependencies": [
["@lowdefy/actions-core", "workspace:packages/plugins/actions/actions-core"],
["@jest/globals", "npm:28.1.0"],
["@lowdefy/helpers", "workspace:packages/utils/helpers"],
["@lowdefy/operators", "workspace:packages/operators"],
["@swc/cli", "virtual:babee6e81435a5d101529cd67f2c6b175f4db37a4ab0b58df15adf73dd11be8917ac14caf44ab4e6882a92c61661055072365b349016e85173e049f006fc2305#npm:0.1.57"],
["@swc/core", "npm:1.2.192"],
["@swc/jest", "virtual:babee6e81435a5d101529cd67f2c6b175f4db37a4ab0b58df15adf73dd11be8917ac14caf44ab4e6882a92c61661055072365b349016e85173e049f006fc2305#npm:0.2.21"],

View File

@ -43,13 +43,13 @@
"clean": "rm -rf dist",
"prepare": "yarn build",
"swc": "swc src --out-dir dist --config-file ../../../../.swcrc --delete-dir-on-start --copy-files",
"test": "jest --coverage"
"test": "yarn node --experimental-vm-modules $(yarn bin jest)"
},
"dependencies": {
"@lowdefy/helpers": "4.0.0-alpha.12"
},
"devDependencies": {
"@lowdefy/operators": "4.0.0-alpha.12",
"@jest/globals": "28.1.0",
"@swc/cli": "0.1.57",
"@swc/core": "1.2.192",
"@swc/jest": "0.2.21",

View File

@ -14,15 +14,18 @@
limitations under the License.
*/
import { jest } from '@jest/globals';
import CallMethod from './CallMethod.js';
const mockActionMethod = jest.fn();
const mockCallMethod = jest.fn();
const methods = { callMethod: mockCallMethod };
beforeEach(() => {
mockActionMethod.mockReset();
});
test('CallMethod action invocation', async () => {
CallMethod({ methods: { callMethod: mockActionMethod }, params: 'call' });
expect(mockActionMethod.mock.calls).toEqual([['call']]);
test('CallMethod mock test', async () => {
CallMethod({
methods,
params: { blockId: 'blockId', method: 'method', args: ['arg1', 'arg2'] },
});
expect(mockCallMethod.mock.calls).toEqual([
[{ blockId: 'blockId', method: 'method', args: ['arg1', 'arg2'] }],
]);
});

View File

@ -17,22 +17,14 @@
import { type } from '@lowdefy/helpers';
function DisplayMessage({ methods: { displayMessage }, params }) {
if (!type.isObject(params) && !type.isNone(params)) {
if (!type.isObject(params)) {
throw new Error(
`Invalid DisplayMessage, check action params. Params must be an object, received "${JSON.stringify(
params
)}".`
);
}
if (type.isNone(params)) {
displayMessage({ content: 'Success' });
}
if (type.isObject(params)) {
displayMessage({
...params,
content: type.isNone(params.content) ? 'Success' : params.content,
});
}
displayMessage(params);
}
export default DisplayMessage;

View File

@ -1,351 +1,45 @@
// /*
// Copyright 2020-2022 Lowdefy, Inc
/*
Copyright 2020-2022 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
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
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.
// */
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 testContext from './testContext.js';
// import DisplayMessage from './DisplayMessage.js';
import { jest } from '@jest/globals';
import DisplayMessage from './DisplayMessage.js';
// const displayMessage = jest.fn();
// const lowdefy = {
// _internal: {
// actions: {
// DisplayMessage,
// },
// blockComponents: {
// Button: { meta: { category: 'display' } },
// },
// displayMessage,
// },
// };
const mockDisplayMessage = jest.fn();
const methods = { displayMessage: mockDisplayMessage };
// const RealDate = Date;
// const mockDate = jest.fn(() => ({ date: 0 }));
// mockDate.now = jest.fn(() => 0);
console.log = () => {};
// // Comment out to use console
// console.log = () => {};
// console.error = () => {};
test('DisplayMessage with all params', () => {
DisplayMessage({
methods,
params: { status: 'success', content: 'Hello', duration: 2, icon: 'Icon' },
});
expect(mockDisplayMessage.mock.calls).toEqual([
[{ status: 'success', content: 'Hello', duration: 2, icon: 'Icon' }],
]);
});
// beforeEach(() => {
// displayMessage.mockReset();
// });
// beforeAll(() => {
// global.Date = mockDate;
// });
// afterAll(() => {
// global.Date = RealDate;
// });
// test('DisplayMessage params is not object', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'a',
// type: 'DisplayMessage',
// params: 1,
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// event: undefined,
// eventName: 'onClick',
// error: {
// action: {
// id: 'a',
// type: 'DisplayMessage',
// params: 1,
// },
// error: {
// error: new Error(
// 'Invalid DisplayMessage, check action params. Params must be an object, received "1".'
// ),
// index: 0,
// type: 'DisplayMessage',
// },
// },
// responses: {
// a: {
// error: new Error(
// 'Invalid DisplayMessage, check action params. Params must be an object, received "1".'
// ),
// type: 'DisplayMessage',
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: false,
// });
// });
// test('DisplayMessage params is null or undefined', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'a',
// type: 'DisplayMessage',
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// event: undefined,
// eventName: 'onClick',
// responses: {
// a: {
// response: undefined,
// type: 'DisplayMessage',
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: true,
// });
// expect(displayMessage.mock.calls).toEqual([[{ content: 'Success' }]]);
// });
// test('DisplayMessage params.content is none', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'a',
// type: 'DisplayMessage',
// params: {
// status: 'info',
// },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// event: undefined,
// eventName: 'onClick',
// responses: {
// a: {
// response: undefined,
// type: 'DisplayMessage',
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: true,
// });
// expect(displayMessage.mock.calls).toEqual([[{ content: 'Success', status: 'info' }]]);
// });
// test('DisplayMessage params.content is ""', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'a',
// type: 'DisplayMessage',
// params: {
// content: '',
// },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// event: undefined,
// eventName: 'onClick',
// responses: {
// a: {
// response: undefined,
// type: 'DisplayMessage',
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: true,
// });
// expect(displayMessage.mock.calls).toEqual([[{ content: '' }]]);
// });
// test('DisplayMessage params.content is falsy', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'a',
// type: 'DisplayMessage',
// params: {
// content: 0,
// },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// event: undefined,
// eventName: 'onClick',
// responses: {
// a: {
// response: undefined,
// type: 'DisplayMessage',
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: true,
// });
// expect(displayMessage.mock.calls).toEqual([[{ content: 0 }]]);
// });
test('DisplayMessage params is not object', () => {
expect(() => DisplayMessage({ methods, params: 'Message content.' })).toThrow(
'Invalid DisplayMessage, check action params. Params must be an object, received ""Message content."".'
);
expect(() => DisplayMessage({ methods, params: null })).toThrow(
'Invalid DisplayMessage, check action params. Params must be an object, received "null".'
);
expect(() => DisplayMessage({ methods })).toThrow(
'Invalid DisplayMessage, check action params. Params must be an object, received "undefined".'
);
});

View File

@ -1,163 +1,60 @@
// /*
// Copyright 2020-2022 Lowdefy, Inc
/*
Copyright 2020-2022 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
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
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.
// */
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 testContext from './testContext.js';
// import Link from './Link.js';
import { jest } from '@jest/globals';
import Link from './Link.js';
// const mockActionMethod = jest.fn();
const mockLink = jest.fn();
const methods = { link: mockLink };
// const lowdefy = {
// _internal: {
// actions: {
// Link,
// },
// blockComponents: {
// Button: { meta: { category: 'display' } },
// },
// link: (params) => {
// if (params.pageId === 'error') {
// throw new Error('Param error');
// } else mockActionMethod(params);
// },
// },
// };
console.log = () => {};
// // Comment out to use console
// console.log = () => {};
// console.error = () => {};
test('Link with params', () => {
Link({
methods,
params: { pageId: 'page-id' },
});
expect(mockLink.mock.calls).toEqual([[{ pageId: 'page-id' }]]);
});
// const RealDate = Date;
// const mockDate = jest.fn(() => ({ date: 0 }));
// mockDate.now = jest.fn(() => 0);
test('Link with string pageId as params', () => {
Link({
methods,
params: 'page-id',
});
expect(mockLink.mock.calls).toEqual([[{ pageId: 'page-id' }]]);
});
// beforeEach(() => {
// mockActionMethod.mockReset();
// });
test('Link with string pageId as params', () => {
Link({
methods,
params: 'page-id',
});
expect(mockLink.mock.calls).toEqual([[{ pageId: 'page-id' }]]);
});
// beforeAll(() => {
// global.Date = mockDate;
// });
test('link method throws', () => {
mockLink.mockImplementationOnce(() => {
throw new Error('Test error');
});
// afterAll(() => {
// global.Date = RealDate;
// });
// test('action invocation', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'button',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'action',
// type: 'Link',
// params: 'call',
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(mockActionMethod.mock.calls).toEqual([[{ pageId: 'call' }]]);
// });
// test('error params action invocation', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'button',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'action',
// type: 'Link',
// params: 'error',
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// event: undefined,
// eventName: 'onClick',
// error: {
// action: {
// id: 'action',
// type: 'Link',
// params: 'error',
// },
// error: {
// error: new Error('Invalid Link, check action params. Received ""error"".'),
// index: 0,
// type: 'Link',
// },
// },
// responses: {
// action: {
// error: new Error('Invalid Link, check action params. Received ""error"".'),
// type: 'Link',
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: false,
// });
// });
expect(() =>
Link({
methods,
params: 'page-id',
})
).toThrow('Invalid Link, check action params. Received ""page-id"".');
});

View File

@ -14,15 +14,13 @@
limitations under the License.
*/
import { jest } from '@jest/globals';
import Login from './Login.js';
const mockActionMethod = jest.fn();
beforeEach(() => {
mockActionMethod.mockReset();
});
const mockLogin = jest.fn();
const methods = { login: mockLogin };
test('Login action invocation', async () => {
Login({ methods: { login: mockActionMethod }, params: 'params' });
expect(mockActionMethod.mock.calls).toEqual([['params']]);
Login({ methods, params: { providerId: 'provider' } });
expect(mockLogin.mock.calls).toEqual([[{ providerId: 'provider' }]]);
});

View File

@ -14,15 +14,13 @@
limitations under the License.
*/
import { jest } from '@jest/globals';
import Logout from './Logout.js';
const mockActionMethod = jest.fn();
beforeEach(() => {
mockActionMethod.mockReset();
});
const mockLogout = jest.fn();
const methods = { logout: mockLogout };
test('Logout action invocation', async () => {
Logout({ methods: { logout: mockActionMethod }, params: 'params' });
expect(mockActionMethod.mock.calls).toEqual([['params']]);
Logout({ methods, params: 'params' });
expect(mockLogout.mock.calls).toEqual([['params']]);
});

View File

@ -14,15 +14,13 @@
limitations under the License.
*/
import { jest } from '@jest/globals';
import Request from './Request.js';
const mockActionMethod = jest.fn();
beforeEach(() => {
mockActionMethod.mockReset();
});
const mockRequest = jest.fn();
const methods = { request: mockRequest };
test('Request action invocation', async () => {
Request({ methods: { request: mockActionMethod }, params: 'call' });
expect(mockActionMethod.mock.calls).toEqual([['call']]);
Request({ methods, params: 'requestId' });
expect(mockRequest.mock.calls).toEqual([['requestId']]);
});

View File

@ -14,15 +14,13 @@
limitations under the License.
*/
import { jest } from '@jest/globals';
import Reset from './Reset.js';
const mockActionMethod = jest.fn();
beforeEach(() => {
mockActionMethod.mockReset();
});
const mockReset = jest.fn();
const methods = { reset: mockReset };
test('Reset action invocation', async () => {
Reset({ methods: { reset: mockActionMethod } });
expect(mockActionMethod.mock.calls).toEqual([[]]);
Reset({ methods: methods });
expect(mockReset.mock.calls).toEqual([[]]);
});

View File

@ -14,15 +14,13 @@
limitations under the License.
*/
import { jest } from '@jest/globals';
import ResetValidation from './ResetValidation.js';
const mockActionMethod = jest.fn();
beforeEach(() => {
mockActionMethod.mockReset();
});
const mockResetValidation = jest.fn();
const methods = { resetValidation: mockResetValidation };
test('ResetValidation action invocation', async () => {
ResetValidation({ methods: { resetValidation: mockActionMethod }, params: 'call' });
expect(mockActionMethod.mock.calls).toEqual([['call']]);
ResetValidation({ methods, params: 'blockId' });
expect(mockResetValidation.mock.calls).toEqual([['blockId']]);
});

View File

@ -1,302 +1,81 @@
// /*
// Copyright 2020-2022 Lowdefy, Inc
/*
Copyright 2020-2022 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
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
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.
// */
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 testContext from './testContext.js';
import { jest } from '@jest/globals';
import ScrollTo from './ScrollTo.js';
// import ScrollTo from './ScrollTo.js';
// Mock document
const mockDocGetElementById = jest.fn();
const mockElemScrollIntoView = jest.fn();
const document = {
getElementById: mockDocGetElementById,
};
// // Mock document
// const mockDocGetElementById = jest.fn();
// const mockElemScrollIntoView = jest.fn();
// const document = {
// getElementById: mockDocGetElementById,
// };
// const mockDocGetElementByIdImp = (id) => {
// if (id === 'root') return { id, scrollIntoView: mockElemScrollIntoView };
// };
// Mock window
const mockWindowFocus = jest.fn();
const mockWindowOpen = jest.fn(() => ({ focus: mockWindowFocus }));
const mockWindowScrollTo = jest.fn();
const window = {
location: { href: '', origin: 'http://lowdefy.com' },
open: mockWindowOpen,
scrollTo: mockWindowScrollTo,
};
// // Mock window
// const mockWindowFocus = jest.fn();
// const mockWindowOpen = jest.fn(() => ({ focus: mockWindowFocus }));
// const mockWindowScrollTo = jest.fn();
// const window = {
// location: { href: '', origin: 'http://lowdefy.com' },
// open: mockWindowOpen,
// scrollTo: mockWindowScrollTo,
// };
test('ScrollTo with no params', async () => {
expect(() => ScrollTo({ document, window })).toThrow(
'Invalid ScrollTo, check action params. Received "undefined".'
);
});
// const lowdefy = {
// _internal: {
// actions: {
// ScrollTo,
// },
// blockComponents: {
// Button: { meta: { category: 'display' } },
// },
// document,
// window,
// },
// };
test('ScrollTo with no blockId', async () => {
ScrollTo({ document, window, params: { behavior: 'smooth', top: 0 } });
expect(mockWindowScrollTo.mock.calls).toEqual([
[
{
behavior: 'smooth',
top: 0,
},
],
]);
});
// // Comment out to use console
// console.log = () => {};
// console.error = () => {};
test('ScrollTo with blockId', async () => {
mockDocGetElementById.mockImplementation((id) => {
if (id === 'blockId') return { id, scrollIntoView: mockElemScrollIntoView };
});
ScrollTo({ document, window, params: { blockId: 'blockId' } });
expect(mockDocGetElementById.mock.calls).toEqual([['blockId']]);
expect(mockElemScrollIntoView.mock.calls).toEqual([[undefined]]);
});
// beforeEach(() => {
// mockWindowOpen.mockReset();
// mockWindowFocus.mockReset();
// mockWindowScrollTo.mockReset();
// mockDocGetElementById.mockReset();
// mockDocGetElementById.mockImplementation(mockDocGetElementByIdImp);
// mockElemScrollIntoView.mockReset();
// });
test('ScrollTo with blockId and options', async () => {
mockDocGetElementById.mockImplementation((id) => {
if (id === 'blockId') return { id, scrollIntoView: mockElemScrollIntoView };
});
ScrollTo({ document, window, params: { blockId: 'blockId', options: { behavior: 'smooth' } } });
expect(mockDocGetElementById.mock.calls).toEqual([['blockId']]);
expect(mockElemScrollIntoView.mock.calls).toEqual([[{ behavior: 'smooth' }]]);
});
// const RealDate = Date;
// const mockDate = jest.fn(() => ({ date: 0 }));
// mockDate.now = jest.fn(() => 0);
// beforeAll(() => {
// global.Date = mockDate;
// });
// afterAll(() => {
// global.Date = RealDate;
// });
// test('ScrollTo with no params', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// valueType: 'string',
// },
// events: {
// onClick: [{ id: 'a', type: 'ScrollTo' }],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// const res = await button.triggerEvent({ name: 'onClick' });
// expect(res).toEqual({
// blockId: 'button',
// bounced: false,
// endTimestamp: { date: 0 },
// error: {
// action: {
// id: 'a',
// type: 'ScrollTo',
// },
// error: {
// error: new Error('Invalid ScrollTo, check action params. Received "undefined".'),
// index: 0,
// type: 'ScrollTo',
// },
// },
// event: undefined,
// eventName: 'onClick',
// responses: {
// a: {
// error: new Error('Invalid ScrollTo, check action params. Received "undefined".'),
// index: 0,
// type: 'ScrollTo',
// },
// },
// startTimestamp: { date: 0 },
// success: false,
// });
// });
// test('ScrollTo with no blockId', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// valueType: 'string',
// },
// events: {
// onClick: [{ id: 'a', type: 'ScrollTo', params: { behavior: 'smooth', top: 0 } }],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// button.triggerEvent({ name: 'onClick' });
// expect(mockWindowScrollTo.mock.calls).toEqual([
// [
// {
// behavior: 'smooth',
// top: 0,
// },
// ],
// ]);
// });
// test('ScrollTo with blockId', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// valueType: 'string',
// },
// events: {
// onClick: [{ id: 'a', type: 'ScrollTo', params: { blockId: 'root' } }],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// button.triggerEvent({ name: 'onClick' });
// expect(mockDocGetElementById.mock.calls).toEqual([['root']]);
// expect(mockElemScrollIntoView.mock.calls).toEqual([[undefined]]);
// });
// test('ScrollTo with blockId and options', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// valueType: 'string',
// },
// events: {
// onClick: [
// {
// id: 'a',
// type: 'ScrollTo',
// params: { blockId: 'root', options: { behavior: 'smooth' } },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// button.triggerEvent({ name: 'onClick' });
// expect(mockDocGetElementById.mock.calls).toEqual([['root']]);
// expect(mockElemScrollIntoView.mock.calls).toEqual([
// [
// {
// behavior: 'smooth',
// },
// ],
// ]);
// });
// test('ScrollTo with blockId, block not found', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// valueType: 'string',
// },
// events: {
// onClick: [{ id: 'a', type: 'ScrollTo', params: { blockId: 'not_there' } }],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// button.triggerEvent({ name: 'onClick' });
// expect(mockDocGetElementById.mock.calls).toEqual([['not_there']]);
// expect(mockElemScrollIntoView.mock.calls).toEqual([]);
// expect(mockWindowScrollTo.mock.calls).toEqual([]);
// });
test('ScrollTo with blockId, block not found', async () => {
mockDocGetElementById.mockImplementation((id) => {
if (id === 'blockId') return { id, scrollIntoView: mockElemScrollIntoView };
});
ScrollTo({ document, window, params: { blockId: 'not_there' } });
expect(mockDocGetElementById.mock.calls).toEqual([['not_there']]);
expect(mockElemScrollIntoView.mock.calls).toEqual([]);
expect(mockWindowScrollTo.mock.calls).toEqual([]);
});

View File

@ -14,15 +14,13 @@
limitations under the License.
*/
import { jest } from '@jest/globals';
import SetGlobal from './SetGlobal.js';
const mockActionMethod = jest.fn();
beforeEach(() => {
mockActionMethod.mockReset();
});
const mockSetGlobal = jest.fn();
const methods = { setGlobal: mockSetGlobal };
test('SetGlobal action invocation', async () => {
SetGlobal({ methods: { setGlobal: mockActionMethod }, params: 'call' });
expect(mockActionMethod.mock.calls).toEqual([['call']]);
SetGlobal({ methods, params: { key: 'value' } });
expect(mockSetGlobal.mock.calls).toEqual([[{ key: 'value' }]]);
});

View File

@ -14,15 +14,13 @@
limitations under the License.
*/
import { jest } from '@jest/globals';
import SetState from './SetState.js';
const mockActionMethod = jest.fn();
beforeEach(() => {
mockActionMethod.mockReset();
});
const mockSetState = jest.fn();
const methods = { setState: mockSetState };
test('SetState action invocation', async () => {
SetState({ methods: { setState: mockActionMethod }, params: 'call' });
expect(mockActionMethod.mock.calls).toEqual([['call']]);
SetState({ methods, params: { key: 'value' } });
expect(mockSetState.mock.calls).toEqual([[{ key: 'value' }]]);
});

View File

@ -1,648 +1,99 @@
// /*
// Copyright 2020-2022 Lowdefy, Inc
/*
Copyright 2020-2022 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
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
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.
// */
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 testContext from './testContext.js';
// import { Throw, ThrowActionError } from './Throw.js';
import { Throw, ThrowActionError } from './Throw.js';
// const closeLoader = jest.fn();
// const displayMessage = jest.fn();
// const lowdefy = {
// _internal: {
// actions: {
// Throw,
// },
// blockComponents: {
// Button: { meta: { category: 'display' } },
// },
// displayMessage,
// },
// };
const methods = { getBlockId: () => 'blockId', getPageId: () => 'pageId' };
// const RealDate = Date;
// const mockDate = jest.fn(() => ({ date: 0 }));
// mockDate.now = jest.fn(() => 0);
test('Throw no params', () => {
expect(() => Throw({ methods })).toThrow(
'Throw action params should be an object. Received "undefined".'
);
});
// // Comment out to use console
// console.log = () => {};
// console.error = () => {};
test('Throw params.throw should be a boolean.', () => {
expect(() => Throw({ methods, params: { throw: 'invalid' } })).toThrow(
'Throw action "throw" param should be an boolean. Received ""invalid"".'
);
});
// beforeEach(() => {
// displayMessage.mockReset();
// closeLoader.mockReset();
// displayMessage.mockImplementation(() => closeLoader);
// });
test('Throw params.throw null', () => {
expect(() => Throw({ methods, params: { throw: null } })).not.toThrow();
});
// beforeAll(() => {
// global.Date = mockDate;
// });
test('Throw params.throw false', () => {
expect(() => Throw({ methods, params: { throw: false } })).not.toThrow();
});
// afterAll(() => {
// global.Date = RealDate;
// });
test('Throw params.throw true, no message or metaData', () => {
const params = { throw: true };
expect(() => Throw({ methods, params })).toThrow(ThrowActionError);
let error;
try {
Throw({ methods, params });
} catch (e) {
error = e;
}
expect(error.message).toEqual('');
expect(error.blockId).toEqual('blockId');
expect(error.metaData).toEqual(undefined);
expect(error.pageId).toEqual('pageId');
});
// test('Throw no params', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'throw',
// type: 'Throw',
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// event: undefined,
// eventName: 'onClick',
// error: {
// action: {
// id: 'throw',
// type: 'Throw',
// },
// error: {
// error: new TypeError('Throw action params should be an object. Received "undefined".'),
// index: 0,
// type: 'Throw',
// },
// },
// responses: {
// throw: {
// error: new TypeError('Throw action params should be an object. Received "undefined".'),
// type: 'Throw',
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: false,
// });
// expect(displayMessage.mock.calls).toMatchInlineSnapshot(`
// Array [
// Array [
// Object {
// "content": "Throw action params should be an object. Received \\"undefined\\".",
// "duration": 6,
// "status": "error",
// },
// ],
// ]
// `);
// });
test('Throw params.throw true, message and no metaData', () => {
const params = { throw: true, message: 'My error message' };
expect(() => Throw({ methods, params })).toThrow(ThrowActionError);
let error;
try {
Throw({ methods, params });
} catch (e) {
error = e;
}
expect(error.message).toEqual('My error message');
expect(error.blockId).toEqual('blockId');
expect(error.metaData).toEqual(undefined);
expect(error.pageId).toEqual('pageId');
});
// test('Throw params.throw true, no message or metaData', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'throw',
// type: 'Throw',
// params: { throw: true },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// error: {
// error: {
// type: 'Throw',
// error: new ThrowActionError(undefined, { blockId: 'button', context: context }),
// index: 0,
// },
// action: {
// id: 'throw',
// type: 'Throw',
// params: { throw: true },
// },
// },
// event: undefined,
// eventName: 'onClick',
// responses: {
// throw: {
// type: 'Throw',
// error: new ThrowActionError(undefined, { blockId: 'button', context: context }),
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: false,
// });
// expect(displayMessage.mock.calls).toMatchInlineSnapshot(`
// Array [
// Array [
// Object {
// "content": "",
// "duration": 6,
// "status": "error",
// },
// ],
// ]
// `);
// });
test('Throw params.throw true, message and metaData string', () => {
const params = { throw: true, message: 'My error message', metaData: 'Meta string' };
expect(() => Throw({ methods, params })).toThrow(ThrowActionError);
let error;
try {
Throw({ methods, params });
} catch (e) {
error = e;
}
expect(error.message).toEqual('My error message');
expect(error.blockId).toEqual('blockId');
expect(error.metaData).toEqual('Meta string');
expect(error.pageId).toEqual('pageId');
});
// test('Throw params.throw true, message and no metaData', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'throw',
// type: 'Throw',
// params: { throw: true, message: 'My error message' },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// error: {
// error: {
// type: 'Throw',
// error: new ThrowActionError('My error message', { blockId: 'button', context: context }),
// index: 0,
// },
// action: {
// id: 'throw',
// type: 'Throw',
// params: { throw: true, message: 'My error message' },
// },
// },
// event: undefined,
// eventName: 'onClick',
// responses: {
// throw: {
// type: 'Throw',
// error: new ThrowActionError('My error message', { blockId: 'button', context: context }),
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: false,
// });
// expect(displayMessage.mock.calls).toMatchInlineSnapshot(`
// Array [
// Array [
// Object {
// "content": "My error message",
// "duration": 6,
// "status": "error",
// },
// ],
// ]
// `);
// });
// test('Throw params.throw true, message and metaData string', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'throw',
// type: 'Throw',
// params: { throw: true, message: 'My error message', metaData: 'Meta string' },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// error: {
// error: {
// type: 'Throw',
// error: new ThrowActionError('My error message', {
// blockId: 'button',
// context: context,
// metaData: 'Meta string',
// }),
// index: 0,
// },
// action: {
// id: 'throw',
// type: 'Throw',
// params: { throw: true, message: 'My error message', metaData: 'Meta string' },
// },
// },
// event: undefined,
// eventName: 'onClick',
// responses: {
// throw: {
// type: 'Throw',
// error: new ThrowActionError('My error message', {
// blockId: 'button',
// context: context,
// metaData: 'Meta string',
// }),
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: false,
// });
// expect(displayMessage.mock.calls).toMatchInlineSnapshot(`
// Array [
// Array [
// Object {
// "content": "My error message",
// "duration": 6,
// "status": "error",
// },
// ],
// ]
// `);
// });
// test('Throw params.throw true, message and metaData object', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'throw',
// type: 'Throw',
// params: { throw: true, message: 'My error message', metaData: { key: 'value' } },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// error: {
// error: {
// type: 'Throw',
// error: new ThrowActionError('My error message', {
// blockId: 'button',
// context: context,
// metaData: { key: 'value' },
// }),
// index: 0,
// },
// action: {
// id: 'throw',
// type: 'Throw',
// params: { throw: true, message: 'My error message', metaData: { key: 'value' } },
// },
// },
// event: undefined,
// eventName: 'onClick',
// responses: {
// throw: {
// type: 'Throw',
// error: new ThrowActionError('My error message', {
// blockId: 'button',
// context: context,
// metaData: 'Meta string',
// }),
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: false,
// });
// expect(displayMessage.mock.calls).toMatchInlineSnapshot(`
// Array [
// Array [
// Object {
// "content": "My error message",
// "duration": 6,
// "status": "error",
// },
// ],
// ]
// `);
// });
// test('Throw params.throw false', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'throw',
// type: 'Throw',
// params: { throw: false },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// event: undefined,
// eventName: 'onClick',
// responses: {
// throw: {
// type: 'Throw',
// response: undefined,
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: true,
// });
// expect(displayMessage.mock.calls).toMatchInlineSnapshot(`Array []`);
// });
// test('Throw params.throw null', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'throw',
// type: 'Throw',
// params: { throw: null },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// event: undefined,
// eventName: 'onClick',
// responses: {
// throw: {
// type: 'Throw',
// response: undefined,
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: true,
// });
// expect(displayMessage.mock.calls).toMatchInlineSnapshot(`Array []`);
// });
// test('Throw params.throw should be a boolean.', async () => {
// const rootBlock = {
// id: 'block:root:root:0',
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'block:root:button:0',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// },
// events: {
// onClick: [
// {
// id: 'throw',
// type: 'Throw',
// params: { throw: 'invalid' },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// await button.triggerEvent({ name: 'onClick' });
// expect(button.Events.events.onClick.history[0]).toEqual({
// blockId: 'button',
// bounced: false,
// error: {
// error: {
// type: 'Throw',
// error: new Error('Throw action "throw" param should be an boolean. Received ""invalid"".'),
// index: 0,
// },
// action: {
// id: 'throw',
// type: 'Throw',
// params: { throw: 'invalid' },
// },
// },
// event: undefined,
// eventName: 'onClick',
// responses: {
// throw: {
// type: 'Throw',
// error: new Error('Throw action "throw" param should be an boolean. Received ""invalid"".'),
// index: 0,
// },
// },
// endTimestamp: { date: 0 },
// startTimestamp: { date: 0 },
// success: false,
// });
// expect(displayMessage.mock.calls).toMatchInlineSnapshot(`
// Array [
// Array [
// Object {
// "content": "Throw action \\"throw\\" param should be an boolean. Received \\"\\"invalid\\"\\".",
// "duration": 6,
// "status": "error",
// },
// ],
// ]
// `);
// });
test('Throw params.throw true, message and metaData object', () => {
const params = { throw: true, message: 'My error message', metaData: { key: 'value' } };
expect(() => Throw({ methods, params })).toThrow(ThrowActionError);
let error;
try {
Throw({ methods, params });
} catch (e) {
error = e;
}
expect(error.message).toEqual('My error message');
expect(error.blockId).toEqual('blockId');
expect(error.metaData).toEqual({ key: 'value' });
expect(error.pageId).toEqual('pageId');
});

View File

@ -14,15 +14,13 @@
limitations under the License.
*/
import { jest } from '@jest/globals';
import Validate from './Validate.js';
const mockActionMethod = jest.fn();
beforeEach(() => {
mockActionMethod.mockReset();
});
const mockValidate = jest.fn();
const methods = { validate: mockValidate };
test('Validate action invocation', async () => {
Validate({ methods: { validate: mockActionMethod }, params: 'call' });
expect(mockActionMethod.mock.calls).toEqual([['call']]);
Validate({ methods, params: 'blockId' });
expect(mockValidate.mock.calls).toEqual([['blockId']]);
});

View File

@ -17,7 +17,7 @@
import { type, wait } from '@lowdefy/helpers';
function Wait({ params }) {
if (!type.isInt(params.ms)) {
if (type.isNone(params) || !type.isInt(params.ms)) {
throw new Error(`Wait action "ms" param should be an integer.`);
}
return wait(params.ms);

View File

@ -1,166 +1,44 @@
// /*
// Copyright 2020-2022 Lowdefy, Inc
/*
Copyright 2020-2022 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
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
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.
// */
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 testContext from './testContext.js';
import { wait } from '@lowdefy/helpers';
import Wait from './Wait.js';
// import Wait from './Wait.js';
test('wait set ms before continuing', async () => {
let flag = false;
// const lowdefy = {
// _internal: {
// actions: {
// Wait,
// },
// blockComponents: {
// Button: { meta: { category: 'display' } },
// },
// auth: {
// login: jest.fn(),
// },
// },
// };
const waitAndSetFlag = async () => {
await Wait({ params: { ms: 10 } });
flag = true;
};
expect(flag).toBe(false);
waitAndSetFlag();
expect(flag).toBe(false);
await wait(5);
expect(flag).toBe(false);
await wait(6);
expect(flag).toBe(true);
});
// const RealDate = Date;
// const mockDate = jest.fn(() => ({ date: 0 }));
// mockDate.now = jest.fn(() => 0);
test('Wait params undefined', async () => {
expect(() => Wait({})).toThrow('Wait action "ms" param should be an integer.');
});
// // Comment out to use console
// console.log = () => {};
// console.error = () => {};
// beforeEach(() => {
// global.Date = mockDate;
// lowdefy._internal.auth.login.mockReset();
// });
// afterAll(() => {
// global.Date = RealDate;
// });
// const timeout = (ms) => {
// return new Promise((resolve) => setTimeout(resolve, ms));
// };
// test('Wait', async () => {
// const rootBlock = {
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'button',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// valueType: 'string',
// },
// events: {
// onClick: [
// {
// id: 'a',
// type: 'Wait',
// params: { ms: 500 },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// let resolved = false;
// button.triggerEvent({ name: 'onClick' }).then(() => {
// resolved = true;
// });
// expect(resolved).toBe(false);
// await timeout(100);
// expect(resolved).toBe(false);
// await timeout(300);
// expect(resolved).toBe(false);
// await timeout(150);
// expect(resolved).toBe(true);
// });
// test('Wait ms not a integer', async () => {
// const rootBlock = {
// blockId: 'root',
// meta: {
// category: 'container',
// },
// areas: {
// content: {
// blocks: [
// {
// id: 'button',
// blockId: 'button',
// type: 'Button',
// meta: {
// category: 'display',
// valueType: 'string',
// },
// events: {
// onClick: [
// {
// id: 'a',
// type: 'Wait',
// params: { ms: 1.1 },
// },
// ],
// },
// },
// ],
// },
// },
// };
// const context = testContext({
// lowdefy,
// rootBlock,
// });
// const button = context._internal.RootBlocks.map['button'];
// const res = await button.triggerEvent({ name: 'onClick' });
// expect(res).toEqual({
// blockId: 'button',
// bounced: false,
// endTimestamp: { date: 0 },
// error: {
// action: { id: 'a', params: { ms: 1.1 }, type: 'Wait' },
// error: {
// error: new Error('Wait action "ms" param should be an integer.'),
// index: 0,
// type: 'Wait',
// },
// },
// event: undefined,
// eventName: 'onClick',
// responses: {
// a: {
// error: new Error('Wait action "ms" param should be an integer.'),
// index: 0,
// type: 'Wait',
// },
// },
// startTimestamp: { date: 0 },
// success: false,
// });
// });
test('Wait params.ms not an integer', async () => {
expect(() => Wait({ params: { key: 'value' } })).toThrow(
'Wait action "ms" param should be an integer.'
);
});

View File

@ -16,7 +16,7 @@
import wait from './wait.js';
test('setNestedValue - set a nested value in array object', async () => {
test('wait set ms before continuing', async () => {
let flag = false;
const waitAndSetFlag = async () => {

View File

@ -2186,8 +2186,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@lowdefy/actions-core@workspace:packages/plugins/actions/actions-core"
dependencies:
"@jest/globals": 28.1.0
"@lowdefy/helpers": 4.0.0-alpha.12
"@lowdefy/operators": 4.0.0-alpha.12
"@swc/cli": 0.1.57
"@swc/core": 1.2.192
"@swc/jest": 0.2.21