roymondchen dfc4f55390 feat(core): 新增 setPageFragment/deletePageFragment 同步页面片实例
App 新增 setPageFragment / deletePageFragment 方法,用于同步页面片实例。
容器关联、切换、取消关联页面片时同步 pageFragments,并触发 page-fragments-change。

- Page 重建时强制同步页面片内容(force)
- useDsl 改用动态获取页面片实例,避免 setup 快照过期
- useEditorDsl 在关联变更、删除容器时同步页面片实例
- 补充 core 与 vue-runtime-help 单测,更新 runtime-api 文档
2026-11-02 14:39:56 +08:00

86 lines
2.3 KiB
TypeScript

import { beforeEach, describe, expect, test } from 'vitest';
import { defineComponent, h, nextTick } from 'vue';
import { mount } from '@vue/test-utils';
import type { MApp } from '@tmagic/core';
import Core, { NodeType } from '@tmagic/core';
import { useDsl } from '../src';
const createDsl = () =>
({
type: NodeType.ROOT,
id: 'app',
items: [
{
type: NodeType.PAGE,
id: 'p1',
items: [{ id: 'pfc', type: 'page-fragment-container' }],
},
{
type: NodeType.PAGE_FRAGMENT,
id: 'f1',
items: [{ id: 'text_in_f1', type: 'text' }],
},
],
}) as unknown as MApp;
const createApp = () => new Core({ config: createDsl(), platform: 'editor' });
const createChild = (pageFragmentContainerId?: string) =>
defineComponent({
setup() {
const { pageConfig } = useDsl(undefined as any, pageFragmentContainerId);
return () =>
h(
'div',
(pageConfig.value?.items || []).map((item) => h('span', { id: item.id })),
);
},
});
const render = (app: Core, pageFragmentContainerId?: string) =>
mount(createChild(pageFragmentContainerId), {
global: {
provide: { app },
},
});
describe('useDsl 页面片', () => {
beforeEach(() => {
document.body.innerHTML = '';
});
test('未关联页面片时渲染为空', () => {
const app = createApp();
const wrapper = render(app, 'pfc');
expect(wrapper.find('span').exists()).toBe(false);
});
test('关联页面片后页面片内容会渲染出来', async () => {
const app = createApp();
const wrapper = render(app, 'pfc');
app.setPageFragment({ id: 'pfc', type: 'page-fragment-container', pageFragmentId: 'f1' });
await nextTick();
expect(wrapper.find('#text_in_f1').exists()).toBe(true);
});
test('取消关联后页面片内容消失', async () => {
const app = createApp();
const wrapper = render(app, 'pfc');
app.setPageFragment({ id: 'pfc', type: 'page-fragment-container', pageFragmentId: 'f1' });
await nextTick();
expect(wrapper.find('#text_in_f1').exists()).toBe(true);
app.setPageFragment({ id: 'pfc', type: 'page-fragment-container', pageFragmentId: '' });
await nextTick();
expect(wrapper.find('span').exists()).toBe(false);
});
});