mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-03-13 14:56:54 +08:00
feat(operators): Remove init so parser is now sync.
This commit is contained in:
parent
c3c35d1642
commit
aabedad46c
@ -25,16 +25,6 @@ class NodeParser {
|
||||
this.parse = this.parse.bind(this);
|
||||
}
|
||||
|
||||
async init() {
|
||||
await Promise.all(
|
||||
Object.values(this.operators).map(async (operator) => {
|
||||
if (operator.init) {
|
||||
await operator.init();
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
parse({ args, input, location }) {
|
||||
const operators = this.operators;
|
||||
const secrets = this.secrets;
|
||||
|
@ -45,36 +45,32 @@ const user = {
|
||||
user: true,
|
||||
};
|
||||
|
||||
test('parse input undefined', async () => {
|
||||
test('parse input undefined', () => {
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
const res = parser.parse({});
|
||||
expect(res.output).toEqual();
|
||||
expect(res.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('parse args not array', async () => {
|
||||
test('parse args not array', () => {
|
||||
const input = {};
|
||||
const args = 'not an array';
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
expect(() => parser.parse({ args, input })).toThrow('Operator parser args must be an array.');
|
||||
});
|
||||
|
||||
test('parse location not string', async () => {
|
||||
test('parse location not string', () => {
|
||||
const input = {};
|
||||
const location = [];
|
||||
const parser = new NodeParser({ operators, payload, secrets, user });
|
||||
await parser.init();
|
||||
expect(() => parser.parse({ args, input, location })).toThrow(
|
||||
'Operator parser location must be a string.'
|
||||
);
|
||||
});
|
||||
|
||||
test('operator returns value', async () => {
|
||||
test('operator returns value', () => {
|
||||
const input = { a: { _test: { params: true } } };
|
||||
const parser = new NodeParser({ operators, payload, secrets, user });
|
||||
await parser.init();
|
||||
const res = parser.parse({ args, input, location });
|
||||
expect(res.output).toEqual({ a: 'test' });
|
||||
expect(operators._test.mock.calls).toMatchInlineSnapshot(`
|
||||
@ -147,35 +143,26 @@ test('operator returns value', async () => {
|
||||
expect(res.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('operator should be object with 1 key', async () => {
|
||||
test('operator should be object with 1 key', () => {
|
||||
const input = { a: { _test: { params: true }, x: 1 } };
|
||||
const parser = new NodeParser({ operators, payload, secrets, user });
|
||||
await parser.init();
|
||||
const res = parser.parse({ args, input, location });
|
||||
expect(res.output).toEqual(input);
|
||||
expect(res.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('undefined operator', async () => {
|
||||
test('undefined operator', () => {
|
||||
const input = { a: { _id: { params: true } } };
|
||||
const parser = new NodeParser({ operators, payload, secrets, user });
|
||||
await parser.init();
|
||||
const res = parser.parse({ args, input, location });
|
||||
expect(res.output).toEqual(input);
|
||||
expect(res.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('operator errors', async () => {
|
||||
test('operator errors', () => {
|
||||
const input = { a: { _error: { params: true } } };
|
||||
const parser = new NodeParser({ operators, payload, secrets, user });
|
||||
await parser.init();
|
||||
const res = parser.parse({ args, input, location });
|
||||
expect(res.output).toEqual({ a: null });
|
||||
expect(res.errors).toEqual([new Error('Test error.')]);
|
||||
});
|
||||
|
||||
test('operator init', async () => {
|
||||
const parser = new NodeParser({ operators, payload, secrets, user });
|
||||
await parser.init();
|
||||
expect(operators._init.init).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
@ -19,24 +19,10 @@ import { applyArrayIndices, serializer, type } from '@lowdefy/helpers';
|
||||
class WebParser {
|
||||
constructor({ context, operators }) {
|
||||
this.context = context;
|
||||
this.init = this.init.bind(this);
|
||||
this.parse = this.parse.bind(this);
|
||||
this.operators = operators;
|
||||
}
|
||||
|
||||
async init() {
|
||||
if (!type.isObject(this.context._internal.lowdefy)) {
|
||||
throw new Error('context._internal.lowdefy must be an object.');
|
||||
}
|
||||
await Promise.all(
|
||||
Object.values(this.operators).map(async (operator) => {
|
||||
if (operator.init) {
|
||||
await operator.init();
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
parse({ actions, args, arrayIndices, event, input, location }) {
|
||||
const operators = this.operators;
|
||||
const context = this.context;
|
||||
|
@ -55,59 +55,40 @@ const operators = {
|
||||
|
||||
operators._init.init = jest.fn();
|
||||
|
||||
test('parse input undefined', async () => {
|
||||
test('parse input undefined', () => {
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({});
|
||||
expect(res.output).toEqual();
|
||||
expect(res.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('context._internal.lowdefy not object', async () => {
|
||||
const context = {
|
||||
_internal: {
|
||||
lowdefy: 'not object',
|
||||
},
|
||||
eventLog: [{ eventLog: true }],
|
||||
id: 'id',
|
||||
requests: [{ requests: true }],
|
||||
state: { state: true },
|
||||
};
|
||||
const parser = new WebParser({ context, operators });
|
||||
await expect(() => parser.init()).rejects.toThrow('context._internal.lowdefy must be an object.');
|
||||
});
|
||||
|
||||
test('parse args not array', async () => {
|
||||
test('parse args not array', () => {
|
||||
const input = {};
|
||||
const args = 'not an array';
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
expect(() => parser.parse({ args, input })).toThrow('Operator parser args must be an array.');
|
||||
});
|
||||
|
||||
test('parse event not object', async () => {
|
||||
test('parse event not object', () => {
|
||||
const input = {};
|
||||
const event = 'not an array';
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
expect(() => parser.parse({ event, input })).toThrow('Operator parser event must be a object.');
|
||||
});
|
||||
|
||||
test('parse location not string', async () => {
|
||||
test('parse location not string', () => {
|
||||
const input = {};
|
||||
const location = [];
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
expect(() => parser.parse({ args, input, location })).toThrow(
|
||||
'Operator parser location must be a string.'
|
||||
);
|
||||
});
|
||||
|
||||
test('operator returns value', async () => {
|
||||
test('operator returns value', () => {
|
||||
const input = { a: { _test: { params: true } } };
|
||||
const location = 'location.$';
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ actions, args, arrayIndices, event, input, location });
|
||||
expect(res.output).toEqual({ a: 'test' });
|
||||
expect(operators._test.mock.calls).toMatchInlineSnapshot(`
|
||||
@ -238,7 +219,6 @@ test('operator returns value', async () => {
|
||||
"state": true,
|
||||
},
|
||||
},
|
||||
"init": [Function],
|
||||
"operators": Object {
|
||||
"_error": [MockFunction],
|
||||
"_init": [MockFunction],
|
||||
@ -275,7 +255,7 @@ test('operator returns value', async () => {
|
||||
expect(res.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('context._internal.lowdefy empty object', async () => {
|
||||
test('context._internal.lowdefy empty object', () => {
|
||||
const context = {
|
||||
_internal: {
|
||||
lowdefy: {},
|
||||
@ -287,7 +267,6 @@ test('context._internal.lowdefy empty object', async () => {
|
||||
};
|
||||
const input = { a: { _test: { params: true } } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ actions, args, arrayIndices, event, input, location });
|
||||
expect(res.output).toEqual({ a: 'test' });
|
||||
expect(operators._test.mock.calls).toMatchInlineSnapshot(`
|
||||
@ -376,7 +355,6 @@ test('context._internal.lowdefy empty object', async () => {
|
||||
"state": true,
|
||||
},
|
||||
},
|
||||
"init": [Function],
|
||||
"operators": Object {
|
||||
"_error": [MockFunction],
|
||||
"_init": [MockFunction],
|
||||
@ -409,35 +387,26 @@ test('context._internal.lowdefy empty object', async () => {
|
||||
expect(res.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('operator should be object with 1 key', async () => {
|
||||
test('operator should be object with 1 key', () => {
|
||||
const input = { a: { _test: { params: true }, x: 1 } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ actions, args, arrayIndices, event, input, location });
|
||||
expect(res.output).toEqual(input);
|
||||
expect(res.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('undefined operator', async () => {
|
||||
test('undefined operator', () => {
|
||||
const input = { a: { _id: { params: true } } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ actions, args, arrayIndices, event, input, location });
|
||||
expect(res.output).toEqual(input);
|
||||
expect(res.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('operator errors', async () => {
|
||||
test('operator errors', () => {
|
||||
const input = { a: { _error: { params: true } } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ actions, args, arrayIndices, event, input, location });
|
||||
expect(res.output).toEqual({ a: null });
|
||||
expect(res.errors).toEqual([new Error('Test error.')]);
|
||||
});
|
||||
|
||||
test('operator init', async () => {
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
expect(operators._init.init).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
@ -52,10 +52,9 @@ const operators = {
|
||||
|
||||
console.error = () => {};
|
||||
|
||||
test('_menu using string menuId', async () => {
|
||||
test('_menu using string menuId', () => {
|
||||
const input = { a: { _menu: 'default' } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual({
|
||||
a: {
|
||||
@ -65,10 +64,9 @@ test('_menu using string menuId', async () => {
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_menu using index', async () => {
|
||||
test('_menu using index', () => {
|
||||
const input = { a: { _menu: 1 } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual({
|
||||
a: {
|
||||
@ -78,10 +76,9 @@ test('_menu using index', async () => {
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_menu in object', async () => {
|
||||
test('_menu in object', () => {
|
||||
const input = { a: { _menu: 'default' } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual({
|
||||
a: {
|
||||
@ -91,10 +88,9 @@ test('_menu in object', async () => {
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_menu full menus', async () => {
|
||||
test('_menu full menus', () => {
|
||||
const input = { _menu: true };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual([
|
||||
{
|
||||
@ -110,10 +106,9 @@ test('_menu full menus', async () => {
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_menu null', async () => {
|
||||
test('_menu null', () => {
|
||||
const input = { _menu: null };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toBe(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -123,40 +118,37 @@ test('_menu null', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_menu param object value', async () => {
|
||||
test('_menu param object value', () => {
|
||||
const input = {
|
||||
_menu: {
|
||||
value: 'm_2',
|
||||
},
|
||||
};
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual({ menuId: 'm_2' });
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_menu param object index', async () => {
|
||||
test('_menu param object index', () => {
|
||||
const input = {
|
||||
_menu: {
|
||||
index: 2,
|
||||
},
|
||||
};
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual({ menuId: 'm_2' });
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_menu params object value not string', async () => {
|
||||
test('_menu params object value not string', () => {
|
||||
const input = {
|
||||
_menu: {
|
||||
value: 1,
|
||||
},
|
||||
};
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toBe(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -166,14 +158,13 @@ test('_menu params object value not string', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_menu params object index not number', async () => {
|
||||
test('_menu params object index not number', () => {
|
||||
const input = {
|
||||
_menu: {
|
||||
index: 'a',
|
||||
},
|
||||
};
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toBe(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -183,14 +174,13 @@ test('_menu params object index not number', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_menu param object all', async () => {
|
||||
test('_menu param object all', () => {
|
||||
const input = {
|
||||
_menu: {
|
||||
all: true,
|
||||
},
|
||||
};
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual([
|
||||
{
|
||||
@ -206,7 +196,7 @@ test('_menu param object all', async () => {
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_menu param object all and value', async () => {
|
||||
test('_menu param object all and value', () => {
|
||||
const input = {
|
||||
_menu: {
|
||||
all: true,
|
||||
@ -214,7 +204,6 @@ test('_menu param object all and value', async () => {
|
||||
},
|
||||
};
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual([
|
||||
{
|
||||
@ -230,14 +219,13 @@ test('_menu param object all and value', async () => {
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_menu param object invalid', async () => {
|
||||
test('_menu param object invalid', () => {
|
||||
const input = {
|
||||
_menu: {
|
||||
other: true,
|
||||
},
|
||||
};
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
|
@ -57,10 +57,9 @@ const context = {
|
||||
|
||||
console.error = () => {};
|
||||
|
||||
test('_request by id', async () => {
|
||||
test('_request by id', () => {
|
||||
const input = { a: { _request: 'string' } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual({
|
||||
a: 'request String',
|
||||
@ -68,10 +67,9 @@ test('_request by id', async () => {
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_request true gives null', async () => {
|
||||
test('_request true gives null', () => {
|
||||
const input = { _request: true };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -81,28 +79,25 @@ test('_request true gives null', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_request return full array', async () => {
|
||||
test('_request return full array', () => {
|
||||
const input = { _request: 'arr' };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual([{ a: 'request a1' }, { a: 'request a2' }]);
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_request return number', async () => {
|
||||
test('_request return number', () => {
|
||||
const input = { _request: 'number' };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toBe(500);
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_request null', async () => {
|
||||
test('_request null', () => {
|
||||
const input = { _request: null };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toBe(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -112,37 +107,33 @@ test('_request null', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_request loading true', async () => {
|
||||
test('_request loading true', () => {
|
||||
const input = { _request: 'not_loaded' };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toBe(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_request dot notation', async () => {
|
||||
test('_request dot notation', () => {
|
||||
const input = { _request: 'arr.0.a' };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual('request a1');
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_request dot notation with arrayindices', async () => {
|
||||
test('_request dot notation with arrayindices', () => {
|
||||
const input = { _request: 'arr.$.a' };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual('request a2');
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_request dot notation returns null if ', async () => {
|
||||
test('_request dot notation returns null if ', () => {
|
||||
const input = { _request: 'returnsNull.key' };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId', arrayIndices });
|
||||
expect(res.output).toEqual(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
|
@ -49,15 +49,14 @@ test('_and errors', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('_and evaluated in NodeParser', async () => {
|
||||
test('_and evaluated in NodeParser', () => {
|
||||
const input = { a: { _and: [true, true] } };
|
||||
const parser = new NodeParser({ operators, payload: {}, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: true });
|
||||
});
|
||||
|
||||
test('_and evaluated in WebParser', async () => {
|
||||
test('_and evaluated in WebParser', () => {
|
||||
const context = {
|
||||
_internal: {
|
||||
lowdefy: {
|
||||
@ -75,7 +74,6 @@ test('_and evaluated in WebParser', async () => {
|
||||
};
|
||||
const input = { a: { _and: [true, true] } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: true });
|
||||
});
|
||||
|
@ -33,9 +33,6 @@ const operators = {
|
||||
const location = 'location';
|
||||
|
||||
const parser = new NodeParser({ operators, payload: {}, secrets: {}, user: {} });
|
||||
beforeAll(async () => {
|
||||
await parser.init();
|
||||
});
|
||||
|
||||
describe('_array.concat', () => {
|
||||
const methodName = 'concat';
|
||||
|
@ -57,27 +57,24 @@ const context = {
|
||||
|
||||
console.error = () => {};
|
||||
|
||||
test('NodeParser, _function that gets from payload', async () => {
|
||||
test('NodeParser, _function that gets from payload', () => {
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const params = { __payload: 'string' };
|
||||
const fn = _function({ location, params, parser });
|
||||
expect(fn).toBeInstanceOf(Function);
|
||||
expect(fn()).toEqual('Some String');
|
||||
});
|
||||
|
||||
test('NodeParser, _function gives args as an array', async () => {
|
||||
test('NodeParser, _function gives args as an array', () => {
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const params = { __args: true };
|
||||
const fn = _function({ location, params, parser });
|
||||
expect(fn('a')).toEqual(['a']);
|
||||
expect(fn('a', { b: true })).toEqual(['a', { b: true }]);
|
||||
});
|
||||
|
||||
test('NodeParser, _function throws on parser errors', async () => {
|
||||
test('NodeParser, _function throws on parser errors', () => {
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const params = { __payload: [] };
|
||||
const fn = _function({ location, params, parser });
|
||||
expect(fn).toThrow(
|
||||
@ -85,9 +82,8 @@ test('NodeParser, _function throws on parser errors', async () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('WebParser, _function that gets from state', async () => {
|
||||
test('WebParser, _function that gets from state', () => {
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const params = { __state: 'string' };
|
||||
const fn = _function({ location, params, parser });
|
||||
expect(fn).toBeInstanceOf(Function);
|
||||
@ -95,18 +91,16 @@ test('WebParser, _function that gets from state', async () => {
|
||||
expect(fn()).toEqual('Some String');
|
||||
});
|
||||
|
||||
test('WebParser, _function gives args as an array', async () => {
|
||||
test('WebParser, _function gives args as an array', () => {
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const params = { __args: true };
|
||||
const fn = _function({ location, params, parser });
|
||||
expect(fn('a')).toEqual(['a']);
|
||||
expect(fn('a', { b: true })).toEqual(['a', { b: true }]);
|
||||
});
|
||||
|
||||
test('WebParser, _function throws on parser errors', async () => {
|
||||
test('WebParser, _function throws on parser errors', () => {
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const params = { __state: [] };
|
||||
const fn = _function({ location, params, parser });
|
||||
expect(fn).toThrow(
|
||||
|
@ -43,10 +43,9 @@ const payload = {
|
||||
|
||||
console.error = () => {};
|
||||
|
||||
test('_operator, _payload', async () => {
|
||||
test('_operator, _payload', () => {
|
||||
const input = { a: { _operator: { name: '_payload', params: 'string' } } };
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({
|
||||
a: 'Some String',
|
||||
@ -54,10 +53,9 @@ test('_operator, _payload', async () => {
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_operator.name invalid', async () => {
|
||||
test('_operator.name invalid', () => {
|
||||
const input = { a: { _operator: { name: '_a' } } };
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: null });
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -67,10 +65,9 @@ test('_operator.name invalid', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_operator.name not allowed to include "experimental"', async () => {
|
||||
test('_operator.name not allowed to include "experimental"', () => {
|
||||
const input = { a: { _operator: { name: '_experimental_op' } } };
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: null });
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -80,10 +77,9 @@ test('_operator.name not allowed to include "experimental"', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_operator.name not a string', async () => {
|
||||
test('_operator.name not a string', () => {
|
||||
const input = { a: { _operator: { name: 1 } } };
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: null });
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -93,10 +89,9 @@ test('_operator.name not a string', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_operator with value not a object', async () => {
|
||||
test('_operator with value not a object', () => {
|
||||
const input = { a: { _operator: 'a' } };
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: null });
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -106,10 +101,9 @@ test('_operator with value not a object', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_operator cannot be set to _operator', async () => {
|
||||
test('_operator cannot be set to _operator', () => {
|
||||
const input = { a: { _operator: { name: '_operator' } } };
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: null });
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
@ -119,19 +113,17 @@ test('_operator cannot be set to _operator', async () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('_operator, _not with no params', async () => {
|
||||
test('_operator, _not with no params', () => {
|
||||
const input = { a: { _operator: { name: '_not' } } };
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: true });
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_operator, _json.parse with params', async () => {
|
||||
test('_operator, _json.parse with params', () => {
|
||||
const input = { a: { _operator: { name: '_json.parse', params: '[{ "a": "a1"}]' } } };
|
||||
const parser = new NodeParser({ operators, payload });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({
|
||||
a: [{ a: 'a1' }],
|
||||
|
@ -49,15 +49,14 @@ test('_or errors', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('_or evaluated in NodeParser', async () => {
|
||||
test('_or evaluated in NodeParser', () => {
|
||||
const input = { a: { _or: [true, false] } };
|
||||
const parser = new NodeParser({ operators, payload: {}, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: true });
|
||||
});
|
||||
|
||||
test('_or evaluated in WebParser', async () => {
|
||||
test('_or evaluated in WebParser', () => {
|
||||
const context = {
|
||||
_internal: {
|
||||
lowdefy: {
|
||||
@ -75,7 +74,6 @@ test('_or evaluated in WebParser', async () => {
|
||||
};
|
||||
const input = { a: { _or: [true, false] } };
|
||||
const parser = new WebParser({ context, operators });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual({ a: true });
|
||||
});
|
||||
|
@ -103,10 +103,9 @@ test('_type none', () => {
|
||||
expect(_type({ params: { type: 'none' }, location, state })).toEqual(true);
|
||||
});
|
||||
|
||||
test('_type date with on packed date pass and calls NodeParser', async () => {
|
||||
test('_type date with on packed date pass and calls NodeParser', () => {
|
||||
const input = { _type: { type: 'date', on: { _date: Date.now() } } };
|
||||
const parser = new NodeParser({ operators, payload: {}, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location });
|
||||
expect(res.output).toEqual(true);
|
||||
});
|
||||
|
@ -29,81 +29,73 @@ const payload = {
|
||||
|
||||
console.error = () => {};
|
||||
|
||||
test('_nunjucks string template', async () => {
|
||||
test('_nunjucks string template', () => {
|
||||
const input = { _nunjucks: 'String with {{ string }} embedded' };
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId' });
|
||||
expect(res.output).toEqual('String with Some String embedded');
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_nunjucks null', async () => {
|
||||
test('_nunjucks null', () => {
|
||||
const input = { _nunjucks: null };
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId' });
|
||||
expect(res.output).toBe(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_nunjucks { template: , on: }', async () => {
|
||||
test('_nunjucks { template: , on: }', () => {
|
||||
const input = {
|
||||
_nunjucks: { template: 'String with {{ string }} embedded', on: { string: 'test' } },
|
||||
};
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId' });
|
||||
expect(res.output).toEqual('String with test embedded');
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_nunjucks template not a string', async () => {
|
||||
test('_nunjucks template not a string', () => {
|
||||
const input = { _nunjucks: ['String with {{ string }} embedded'] };
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId' });
|
||||
expect(res.output).toBe(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_nunjucks params on template not a string', async () => {
|
||||
test('_nunjucks params on template not a string', () => {
|
||||
const input = {
|
||||
_nunjucks: { template: ['String with {{ string }} embedded'], on: { string: 'test' } },
|
||||
};
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId' });
|
||||
expect(res.output).toBe(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_nunjucks on not a object', async () => {
|
||||
test('_nunjucks on not a object', () => {
|
||||
const input = {
|
||||
_nunjucks: { template: 'String with {{ string }} embedded', on: [{ string: 'test' }] },
|
||||
};
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId' });
|
||||
expect(res.output).toBe('String with embedded');
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_nunjucks on null', async () => {
|
||||
test('_nunjucks on null', () => {
|
||||
const input = {
|
||||
_nunjucks: { template: 'String with {{ string }} embedded', on: null },
|
||||
};
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId' });
|
||||
expect(res.output).toBe('String with embedded');
|
||||
expect(res.errors).toMatchInlineSnapshot(`Array []`);
|
||||
});
|
||||
|
||||
test('_nunjucks invalid template', async () => {
|
||||
test('_nunjucks invalid template', () => {
|
||||
const input = { _nunjucks: 'String with {{ string embedded' };
|
||||
const parser = new NodeParser({ operators, payload, secrets: {}, user: {} });
|
||||
await parser.init();
|
||||
const res = parser.parse({ input, location: 'locationId' });
|
||||
expect(res.output).toBe(null);
|
||||
expect(res.errors).toMatchInlineSnapshot(`
|
||||
|
Loading…
x
Reference in New Issue
Block a user