From a5ca6e68981151f6a81f368e56c198c7cebc24ac Mon Sep 17 00:00:00 2001 From: Bowen Tan Date: Wed, 19 Jan 2022 10:41:07 +0800 Subject: [PATCH] add removeModuleId test --- packages/editor/__tests__/addModuleId.spec.ts | 59 ++++++++++++++++++- packages/editor/src/utils/addModuleId.ts | 4 +- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/packages/editor/__tests__/addModuleId.spec.ts b/packages/editor/__tests__/addModuleId.spec.ts index cac21c28..a8db2a6d 100644 --- a/packages/editor/__tests__/addModuleId.spec.ts +++ b/packages/editor/__tests__/addModuleId.spec.ts @@ -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", + }, + ] + `); + }); }); diff --git a/packages/editor/src/utils/addModuleId.ts b/packages/editor/src/utils/addModuleId.ts index d0f0a51c..987b428c 100644 --- a/packages/editor/src/utils/addModuleId.ts +++ b/packages/editor/src/utils/addModuleId.ts @@ -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; -} \ No newline at end of file +}