add removeModuleId test

This commit is contained in:
Bowen Tan 2022-01-19 10:41:07 +08:00
parent 504cdc69e7
commit a5ca6e6898
2 changed files with 60 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { Type } from '@sinclair/typebox';
import { addModuleId } from '../src/utils/addModuleId';
import { addModuleId, removeModuleId } from '../src/utils/addModuleId';
describe('format to module schema', () => {
it('will add module id to the expression', () => {
@ -58,4 +58,61 @@ describe('format to module schema', () => {
]
`);
});
it('will remove module id in expression', () => {
expect(
removeModuleId({
version: 'test/v1',
kind: 'Module',
metadata: {
name: 'test',
},
spec: {
properties: {
value: Type.Object({
BE_CAREFUL: Type.Number(),
}),
},
events: [],
stateMap: {},
},
impl: [
{
id: '{{ $moduleId }}__input1',
type: 'test/v1/input',
properties: {},
traits: [],
},
{
id: 'BE_CAREFUL',
type: 'test/v1/child',
properties: {
test: '{{ value.BE_CAREFUL.toFixed(2) }}',
add: '{{ {{ $moduleId }}__input1.value }} / {{ {{ $moduleId }}__BE_CAREFUL.value }}'
},
traits: [],
},
],
}).impl
).toMatchInlineSnapshot(`
Array [
Object {
"id": "input1",
"properties": Object {},
"traits": Array [],
"type": "test/v1/input",
},
Object {
"id": "BE_CAREFUL",
"properties": Object {
"add": "{{ input1.value }} / {{ BE_CAREFUL.value }}",
"test": "{{ value.BE_CAREFUL.toFixed(2) }}",
},
"traits": Array [],
"type": "test/v1/child",
},
]
`);
});
});

View File

@ -51,7 +51,7 @@ export function removeModuleId(module: Module): Module {
for (const key in tree) {
const val = tree[key];
if (typeof val === 'string') {
tree[key] = val.replaceAll(ModuleIdPrefix, '');
tree[key] = val.replace(/{{ \$moduleId }}__/g, '');
} else if (typeof val === 'object') {
traverse(val);
}
@ -113,4 +113,4 @@ function replaceIdsInExp(exp: string, ids: string[]): string | void {
}, exp);
return newExp;
}
}