fix(build): Add list of operators in context to build.

This commit is contained in:
Gervwyk 2021-03-30 21:51:15 +02:00
parent 63f8d14379
commit 88a6f24c8f
2 changed files with 239 additions and 4 deletions

View File

@ -16,7 +16,7 @@
limitations under the License.
*/
import { set, type } from '@lowdefy/helpers';
import { get, set, type } from '@lowdefy/helpers';
/* Page and block build steps
@ -30,8 +30,45 @@ Blocks:
- set request ids
- set block meta
- set blocks to areas.content
- set operators list on context blocks
*/
function getContextOperators(block) {
const stripContext = (_, value) => {
if (get(value, 'meta.category') === 'context') {
return null;
}
return value;
};
const { requests, ...webBlock } = block;
webBlock.areas = JSON.parse(JSON.stringify(webBlock.areas || {}), stripContext);
const operators = new Set();
const pushOperators = (_, value) => {
if (type.isObject(value) && Object.keys(value).length === 1) {
const key = Object.keys(value)[0];
const [op, _] = key.split('.');
const operator = op.replace(/^(\_+)/gm, '_');
if (operator.length > 1 && operator[0] === '_') {
operators.add(operator);
}
}
return value;
};
JSON.parse(JSON.stringify(webBlock), pushOperators);
return [...operators];
}
function fillContextOperators(block) {
if (get(block, 'meta.category') === 'context') {
block.operators = getContextOperators(block);
}
Object.keys(block.areas || {}).forEach((key) => {
block.areas[key].blocks.map((blk) => {
fillContextOperators(blk);
});
});
}
function buildRequests(block, context) {
if (!type.isNone(block.requests)) {
if (!type.isArray(block.requests)) {
@ -125,10 +162,10 @@ async function buildBlock(block, context) {
if (block.meta.category === 'context') {
ctx = {
auth: context.auth,
pageId: context.pageId,
contextId: block.blockId,
requests: [],
metaLoader: context.metaLoader,
pageId: context.pageId,
requests: [],
};
}
buildRequests(block, ctx);
@ -189,6 +226,7 @@ async function buildPages({ components, context }) {
});
// set page.id since buildBlock sets id as well.
page.id = `page:${page.pageId}`;
fillContextOperators(page);
});
await Promise.all(pageBuildPromises);
return components;

View File

@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import { get } from '@lowdefy/helpers';
import buildPages from './buildPages';
import testContext from '../test/testContext';
@ -377,6 +377,7 @@ test('no blocks on page', async () => {
{
id: 'page:1',
auth: 'public',
operators: [],
pageId: '1',
blockId: '1',
type: 'Context',
@ -450,6 +451,7 @@ test('block meta should include all meta fields', async () => {
{
id: 'page:page_1',
auth: 'public',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -513,6 +515,7 @@ test('nested blocks', async () => {
{
id: 'page:page_1',
auth: 'public',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -589,6 +592,7 @@ describe('block areas', () => {
id: 'page:page1',
auth: 'public',
blockId: 'page1',
operators: [],
pageId: 'page1',
type: 'Context',
meta: outputMetas.Context,
@ -631,6 +635,7 @@ describe('block areas', () => {
id: 'page:1',
auth: 'public',
blockId: '1',
operators: [],
pageId: '1',
type: 'Context',
meta: outputMetas.Context,
@ -682,6 +687,7 @@ describe('block areas', () => {
id: 'page:1',
auth: 'public',
pageId: '1',
operators: [],
blockId: '1',
type: 'Context',
meta: outputMetas.Context,
@ -739,6 +745,7 @@ describe('block areas', () => {
{
id: 'page:1',
auth: 'public',
operators: [],
pageId: '1',
blockId: '1',
type: 'Context',
@ -804,6 +811,7 @@ describe('block areas', () => {
{
id: 'page:1',
auth: 'public',
operators: [],
pageId: '1',
blockId: '1',
type: 'Context',
@ -877,6 +885,7 @@ describe('block areas', () => {
{
id: 'page:1',
auth: 'public',
operators: [],
pageId: '1',
blockId: '1',
type: 'Context',
@ -966,6 +975,7 @@ describe('block areas', () => {
{
id: 'page:1',
auth: 'public',
operators: [],
pageId: '1',
blockId: '1',
type: 'Context',
@ -1072,6 +1082,7 @@ describe('build requests', () => {
{
id: 'page:page_1',
auth: 'public',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -1117,6 +1128,7 @@ describe('build requests', () => {
{
id: 'page:page_1',
auth: 'public',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -1129,6 +1141,7 @@ describe('build requests', () => {
id: 'block:page_1:context',
blockId: 'context',
type: 'Context',
operators: [],
meta: outputMetas.Context,
requests: [
{
@ -1176,6 +1189,7 @@ describe('build requests', () => {
id: 'page:page_1',
auth: 'public',
blockId: 'page_1',
operators: [],
pageId: 'page_1',
type: 'Context',
meta: outputMetas.Context,
@ -1238,6 +1252,7 @@ describe('build requests', () => {
{
id: 'page:page_1',
auth: 'public',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -1250,6 +1265,7 @@ describe('build requests', () => {
id: 'block:page_1:context',
blockId: 'context',
type: 'Context',
operators: [],
meta: outputMetas.Context,
requests: [
{
@ -1329,6 +1345,7 @@ describe('build requests', () => {
url: 'https://example.com/remoteEntry.js',
},
},
operators: [],
pageId: 'page_1',
requests: [
{
@ -1344,6 +1361,7 @@ describe('build requests', () => {
{
id: 'block:page_1:context',
blockId: 'context',
operators: [],
type: 'Context',
requests: [],
areas: {
@ -1425,6 +1443,7 @@ describe('build requests', () => {
{
id: 'page:page_1',
auth: 'public',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -1478,6 +1497,7 @@ test('add user defined loading to meta', async () => {
{
id: 'page:page_1',
auth: 'public',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -1557,6 +1577,7 @@ describe('auth field', () => {
{
id: 'page:page_1',
auth: 'defaulted',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -1566,6 +1587,7 @@ describe('auth field', () => {
{
id: 'page:page_2',
auth: 'defaulted',
operators: [],
pageId: 'page_2',
blockId: 'page_2',
type: 'Context',
@ -1605,6 +1627,7 @@ describe('auth field', () => {
{
id: 'page:page_1',
auth: 'defaulted',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -1614,6 +1637,7 @@ describe('auth field', () => {
{
id: 'page:page_2',
auth: 'setting',
operators: [],
pageId: 'page_2',
blockId: 'page_2',
type: 'Context',
@ -1663,6 +1687,7 @@ describe('auth field', () => {
{
id: 'page:page_1',
auth: 'defaulted',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -1679,6 +1704,7 @@ describe('auth field', () => {
{
id: 'page:page_2',
auth: 'defaulted',
operators: [],
pageId: 'page_2',
blockId: 'page_2',
type: 'Context',
@ -1735,6 +1761,7 @@ describe('auth field', () => {
{
id: 'page:page_1',
auth: 'setting',
operators: [],
pageId: 'page_1',
blockId: 'page_1',
type: 'Context',
@ -1751,6 +1778,7 @@ describe('auth field', () => {
{
id: 'page:page_2',
auth: 'defaulted',
operators: [],
pageId: 'page_2',
blockId: 'page_2',
type: 'Context',
@ -1768,3 +1796,172 @@ describe('auth field', () => {
});
});
});
describe('web operators', () => {
test('set empty operators array for every context', async () => {
const components = {
auth,
pages: [
{
id: 'page_1',
type: 'Context',
blocks: [
{
id: 'context_1',
type: 'Context',
},
{
id: 'context_2',
type: 'Context',
blocks: [
{
id: 'context_2_1',
type: 'Context',
},
{
id: 'block_2_2',
type: 'Display',
},
],
},
{
id: 'block_3',
type: 'Display',
},
],
},
{
id: 'page_2',
type: 'Context',
},
],
};
const res = await buildPages({ components, context });
expect(get(res, 'pages.0.operators')).toEqual([]);
expect(get(res, 'pages.0.areas.content.blocks.0.operators')).toEqual([]);
expect(get(res, 'pages.0.areas.content.blocks.1.areas.content.blocks.0.operators')).toEqual([]);
expect(get(res, 'pages.1.operators')).toEqual([]);
});
test('set all operators for context', async () => {
const components = {
auth,
pages: [
{
id: 'page_1',
type: 'Context',
properties: {
a: { _c_op_1: {} },
},
blocks: [
{
id: 'block_1',
type: 'Display',
visible: {
_v_1: {},
},
properties: {
a: { _op_1: {} },
b: { _op_1: {} },
c: { _op_2: { __op_3: { ___op_4: {} } } },
},
},
],
},
],
};
const res = await buildPages({ components, context });
expect(get(res, 'pages.0.operators')).toEqual([
'_c_op_1',
'_v_1',
'_op_1',
'_op_4',
'_op_3',
'_op_2',
]);
});
test('exclude requests operators', async () => {
const components = {
auth,
pages: [
{
id: 'page_1',
type: 'Context',
requests: [
{
id: 'request_1',
properties: {
a: { _r_op_1: {} },
},
},
],
properties: {
a: { _c_op_1: {} },
},
blocks: [
{
id: 'block_1',
type: 'Display',
visible: {
_v_1: {},
},
properties: {
a: { _op_1: {} },
b: { _op_1: {} },
c: { _op_2: { __op_3: { ___op_4: {} } } },
},
},
],
},
],
};
const res = await buildPages({ components, context });
expect(get(res, 'pages.0.operators')).toEqual([
'_c_op_1',
'_v_1',
'_op_1',
'_op_4',
'_op_3',
'_op_2',
]);
});
test('set operators specific to multiple contexts', async () => {
const components = {
auth,
pages: [
{
id: 'page_1',
type: 'Context',
properties: {
a: { _c_op_1: {} },
},
blocks: [
{
id: 'block_1',
type: 'Context',
visible: {
_v_1: {},
},
properties: {
a: { _op_1: {} },
b: { _op_1: {} },
c: { _op_2: { __op_3: { ___op_4: {} } } },
},
},
],
},
],
};
const res = await buildPages({ components, context });
expect(get(res, 'pages.0.operators')).toEqual(['_c_op_1']);
expect(get(res, 'pages.0.areas.content.blocks.0.operators')).toEqual([
'_v_1',
'_op_1',
'_op_4',
'_op_3',
'_op_2',
]);
});
});