fix(operators-js): Fix getter operator tests.

This commit is contained in:
Sam 2022-02-07 14:18:52 +02:00
parent aa1d72c191
commit c774f3f31f
No known key found for this signature in database
GPG Key ID: D004126FCD1A6DF0
17 changed files with 194 additions and 508 deletions

View File

@ -3,11 +3,11 @@ export default {
collectCoverage: true,
collectCoverageFrom: ['src/**/*.js'],
coverageDirectory: 'coverage',
coveragePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/test/', '<rootDir>/dist/index.js'],
coveragePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/test/', '<rootDir>/src/index.js'],
coverageReporters: [['lcov', { projectRoot: '../..' }], 'text', 'clover'],
errorOnDeprecated: true,
testEnvironment: 'jsdom',
testPathIgnorePatterns: ['<rootDir>/dist/'],
testPathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/src/index.js'],
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest', { configFile: '../../.swcrc.test' }],
},

View File

@ -15,17 +15,18 @@
*/
import _index from './_index.js';
jest.mock('@lowdefy/operators');
const input = {
arrayIndices: [0],
location: 'location',
params: 'params',
};
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
test('args calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
_index(input);
test('_index calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
_index({
arrayIndices: [0],
location: 'location',
params: 'params',
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{

View File

@ -15,22 +15,23 @@
*/
import actions from './actions.js';
jest.mock('@lowdefy/operators');
const input = {
actions: {
action_id: {
response: 'returned from action',
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
test('actions calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
actions({
actions: {
action_id: {
response: 'returned from action',
},
},
},
arrayIndices: [0],
location: 'location',
params: 'params',
};
test('actions calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
actions(input);
arrayIndices: [0],
location: 'location',
params: 'params',
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{

View File

@ -15,18 +15,19 @@
*/
import event from './event.js';
jest.mock('@lowdefy/operators');
const input = {
arrayIndices: [0],
event: { event: true },
location: 'location',
params: 'params',
};
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
test('event calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
event(input);
test('event calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
event({
arrayIndices: [0],
event: { event: true },
location: 'location',
params: 'params',
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{

View File

@ -14,226 +14,29 @@
limitations under the License.
*/
import { WebParser } from '@lowdefy/operators';
import event_log from './event_log.js';
const arrayIndices = [1];
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
const context = {
_internal: {
lowdefy: {
inputs: { id: true },
lowdefyGlobal: { global: true },
menus: [{ menus: true }],
urlQuery: { urlQuery: true },
user: { user: true },
},
},
eventLog: [{ eventLog: true }],
id: 'id',
requests: [{ requests: true }],
state: { state: true },
};
console.error = () => {};
test('_event_log in array', async () => {
const input = { a: { _event_log: '1.blockId' } };
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual({
a: 'block_b',
test('request_details calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
event_log({
eventLog: [{ eventName: 'test' }],
arrayIndices: [0],
location: 'location',
params: 'params',
});
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_event_log full state', async () => {
const input = { _event_log: true };
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual([
{
blockId: 'block_a',
actionName: 'name_a',
response: [{ data: ['a', 'b'] }],
ts: new Date(0),
status: 'success',
},
{
blockId: 'block_b',
actionName: 'name_b',
ts: new Date(1),
error: [{ error: 'error', message: 'broken', name: 'e' }],
},
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{
arrayIndices: [0],
location: 'location',
object: [{ eventName: 'test' }],
operator: '_event_log',
params: 'params',
},
],
]);
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_event_log null', async () => {
const input = { _event_log: null };
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toBe(null);
expect(res.errors).toMatchInlineSnapshot(`
Array [
[Error: Operator Error: _event_log params must be of type string, integer, boolean or object. Received: null at locationId.],
]
`);
});
test('_event_log param object key', async () => {
const input = {
_event_log: {
key: '0.actionName',
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual('name_a');
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_event_log param object all', async () => {
const input = {
_event_log: {
all: true,
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual([
{
blockId: 'block_a',
actionName: 'name_a',
response: [{ data: ['a', 'b'] }],
ts: new Date(0),
status: 'success',
},
{
blockId: 'block_b',
actionName: 'name_b',
ts: new Date(1),
error: [{ error: 'error', message: 'broken', name: 'e' }],
},
]);
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_event_log param object all and key', async () => {
const input = {
_event_log: {
all: true,
key: 'string',
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual([
{
blockId: 'block_a',
actionName: 'name_a',
response: [{ data: ['a', 'b'] }],
ts: new Date(0),
status: 'success',
},
{
blockId: 'block_b',
actionName: 'name_b',
ts: new Date(1),
error: [{ error: 'error', message: 'broken', name: 'e' }],
},
]);
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_event_log param object invalid', async () => {
const input = {
_event_log: {
other: true,
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual(null);
expect(res.errors).toMatchInlineSnapshot(`
Array [
[Error: Operator Error: _event_log.key must be of type string or integer. Received: {"other":true} at locationId.],
]
`);
});
test('_event_log param array', async () => {
const input = {
_event_log: ['string'],
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual(null);
expect(res.errors).toMatchInlineSnapshot(`
Array [
[Error: Operator Error: _event_log params must be of type string, integer, boolean or object. Received: ["string"] at locationId.],
]
`);
});
test('_event_log param object with string default', async () => {
const input = {
_event_log: {
key: 'notFound',
default: 'defaultValue',
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual('defaultValue');
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_event_log param object with zero default', async () => {
const input = {
_event_log: {
key: 'notFound',
default: 0,
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual(0);
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_event_log param object with false default', async () => {
const input = {
_event_log: {
key: 'notFound',
default: false,
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual(false);
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_event_log param object with no default', async () => {
const input = {
_event_log: {
key: 'notFound',
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual(null);
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});

View File

@ -13,26 +13,20 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import _global from './global.js';
jest.mock('@lowdefy/operators');
let lowdefyOperators;
let global;
const input = {
arrayIndices: [0],
location: 'location',
lowdefyGlobal: { lowdefyGlobal: true },
params: 'params',
};
beforeEach(async () => {
lowdefyOperators = await import('@lowdefy/operators');
global = (await import('./global.js')).default;
});
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
test('global calls getFromObject', async () => {
global(input);
const lowdefyOperators = await import('@lowdefy/operators');
_global({
arrayIndices: [0],
location: 'location',
params: 'params',
lowdefyGlobal: { lowdefyGlobal: true },
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{

View File

@ -15,18 +15,19 @@
*/
import input from './input.js';
jest.mock('@lowdefy/operators');
const inputParams = {
arrayIndices: [0],
input: { input: true },
location: 'location',
params: 'params',
};
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
test('input calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
input(inputParams);
test('input calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
input({
arrayIndices: [0],
input: { input: true },
location: 'location',
params: 'params',
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{

View File

@ -31,6 +31,7 @@ const validProperties = [
'hash',
];
// TODO: Fix with new router and link
function _location({ arrayIndices, context, location, params }) {
if (!window || !window.location) {
throw new Error(

View File

@ -15,7 +15,10 @@
*/
import location from './location.js';
jest.mock('@lowdefy/operators');
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
beforeEach(() => {
Object.defineProperty(window, 'location', {
@ -43,7 +46,7 @@ const input = {
params: 'origin',
};
test('location calls getFromObject', () => {
test('location calls getFromObject', async () => {
const lowdefyOperators = import('@lowdefy/operators');
location(input);
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([

View File

@ -14,182 +14,41 @@
limitations under the License.
*/
/* eslint-disable max-classes-per-file */
import { WebParser } from '@lowdefy/operators';
import request_details from './request_details.js';
const arrayIndices = [1];
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
const context = {
_internal: {
lowdefy: {
inputs: { id: true },
lowdefyGlobal: { global: true },
menus: [{ menus: true }],
urlQuery: { urlQuery: true },
user: { user: true },
test('request_details calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
request_details({
requests: {
request_id: {
response: 'returned from action',
loading: false,
error: [],
},
},
},
eventLog: [{ eventLog: true }],
id: 'id',
requests: [{ requests: true }],
state: { state: true },
};
console.error = () => {};
test('_request_details in object', async () => {
const input = { _request_details: 'string' };
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual({ loading: false, response: 'request String' });
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_request_details all requests', async () => {
const input = { _request_details: true };
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual({
not_loaded: { loading: true, response: 'fail' },
string: { loading: false, response: 'request String' },
number: { loading: false, response: 500 },
arr: { loading: false, response: [{ a: 'request a1' }, { a: 'request a2' }] },
returnsNull: { loading: false, response: null },
arrayIndices: [0],
location: 'location',
params: 'params',
});
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_request_details null', async () => {
const input = { _request_details: null };
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toBe(null);
expect(res.errors).toMatchInlineSnapshot(`
Array [
[Error: Operator Error: _request_details params must be of type string, integer, boolean or object. Received: null at locationId.],
]
`);
});
test('_request_details nested', async () => {
const input = { _request_details: 'string.response' };
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual('request String');
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_request_details param object key', async () => {
const input = {
_request_details: {
key: 'string',
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual({ loading: false, response: 'request String' });
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_request_details param object all', async () => {
const input = {
_request_details: {
all: true,
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual({
not_loaded: { loading: true, response: 'fail' },
string: { loading: false, response: 'request String' },
number: { loading: false, response: 500 },
arr: { loading: false, response: [{ a: 'request a1' }, { a: 'request a2' }] },
returnsNull: { loading: false, response: null },
});
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_request_details param object all and key', async () => {
const input = {
_request_details: {
all: true,
key: 'string',
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual({
not_loaded: { loading: true, response: 'fail' },
string: { loading: false, response: 'request String' },
number: { loading: false, response: 500 },
arr: { loading: false, response: [{ a: 'request a1' }, { a: 'request a2' }] },
returnsNull: { loading: false, response: null },
});
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_request_details param object invalid', async () => {
const input = {
_request_details: {
other: true,
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual(null);
expect(res.errors).toMatchInlineSnapshot(`
Array [
[Error: Operator Error: _request_details.key must be of type string or integer. Received: {"other":true} at locationId.],
]
`);
});
test('_request_details param array', async () => {
const input = {
_request_details: ['string'],
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual(null);
expect(res.errors).toMatchInlineSnapshot(`
Array [
[Error: Operator Error: _request_details params must be of type string, integer, boolean or object. Received: ["string"] at locationId.],
]
`);
});
test('_request_details param object with string default', async () => {
const input = {
_request_details: {
key: 'notFound',
default: 'defaultValue',
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual('defaultValue');
expect(res.errors).toMatchInlineSnapshot(`Array []`);
});
test('_request_details param object with no default', async () => {
const input = {
_request_details: {
key: 'notFound',
},
};
const parser = new WebParser({ context });
await parser.init();
const res = parser.parse({ input, location: 'locationId', arrayIndices });
expect(res.output).toEqual(null);
expect(res.errors).toMatchInlineSnapshot(`Array []`);
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{
arrayIndices: [0],
location: 'location',
object: {
request_id: {
response: 'returned from action',
loading: false,
error: [],
},
},
operator: '_request_details',
params: 'params',
},
],
]);
});

View File

@ -15,18 +15,19 @@
*/
import state from './state.js';
jest.mock('@lowdefy/operators');
const input = {
arrayIndices: [0],
location: 'location',
params: 'params',
state: { state: true },
};
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
test('state calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
state(input);
test('state calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
state({
arrayIndices: [0],
location: 'location',
params: 'params',
state: { state: true },
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{

View File

@ -15,19 +15,21 @@
*/
import url_query from './url_query.js';
jest.mock('@lowdefy/operators');
const input = {
arrayIndices: [0],
location: 'location',
params: 'params',
secrets: { secrets: true },
urlQuery: { urlQuery: true },
};
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
test('url_query calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
url_query(input);
const input = {};
test('url_query calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
url_query({
arrayIndices: [0],
location: 'location',
params: 'params',
urlQuery: { urlQuery: true },
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{

View File

@ -15,17 +15,18 @@
*/
import payload from './payload.js';
jest.mock('@lowdefy/operators');
const input = {
location: 'location',
params: 'params',
payload: { payload: true },
};
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
test('payload calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
payload(input);
test('payload calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
payload({
location: 'location',
params: 'params',
payload: { payload: true },
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{

View File

@ -15,12 +15,15 @@
*/
import secret from './secret.js';
jest.mock('@lowdefy/operators');
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
console.error = () => {};
test('secret calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
test('secret calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
secret({
arrayIndices: [0],
location: 'location',
@ -41,8 +44,8 @@ test('secret calls getFromObject', () => {
]);
});
test('secret default value', () => {
const lowdefyOperators = import('@lowdefy/operators');
test('secret default value', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
secret({
arrayIndices: [0],
location: 'location',
@ -72,8 +75,8 @@ test('secret get all is not allowed', () => {
);
});
test('secret OpenID Connect and JSON web token secrets are filtered out', () => {
const lowdefyOperators = import('@lowdefy/operators');
test('secret OpenID Connect and JSON web token secrets are filtered out', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
secret({
arrayIndices: [0],
location: 'location',

View File

@ -13,7 +13,11 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
jest.mock('@lowdefy/operators');
import args from './args.js';
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
const input = {
args: [{ args: true }],
@ -23,9 +27,13 @@ const input = {
};
test('args calls getFromObject', async () => {
const args = await import('./args.js');
const lowdefyOperators = await import('@lowdefy/operators');
args.default(input);
args({
args: [{ args: true }],
arrayIndices: [0],
location: 'location',
params: 'params',
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{

View File

@ -14,19 +14,21 @@
limitations under the License.
*/
import get from './get.js';
jest.mock('@lowdefy/operators');
test('_get calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
const input = {
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
test('_get calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
get({
arrayIndices: [0],
location: 'location',
params: {
from: { a: 1 },
key: 'a',
},
};
get.default(input);
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{
@ -52,7 +54,6 @@ test('_get returns null if from is null', () => {
key: 'a',
},
};
get(input);
expect(get(input)).toBe(null);
});
@ -66,7 +67,6 @@ test('_get returns default value if from is null', () => {
default: 'default',
},
};
get(input);
expect(get(input)).toBe('default');
});

View File

@ -15,7 +15,9 @@
*/
import user from './user.js';
jest.mock('@lowdefy/operators');
jest.mock('@lowdefy/operators', () => ({
getFromObject: jest.fn(),
}));
const input = {
arrayIndices: [0],
@ -24,9 +26,14 @@ const input = {
user: { name: 'first name' },
};
test('user calls getFromObject', () => {
const lowdefyOperators = import('@lowdefy/operators');
user(input);
test('user calls getFromObject', async () => {
const lowdefyOperators = await import('@lowdefy/operators');
user({
arrayIndices: [0],
location: 'location',
params: 'params',
user: { name: 'first name' },
});
expect(lowdefyOperators.getFromObject.mock.calls).toEqual([
[
{