93 lines
3.0 KiB
TypeScript
93 lines
3.0 KiB
TypeScript
import { afterEach, expect, it } from "vitest";
|
||
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||
import { Ui结构表单 } from "../../src/界面/结构表单/结构表单";
|
||
import { 生成字段修改, 投影表单值 } from "../../src/界面/结构表单/投影会话";
|
||
|
||
afterEach(cleanup);
|
||
|
||
it("NC-form-rebase-intent:冲突刷新只保留作者真正改动的字段", async () => {
|
||
const schema = {
|
||
type: "object",
|
||
properties: { 名称: { type: "string" }, 摘要: { type: "string" } },
|
||
};
|
||
let output: unknown;
|
||
const submit = async (changes: unknown) => {
|
||
output = changes;
|
||
};
|
||
const view = render(
|
||
<Ui结构表单
|
||
visible={schema}
|
||
editable={schema}
|
||
initial={{ 名称: "旧名", 摘要: "旧摘要" }}
|
||
onSubmit={submit}
|
||
/>,
|
||
);
|
||
fireEvent.change(screen.getByLabelText("名称"), {
|
||
target: { value: "我改的名字" },
|
||
});
|
||
view.rerender(
|
||
<Ui结构表单
|
||
visible={schema}
|
||
editable={schema}
|
||
initial={{ 名称: "旧名", 摘要: "服务器新摘要" }}
|
||
onSubmit={submit}
|
||
/>,
|
||
);
|
||
expect((screen.getByLabelText("摘要") as HTMLInputElement).value).toBe(
|
||
"服务器新摘要",
|
||
);
|
||
fireEvent.click(screen.getByRole("button", { name: "保存档案" }));
|
||
expect(output).toEqual([{ 路径: ["名称"], 动作: "set", 值: "我改的名字" }]);
|
||
});
|
||
it("NC-form-editable-projection:嵌套字段只提交可编辑叶子并隐藏受限值", async () => {
|
||
const visible = {
|
||
type: "object",
|
||
properties: {
|
||
人物: {
|
||
type: "object",
|
||
properties: {
|
||
姓名: { type: "string", title: "姓名" },
|
||
编号: { type: "string", title: "编号" },
|
||
},
|
||
},
|
||
},
|
||
};
|
||
const editable = {
|
||
type: "object",
|
||
properties: {
|
||
人物: { type: "object", properties: { 姓名: { type: "string" } } },
|
||
},
|
||
};
|
||
const base = { 人物: { 姓名: "旧名", 编号: "只读身份", 秘密: "不可显示" } };
|
||
let output: unknown;
|
||
render(
|
||
<Ui结构表单
|
||
visible={visible}
|
||
editable={editable}
|
||
initial={base}
|
||
onSubmit={async (changes) => {
|
||
output = changes;
|
||
}}
|
||
/>,
|
||
);
|
||
expect(screen.queryByText("不可显示")).toBeNull();
|
||
expect((screen.getByLabelText("编号") as HTMLInputElement).readOnly).toBe(
|
||
true,
|
||
);
|
||
fireEvent.change(screen.getByLabelText("姓名"), {
|
||
target: { value: "新名" },
|
||
});
|
||
fireEvent.click(screen.getByRole("button", { name: "保存档案" }));
|
||
expect(output).toEqual([{ 路径: ["人物", "姓名"], 动作: "set", 值: "新名" }]);
|
||
});
|
||
|
||
it("NC-form-projection-refresh:新投影清除不允许显示的旧输入", () => {
|
||
const schema = { type: "object", properties: { 公开: { type: "string" } } };
|
||
expect(投影表单值(schema, { 公开: "保留", 秘密: "旧输入" })).toEqual({
|
||
公开: "保留",
|
||
});
|
||
expect(生成字段修改({ 公开: "旧值" }, { 公开: "保留" }, schema)).toEqual([
|
||
{ 路径: ["公开"], 动作: "set", 值: "保留" },
|
||
]);
|
||
});
|