vendor: amis 6.13.0 base (fork root)

This commit is contained in:
2026-06-16 22:14:57 +08:00
commit 8c8e4247a9
3840 changed files with 2467374 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,646 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`doAction:page reload 1`] = `
<div>
<div
class="cxd-Page"
>
<div
class="cxd-Page-content"
>
<div
class="cxd-Page-main"
>
<div
class="cxd-Page-body"
role="page-body"
>
<button
class="cxd-Button cxd-Button--primary cxd-Button--size-default mb-2"
type="button"
>
<span>
刷新Page数据加载请求
</span>
</button>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="title"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的标题:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="title"
placeholder=""
size="10"
type="text"
value="Test Page Component"
/>
</div>
</div>
</div>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="age"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的年龄:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="age"
placeholder=""
size="10"
type="text"
value=""
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;
exports[`doAction:page reload 2`] = `
<div>
<div
class="cxd-Page"
>
<div
class="cxd-Page-content"
>
<div
class="cxd-Page-main"
>
<div
class="cxd-Page-body"
role="page-body"
>
<div
class="cxd-Spinner-overlay in"
/>
<div
class="cxd-Spinner cxd-Spinner--overlay in"
data-testid="spinner"
>
<div
class="cxd-Spinner-icon cxd-Spinner-icon--lg cxd-Spinner-icon--default"
/>
</div>
<button
class="cxd-Button cxd-Button--primary cxd-Button--size-default mb-2"
type="button"
>
<span>
刷新Page数据加载请求
</span>
</button>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="title"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的标题:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="title"
placeholder=""
size="10"
type="text"
value="Test Page Component"
/>
</div>
</div>
</div>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="age"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的年龄:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="age"
placeholder=""
size="10"
type="text"
value=""
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;
exports[`doAction:page reload with data 1`] = `
<div>
<div
class="cxd-Page"
>
<div
class="cxd-Page-content"
>
<div
class="cxd-Page-main"
>
<div
class="cxd-Page-body"
role="page-body"
>
<button
class="cxd-Button cxd-Button--primary cxd-Button--size-default mb-2"
type="button"
>
<span>
刷新Page数据加载请求,同时把年龄更新为18
</span>
</button>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="title"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的标题:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="title"
placeholder=""
size="10"
type="text"
value="Test Page Component"
/>
</div>
</div>
</div>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="age"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的年龄:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="age"
placeholder=""
size="10"
type="text"
value=""
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;
exports[`doAction:page reload with data 2`] = `
<div>
<div
class="cxd-Page"
>
<div
class="cxd-Page-content"
>
<div
class="cxd-Page-main"
>
<div
class="cxd-Page-body"
role="page-body"
>
<div
class="cxd-Spinner-overlay in"
/>
<div
class="cxd-Spinner cxd-Spinner--overlay in"
data-testid="spinner"
>
<div
class="cxd-Spinner-icon cxd-Spinner-icon--lg cxd-Spinner-icon--default"
/>
</div>
<button
class="cxd-Button cxd-Button--primary cxd-Button--size-default mb-2"
type="button"
>
<span>
刷新Page数据加载请求,同时把年龄更新为18
</span>
</button>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="title"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的标题:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="title"
placeholder=""
size="10"
type="text"
value="Test Page Component"
/>
</div>
</div>
</div>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="age"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的年龄:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="age"
placeholder=""
size="10"
type="text"
value="18"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;
exports[`doAction:page reload with data(same name) 1`] = `
<div>
<div
class="cxd-Page"
>
<div
class="cxd-Page-content"
>
<div
class="cxd-Page-main"
>
<div
class="cxd-Page-body"
role="page-body"
>
<button
class="cxd-Button cxd-Button--primary cxd-Button--size-default mb-2"
type="button"
>
<span>
刷新Page数据加载请求,请求返回的date会覆盖动作传递的date
</span>
</button>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="title"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的标题:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="title"
placeholder=""
size="10"
type="text"
value="Test Page Component"
/>
</div>
</div>
</div>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="date"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
日期:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="date"
placeholder=""
size="10"
type="text"
value="2017-10-13"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;
exports[`doAction:page reload with data(same name) 2`] = `
<div>
<div
class="cxd-Page"
>
<div
class="cxd-Page-content"
>
<div
class="cxd-Page-main"
>
<div
class="cxd-Page-body"
role="page-body"
>
<div
class="cxd-Spinner-overlay"
/>
<div
class="cxd-Spinner cxd-Spinner--overlay"
data-testid="spinner"
>
<div
class="cxd-Spinner-icon cxd-Spinner-icon--lg cxd-Spinner-icon--default"
/>
</div>
<button
class="cxd-Button cxd-Button--primary cxd-Button--size-default mb-2"
type="button"
>
<span>
刷新Page数据加载请求,请求返回的date会覆盖动作传递的date
</span>
</button>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="title"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
我的标题:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="title"
placeholder=""
size="10"
type="text"
value="Test Page Component"
/>
</div>
</div>
</div>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="date"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
日期:
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
name="date"
placeholder=""
size="10"
type="text"
value="2017-10-13"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;

View File

@@ -0,0 +1,87 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv} from '../../helper';
test('EventAction:action', async () => {
const notify = jest.fn();
const {getByText, container}: any = render(
amisRender(
{
type: 'page',
data: {
btnDisabled: true,
btnNotDisabled: false
},
body: [
{
type: 'button',
label: 'action',
level: 'primary',
onEvent: {
click: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发点击事件'
}
}
]
},
mouseenter: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发鼠标移入事件'
}
}
]
},
mouseleave: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发鼠标移出事件'
}
}
]
}
}
}
]
},
{},
makeEnv({
notify
})
)
);
fireEvent.click(getByText('action'));
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发点击事件', {
msg: '派发点击事件',
msgType: 'info'
});
});
fireEvent.mouseEnter(getByText('action'));
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发鼠标移入事件', {
msg: '派发鼠标移入事件',
msgType: 'info'
});
});
fireEvent.mouseLeave(getByText('action'));
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发鼠标移出事件', {
msg: '派发鼠标移出事件',
msgType: 'info'
});
});
});

View File

@@ -0,0 +1,87 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv} from '../../helper';
test('EventAction:avatar', async () => {
const notify = jest.fn();
const {getByText, container}: any = render(
amisRender(
{
type: 'page',
data: {
btnDisabled: true,
btnNotDisabled: false
},
body: [
{
type: 'avatar',
text: 'AM',
onEvent: {
click: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发点击事件'
}
}
]
},
mouseenter: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发鼠标移入事件'
}
}
]
},
mouseleave: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发鼠标移出事件'
}
}
]
}
}
}
]
},
{},
makeEnv({
notify
})
)
);
const avatar = container.querySelector('.cxd-Avatar');
fireEvent.click(avatar);
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发点击事件', {
msg: '派发点击事件',
msgType: 'info'
});
});
fireEvent.mouseEnter(avatar);
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发鼠标移入事件', {
msg: '派发鼠标移入事件',
msgType: 'info'
});
});
fireEvent.mouseLeave(avatar);
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发鼠标移出事件', {
msg: '派发鼠标移出事件',
msgType: 'info'
});
});
});

View File

@@ -0,0 +1,75 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv} from '../../helper';
test('EventAction:collapse', async () => {
const notify = jest.fn();
const {getByText, container}: any = render(
amisRender(
{
type: 'page',
data: {},
body: [
{
type: 'collapse',
header: '折叠器',
body: [
{
type: 'tpl',
tpl: '内容',
wrapperComponent: '',
inline: false
}
],
onEvent: {
expand: {
weight: 0,
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发展开事件'
}
}
]
},
collapse: {
weight: 0,
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发收起事件'
}
}
]
}
}
}
]
},
{},
makeEnv({
notify
})
)
);
fireEvent.click(getByText('折叠器'));
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发收起事件', {
msg: '派发收起事件',
msgType: 'info'
});
});
fireEvent.click(getByText('折叠器'));
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发展开事件', {
msg: '派发展开事件',
msgType: 'info'
});
});
});

View File

@@ -0,0 +1,82 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv} from '../../helper';
test('EventAction:container', async () => {
const notify = jest.fn();
const {getByText, container}: any = render(
amisRender(
{
type: 'page',
body: [
{
type: 'container',
body: '这里是容器内容区',
onEvent: {
click: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发点击事件'
}
}
]
},
mouseenter: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发鼠标移入事件'
}
}
]
},
mouseleave: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发鼠标移出事件'
}
}
]
}
}
}
]
},
{},
makeEnv({
notify
})
)
);
fireEvent.click(getByText('这里是容器内容区'));
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发点击事件', {
msg: '派发点击事件',
msgType: 'info'
});
});
fireEvent.mouseEnter(getByText('这里是容器内容区'));
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发鼠标移入事件', {
msg: '派发鼠标移入事件',
msgType: 'info'
});
});
fireEvent.mouseLeave(getByText('这里是容器内容区'));
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发鼠标移出事件', {
msg: '派发鼠标移出事件',
msgType: 'info'
});
});
});

View File

@@ -0,0 +1,582 @@
import React = require('react');
import * as renderer from 'react-test-renderer';
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv, replaceReactAriaIds, wait} from '../../helper';
test('doAction:crud reload', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
count: 171,
rows: [
{
engine: 'Trident - pbz7l',
browser: 'Internet Explorer 4.0',
platform: 'Win 95+',
version: '4',
grade: 'X',
badgeText: '默认',
id: 1
},
{
engine: 'Trident - tir4m8',
browser: 'Internet Explorer 5.0',
platform: 'Win 95+',
version: '5',
grade: 'C',
badgeText: '危险',
id: 2
},
{
engine: 'Trident - wcn6f',
browser: 'Internet Explorer 5.5',
platform: 'Win 95+',
version: '5.5',
grade: 'A',
id: 3
},
{
engine: 'Trident - uwmcbf',
browser: 'Internet Explorer 6',
platform: 'Win 98+',
version: '6',
grade: 'A',
id: 4
},
{
engine: 'Trident - yjgst7',
browser: 'Internet Explorer 7',
platform: 'Win XP SP2+',
version: '7',
grade: 'A',
id: 5
},
{
engine: 'Trident - w9ee2k',
browser: 'AOL browser (AOL desktop)',
platform: 'Win XP',
version: '6',
grade: 'A',
id: 6
},
{
engine: 'Gecko - hi6cd',
browser: 'Firefox 1.0',
platform: 'Win 98+ / OSX.2+',
version: '1.7',
grade: 'A',
id: 7
},
{
engine: 'Gecko - 4kxz6',
browser: 'Firefox 1.5',
platform: 'Win 98+ / OSX.2+',
version: '1.8',
grade: 'A',
id: 8
},
{
engine: 'Gecko - x0u91o',
browser: 'Firefox 2.0',
platform: 'Win 98+ / OSX.2+',
version: '1.8',
grade: 'A',
id: 9
},
{
engine: 'Gecko - iou01',
browser: 'Firefox 3.0',
platform: 'Win 2k+ / OSX.3+',
version: '1.9',
grade: 'A',
id: 10
}
]
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
data: {
name: 'amis',
age: 18,
date: '2023-6-6'
},
body: [
{
type: 'button',
label: '刷新CRUD数据加载请求',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'crud_reload',
actionType: 'reload'
}
]
}
}
},
{
type: 'crud',
api: '/api/mock2/sample',
id: 'crud_reload',
syncLocation: false,
columns: [
{
name: 'id',
label: 'ID'
},
{
name: 'engine',
label: 'Rendering engine'
},
{
name: 'browser',
label: 'Browser'
},
{
name: 'platform',
label: 'Platform(s)'
},
{
name: 'version',
label: 'Engine version'
},
{
name: 'grade',
label: 'CSS grade'
},
{
type: 'operation',
label: '操作',
buttons: [
{
label: '详情',
type: 'button',
level: 'link',
actionType: 'dialog',
dialog: {
title: '查看详情',
body: {
type: 'form',
body: [
{
type: 'input-text',
name: 'engine',
label: 'Engine'
},
{
type: 'input-text',
name: 'browser',
label: 'Browser'
},
{
type: 'input-text',
name: 'platform',
label: 'platform'
},
{
type: 'input-text',
name: 'version',
label: 'version'
},
{
type: 'control',
label: 'grade',
body: {
type: 'tag',
label: '${grade}',
displayMode: 'normal',
color: 'active'
}
}
]
}
}
},
{
label: '删除',
type: 'button',
level: 'link',
className: 'text-danger',
disabledOn: "this.grade === 'A'"
}
]
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(getByText('刷新CRUD数据加载请求')).toBeInTheDocument();
});
fireEvent.click(getByText(/刷新CRUD数据加载请求/));
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual(
'/api/mock2/sample?page=1&perPage=10'
);
expect(fetcher.mock.calls[1][0].url).toEqual(
'/api/mock2/sample?page=1&perPage=10'
);
});
await wait(500);
replaceReactAriaIds(container);
expect(container).toMatchSnapshot();
});
test('doAction:crud reload with data1', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
name: 'Amis Renderer',
author: 'fex',
date: 1688714086
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
data: {
name: 'amis',
age: 18,
date: '2023-6-6'
},
body: [
{
type: 'button',
label: '刷新CRUD数据加载请求,同时追加参数date',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'crud_reload',
actionType: 'reload',
data: {
date: '${date}'
}
}
]
}
}
},
{
type: 'crud',
api: '/api/mock2/sample',
id: 'crud_reload',
syncLocation: false,
columns: [
{
name: 'id',
label: 'ID'
},
{
name: 'engine',
label: 'Rendering engine'
},
{
name: 'browser',
label: 'Browser'
},
{
name: 'platform',
label: 'Platform(s)'
},
{
name: 'version',
label: 'Engine version'
},
{
name: 'grade',
label: 'CSS grade'
},
{
type: 'operation',
label: '操作',
buttons: [
{
label: '详情',
type: 'button',
level: 'link',
actionType: 'dialog',
dialog: {
title: '查看详情',
body: {
type: 'form',
body: [
{
type: 'input-text',
name: 'engine',
label: 'Engine'
},
{
type: 'input-text',
name: 'browser',
label: 'Browser'
},
{
type: 'input-text',
name: 'platform',
label: 'platform'
},
{
type: 'input-text',
name: 'version',
label: 'version'
},
{
type: 'control',
label: 'grade',
body: {
type: 'tag',
label: '${grade}',
displayMode: 'normal',
color: 'active'
}
}
]
}
}
},
{
label: '删除',
type: 'button',
level: 'link',
className: 'text-danger',
disabledOn: "this.grade === 'A'"
}
]
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(
getByText('刷新CRUD数据加载请求,同时追加参数date')
).toBeInTheDocument();
});
fireEvent.click(getByText(/刷新CRUD数据加载请求,同时追加参数date/));
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual(
'/api/mock2/sample?page=1&perPage=10'
);
expect(fetcher.mock.calls[1][0].url).toEqual(
'/api/mock2/sample?page=1&date=2023-6-6&perPage=10'
);
});
replaceReactAriaIds(container);
expect(container).toMatchSnapshot();
});
test('doAction:crud reload with data2', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
name: 'Amis Renderer',
author: 'fex',
date: 1688714086
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
data: {
name: 'amis',
age: 18,
date: '2023-6-6'
},
body: [
{
type: 'button',
label: '刷新CRUD数据加载请求,同时追加按钮所在数据域的所有数据',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'crud_reload',
actionType: 'reload',
data: {
'&': '$$',
'author': 'fex'
}
}
]
}
}
},
{
type: 'crud',
api: '/api/mock2/sample',
id: 'crud_reload',
syncLocation: false,
columns: [
{
name: 'id',
label: 'ID'
},
{
name: 'engine',
label: 'Rendering engine'
},
{
name: 'browser',
label: 'Browser'
},
{
name: 'platform',
label: 'Platform(s)'
},
{
name: 'version',
label: 'Engine version'
},
{
name: 'grade',
label: 'CSS grade'
},
{
type: 'operation',
label: '操作',
buttons: [
{
label: '详情',
type: 'button',
level: 'link',
actionType: 'dialog',
dialog: {
title: '查看详情',
body: {
type: 'form',
body: [
{
type: 'input-text',
name: 'engine',
label: 'Engine'
},
{
type: 'input-text',
name: 'browser',
label: 'Browser'
},
{
type: 'input-text',
name: 'platform',
label: 'platform'
},
{
type: 'input-text',
name: 'version',
label: 'version'
},
{
type: 'control',
label: 'grade',
body: {
type: 'tag',
label: '${grade}',
displayMode: 'normal',
color: 'active'
}
}
]
}
}
},
{
label: '删除',
type: 'button',
level: 'link',
className: 'text-danger',
disabledOn: "this.grade === 'A'"
}
]
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(
getByText('刷新CRUD数据加载请求,同时追加按钮所在数据域的所有数据')
).toBeInTheDocument();
});
fireEvent.click(
getByText(/刷新CRUD数据加载请求,同时追加按钮所在数据域的所有数据/)
);
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual(
'/api/mock2/sample?page=1&perPage=10'
);
expect(fetcher.mock.calls[1][0].url).toEqual(
'/api/mock2/sample?page=1&name=amis&age=18&date=2023-6-6&author=fex&perPage=10'
);
});
replaceReactAriaIds(container);
expect(container).toMatchSnapshot();
});

View File

@@ -0,0 +1,428 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`EventAction:inputRange 1`] = `
<div>
<div
class="cxd-Page"
>
<div
class="cxd-Page-content"
>
<div
class="cxd-Page-main"
>
<div
class="cxd-Page-body"
role="page-body"
>
<div
class="cxd-Form-item cxd-Form-item--normal"
data-amis-name="range"
data-role="form-item"
>
<div
class="cxd-RangeControl cxd-InputRange cxd-Form-control"
>
<div
class="cxd-InputRange-wrap"
>
<div
class="cxd-InputRange-track cxd-InputRange-track--background"
>
<div
class="cxd-InputRange-track-active"
style="width: 0%; left: 0%;"
/>
<div
class="cxd-InputRange-handle"
style="left: 0%; z-index: 1;"
>
<div
class="cxd-InputRange-handle-icon"
>
<icon-mock
classname="icon icon-slider-handle"
icon="slider-handle"
/>
</div>
<div
class="cxd-InputRange-label pos-top"
>
<span>
0
</span>
</div>
</div>
</div>
</div>
<div
class="cxd-InputRange-input"
>
<div
class="cxd-Number cxd-Number--borderFull"
>
<div
class="cxd-Number-handler-wrap"
>
<span
aria-disabled="false"
aria-label="Increase Value"
class="cxd-Number-handler cxd-Number-handler-up"
role="button"
unselectable="on"
>
<span
class="cxd-Number-handler-up-inner"
unselectable="on"
/>
</span>
<span
aria-disabled="true"
aria-label="Decrease Value"
class="cxd-Number-handler cxd-Number-handler-down cxd-Number-handler-down-disabled"
role="button"
unselectable="on"
>
<span
class="cxd-Number-handler-down-inner"
unselectable="on"
/>
</span>
</div>
<div
class="cxd-Number-input-wrap"
>
<input
aria-valuemax="100"
aria-valuemin="0"
aria-valuenow="0"
autocomplete="off"
class="cxd-Number-input"
role="spinbutton"
step="1"
value="0"
/>
</div>
</div>
</div>
<a
class="cxd-InputRange-clear"
>
<icon-mock
classname="icon icon-close"
icon="close"
/>
</a>
</div>
</div>
<div
class="cxd-Panel cxd-Panel--default cxd-Panel--form"
data-role="container"
>
<div
class="cxd-Panel-heading"
>
<h3
class="cxd-Panel-title"
>
<span
class="cxd-TplField fr-view"
>
<span>
表单
</span>
</span>
</h3>
</div>
<div
class="cxd-Panel-body"
>
<form
class="cxd-Form cxd-Form--normal"
novalidate=""
>
<input
style="display: none;"
type="submit"
/>
<div
class="cxd-JsonField cxd-Form--debug"
>
<div
class="react-json-view"
style="font-family: monospace; cursor: default; background-color: rgba(0, 0, 0, 0); position: relative;"
>
<div
class="pretty-json-container object-container"
>
<div
class="object-content"
>
<div
class="object-key-val"
>
<span>
<span
style="display: inline-block; cursor: pointer;"
>
<div
class="icon-container"
style="display: inline-block; width: 17px;"
>
<span
class="expanded-icon"
>
<svg
fill="#586e75"
height="1em"
style="vertical-align: middle; color: rgb(88, 110, 117); height: 1em; width: 1em;"
viewBox="0 0 1792 1792"
width="1em"
>
<path
d="M1344 800v64q0 14-9 23t-23 9h-832q-14 0-23-9t-9-23v-64q0-14 9-23t23-9h832q14 0 23 9t9 23zm128 448v-832q0-66-47-113t-113-47h-832q-66 0-113 47t-47 113v832q0 66 47 113t113 47h832q66 0 113-47t47-113zm128-832v832q0 119-84.5 203.5t-203.5 84.5h-832q-119 0-203.5-84.5t-84.5-203.5v-832q0-119 84.5-203.5t203.5-84.5h832q119 0 203.5 84.5t84.5 203.5z"
/>
</svg>
</span>
</div>
<span />
<span
style="display: inline-block; cursor: pointer; font-weight: bold; color: rgb(0, 43, 54);"
>
{
</span>
</span>
<div
class="object-meta-data"
style="display: inline-block; padding: 0px 0px 0px 10px;"
>
<span
class="object-size"
style="color: rgba(0, 0, 0, 0.3); border-radius: 3px; font-style: italic; margin: 0px 6px 0px 0px; cursor: default;"
>
2
item
s
</span>
</div>
</span>
<div
class="pushed-content object-container"
>
<div
class="object-content"
style="margin-left: 6px;"
>
<div
class="variable-row"
style="border-left: 1px solid rgb(235, 235, 235); padding: 3px 5px 3px 20px;"
>
<span>
<span
class="object-key"
style="display: inline-block; color: rgb(0, 43, 54); letter-spacing: 0.5px; font-style: none; vertical-align: top; opacity: 0.85;"
>
<span
style="vertical-align: top;"
>
"
</span>
<span
style="display: inline-block;"
>
rangeValue
</span>
<span
style="vertical-align: top;"
>
"
</span>
</span>
<span
style="display: inline-block; margin: 0px 5px; color: rgb(0, 43, 54); vertical-align: top;"
>
:
</span>
</span>
<div
class="variable-value"
style="display: inline-block; padding-right: 6px; position: relative; cursor: default;"
>
<div
style="display: inline-block; color: rgb(203, 75, 22);"
>
<span
class="string-value"
style="cursor: default;"
>
"
值为0
"
</span>
</div>
</div>
</div>
<div
class="variable-row"
style="border-left: 1px solid rgb(235, 235, 235); padding: 3px 5px 3px 20px;"
>
<span>
<span
class="object-key"
style="display: inline-block; color: rgb(0, 43, 54); letter-spacing: 0.5px; font-style: none; vertical-align: top; opacity: 0.85;"
>
<span
style="vertical-align: top;"
>
"
</span>
<span
style="display: inline-block;"
>
rangeEvent
</span>
<span
style="vertical-align: top;"
>
"
</span>
</span>
<span
style="display: inline-block; margin: 0px 5px; color: rgb(0, 43, 54); vertical-align: top;"
>
:
</span>
</span>
<div
class="variable-value"
style="display: inline-block; padding-right: 6px; position: relative; cursor: default;"
>
<div
style="display: inline-block; color: rgb(203, 75, 22);"
>
<span
class="string-value"
style="cursor: default;"
>
"
触发了blur
"
</span>
</div>
</div>
</div>
</div>
</div>
<span
class="brace-row"
>
<span
style="display: inline-block; cursor: pointer; font-weight: bold; color: rgb(0, 43, 54); padding-left: 3px;"
>
}
</span>
</span>
</div>
</div>
</div>
</div>
</div>
<div
class="cxd-Form-item cxd-Form-item--normal input-text-range-value"
data-amis-name="rangeValue"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
滑块值
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl is-disabled"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
disabled=""
name="rangeValue"
placeholder=""
size="10"
type="text"
value="值为0"
/>
</div>
</div>
</div>
<div
class="cxd-Form-item cxd-Form-item--normal input-text-range-event"
data-amis-name="rangeEvent"
data-role="form-item"
>
<label
class="cxd-Form-label"
>
<span>
<span
class="cxd-TplField fr-view"
>
<span>
滑块事件
</span>
</span>
</span>
</label>
<div
class="cxd-Form-control cxd-TextControl is-disabled"
>
<div
class="cxd-TextControl-input"
>
<input
autocomplete="off"
class=""
disabled=""
name="rangeEvent"
placeholder=""
size="10"
type="text"
value="触发了blur"
/>
</div>
</div>
</div>
</form>
</div>
<div
class="cxd-Panel-footerWrap"
>
<div
class="cxd-Panel-btnToolbar cxd-Panel-footer"
>
<button
class="cxd-Button cxd-Button--primary cxd-Button--size-default"
type="submit"
>
<span>
提交
</span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;

View File

@@ -0,0 +1,225 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../../src';
import {render as amisRender} from '../../../../src';
import {makeEnv, wait} from '../../../helper';
test('EventAction:combo', async () => {
// const notify = jest.fn();
// const {getByText, container}: any = render(
// amisRender(
// {
// type: 'page',
// title: 'combo',
// body: [
// {
// type: 'form',
// id: 'u:71e9bb1ed157',
// title: '表单',
// data: {
// combo: [
// {
// name: 'Tom',
// age: 18
// },
// {
// name: 'Jerry',
// age: 22
// }
// ]
// },
// body: [
// {
// type: 'button',
// label: '更新数据',
// onEvent: {
// click: {
// actions: [
// {
// actionType: 'setValue',
// componentId: 'u:54031932ab8c',
// args: {
// value: [
// {
// name: 'Jack',
// age: 22
// }
// ]
// }
// }
// ]
// }
// }
// },
// {
// type: 'button',
// label: 'clear',
// onEvent: {
// click: {
// actions: [
// {
// actionType: 'clear',
// componentId: 'u:54031932ab8c'
// }
// ]
// }
// }
// },
// {
// type: 'button',
// label: 'reset',
// onEvent: {
// click: {
// actions: [
// {
// actionType: 'reset',
// componentId: 'u:54031932ab8c'
// }
// ]
// }
// }
// },
// {
// type: 'combo',
// label: '组合输入',
// name: 'combo',
// id: 'u:54031932ab8c',
// multiple: true,
// tabsMode: true,
// resetValue: [
// {
// name: 'Tom',
// age: 18
// }
// ],
// items: [
// {
// type: 'input-text',
// name: 'name'
// },
// {
// type: 'input-text',
// name: 'age'
// },
// {
// type: 'button',
// label: '自增第${index}项的年龄',
// onEvent: {
// click: {
// actions: [
// {
// actionType: 'setValue',
// componentId: 'u:54031932ab8c',
// args: {
// value: {
// age: '${age+1}'
// },
// index: '${index}'
// }
// }
// ]
// }
// }
// }
// ],
// onEvent: {
// add: {
// actions: [
// {
// actionType: 'toast',
// args: {
// msgType: 'info',
// msg: '派发新增事件'
// }
// }
// ]
// },
// delete: {
// actions: [
// {
// actionType: 'toast',
// args: {
// msgType: 'info',
// msg: '派发删除事件'
// }
// }
// ]
// },
// tabsChange: {
// actions: [
// {
// actionType: 'toast',
// args: {
// msgType: 'info',
// msg: '派发选项卡切换事件'
// }
// }
// ]
// }
// }
// }
// ]
// }
// ]
// },
// {},
// makeEnv({
// notify
// })
// )
// );
// await waitFor(() => {
// expect(getByText('新增')).toBeInTheDocument();
// });
// fireEvent.click(getByText('新增'));
// await waitFor(() => {
// expect(notify).toHaveBeenCalledWith('info', '派发新增事件', {
// msg: '派发新增事件',
// msgType: 'info'
// });
// });
// fireEvent.click(
// container.querySelectorAll(
// 'div[data-tooltip="删除"]'
// )[1] as HTMLInputElement
// );
// await waitFor(() => {
// expect(notify).toHaveBeenCalledWith('info', '派发删除事件', {
// msg: '派发删除事件',
// msgType: 'info'
// });
// expect(notify).toHaveBeenCalledWith('info', '派发选项卡切换事件', {
// msg: '派发选项卡切换事件',
// msgType: 'info'
// });
// });
// await waitFor(() => {
// expect(getByText('自增第0项的年龄')).toBeInTheDocument();
// });
// fireEvent.click(getByText('自增第0项的年龄'));
// await waitFor(() => {
// expect(
// container.querySelector('input[name=age]')?.getAttribute('value')
// ).toBe('19');
// });
// await waitFor(() => {
// expect(getByText('更新数据')).toBeInTheDocument();
// });
// fireEvent.click(getByText('更新数据'));
// await waitFor(() => {
// expect(
// container.querySelector('input[name=name]')?.getAttribute('value')
// ).toBe('Jack');
// });
// fireEvent.click(getByText('reset'));
// await waitFor(() => {
// expect(
// container.querySelector('input[name=name]')?.getAttribute('value')
// ).toBe('Tom');
// });
// fireEvent.click(getByText('clear'));
// await waitFor(() => {
// expect(
// container.querySelector('div[class="cxd-Tabs-content"]')
// ).toBeEmptyDOMElement();
// });
});

View File

@@ -0,0 +1,960 @@
import React = require('react');
import * as renderer from 'react-test-renderer';
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../../src';
import {render as amisRender} from '../../../../src';
import {makeEnv, wait} from '../../../helper';
test('doAction:form validate', async () => {
const notify = jest.fn();
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '校验表单',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
actionType: 'validate',
componentId: 'form_validate',
outputVar: 'form_validate_result'
},
{
actionType: 'setValue',
componentId: 'validate_info',
args: {
value: '${event.data.form_validate_result|json}'
}
}
]
}
}
},
{
type: 'input-text',
name: 'validate_info',
id: 'validate_info',
label: '验证信息:'
},
{
type: 'form',
id: 'form_validate',
api: '/api/mock2/form/saveForm',
body: [
{
type: 'input-text',
name: 'name',
label: '姓名:',
required: true
},
{
name: 'email',
type: 'input-text',
label: '邮箱:',
required: true,
validations: {
isEmail: true
}
}
]
}
]
},
{},
makeEnv({
notify
})
)
);
await waitFor(() => {
expect(getByText('校验表单')).toBeInTheDocument();
});
fireEvent.click(getByText(/校验表单/));
await wait(300);
await waitFor(() => {
expect(
(container.querySelector('[name="validate_info"]') as any)?.value
).toEqual(
`{ "error": "依赖的部分字段没有通过验证", "errors": { "name": [ "这是必填项" ], "email": [ "这是必填项" ] }, "payload": {}}`
);
});
expect(container).toMatchSnapshot();
fireEvent.change(container.querySelector('[name="name"]')!, {
target: {value: 'amis'}
});
fireEvent.change(container.querySelector('[name="email"]')!, {
target: {value: 'amis@baidu.com'}
});
await waitFor(() => {
expect(container.querySelector('[value="amis"]')).toBeInTheDocument();
expect(
container.querySelector('[value="amis@baidu.com"]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
fireEvent.change(container.querySelector('[name="validate_info"]')!, {
target: {value: ''}
});
await waitFor(() => {
expect(
(container.querySelector('[name="validate_info"]') as any)?.value
).toEqual('');
});
fireEvent.click(getByText(/校验表单/));
await waitFor(() => {
expect(
container.querySelector('.is-error .is-required .has-error--isRequired')
).not.toBeInTheDocument();
});
await wait(300);
await waitFor(() => {
expect(
(container.querySelector('[name="validate_info"]') as any)?.value
).toEqual(
`{ "error": "", "payload": { "name": "amis", "email": "amis@baidu.com" }}`
);
});
expect(container).toMatchSnapshot();
});
test('doAction:form submit', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
id: 1
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '提交表单',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
actionType: 'submit',
componentId: 'form_submit',
outputVar: 'form_submit_result'
},
{
actionType: 'setValue',
componentId: 'submit_info',
args: {
value: '${event.data.form_submit_result|json}'
}
}
]
}
}
},
{
type: 'input-text',
name: 'submit_info',
id: 'submit_info',
label: '提交信息:'
},
{
type: 'form',
id: 'form_submit',
api: '/api/mock2/form/saveForm',
body: [
{
type: 'input-text',
name: 'name',
label: '姓名:',
required: true
},
{
name: 'email',
type: 'input-text',
label: '邮箱:',
required: true,
validations: {
isEmail: true
}
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(getByText('提交表单')).toBeInTheDocument();
});
fireEvent.click(getByText(/提交表单/));
await wait(300);
await waitFor(() => {
expect(
(container.querySelector('[name="submit_info"]') as any)?.value
).toEqual(
`{ "error": "依赖的部分字段没有通过验证", "errors": { "name": [ "这是必填项" ], "email": [ "这是必填项" ] }, "payload": {}}`
);
});
expect(container).toMatchSnapshot();
fireEvent.change(container.querySelector('[name="name"]')!, {
target: {value: 'amis'}
});
fireEvent.change(container.querySelector('[name="email"]')!, {
target: {value: 'amis@baidu.com'}
});
await waitFor(() => {
expect(container.querySelector('[value="amis"]')).toBeInTheDocument();
expect(
container.querySelector('[value="amis@baidu.com"]')
).toBeInTheDocument();
});
fireEvent.change(container.querySelector('[name="submit_info"]')!, {
target: {value: ''}
});
await waitFor(() => {
expect(
(container.querySelector('[name="submit_info"]') as any)?.value
).toEqual('');
});
expect(container).toMatchSnapshot();
fireEvent.click(getByText(/提交表单/));
await waitFor(() => {
expect(
container.querySelector('.is-error .is-required .has-error--isRequired')
).not.toBeInTheDocument();
});
await wait(300);
await waitFor(() => {
expect(
(container.querySelector('[name="submit_info"]') as any)?.value
).toEqual(
`{ "error": "", "payload": { "name": "amis", "email": "amis@baidu.com" }, "responseData": { "id": 1 }}`
);
});
expect(container).toMatchSnapshot();
});
test('doAction:form force validate before submit', async () => {
const notify = jest.fn();
const {container, getByText, queryByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'form',
body: [
{
type: 'input-text',
name: 'value1',
validations: {
equalsField: 'value2'
},
value: 1
},
{
name: 'value2',
type: 'input-text',
validations: {
equalsField: 'value1'
}
}
]
}
]
},
{},
makeEnv({
notify
})
)
);
await waitFor(() => {
expect(getByText('提交')).toBeInTheDocument();
});
fireEvent.click(getByText(/提交/));
await wait(300);
await waitFor(() => {
expect(queryByText('输入的数据与 value2 值不一致')).toBeInTheDocument();
expect(queryByText('输入的数据与 value1 值不一致')).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
fireEvent.change(container.querySelector('[name="value2"]')!, {
target: {value: '1'}
});
await wait(300);
await waitFor(() => {
expect(queryByText('输入的数据与 value1 值不一致')).not.toBeInTheDocument();
});
fireEvent.click(getByText(/提交/));
await wait(300);
await waitFor(() => {
expect(queryByText('输入的数据与 value2 值不一致')).not.toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:form setValue', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
id: 1
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '修改表单数据',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
actionType: 'setValue',
componentId: 'form_setvalue',
args: {
value: {
name: 'amis',
email: 'amis@baidu.com'
}
}
}
]
}
}
},
{
type: 'form',
id: 'form_setvalue',
body: [
{
type: 'input-text',
name: 'name',
label: '姓名:'
},
{
name: 'email',
type: 'input-text',
label: '邮箱:'
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(getByText('修改表单数据')).toBeInTheDocument();
});
fireEvent.click(getByText(/修改表单数据/));
await waitFor(() => {
expect(container.querySelector('[value="amis"]')).toBeInTheDocument();
expect(
container.querySelector('[value="amis@baidu.com"]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:form reload default', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
name: 'Amis Renderer',
author: 'fex',
date: 1688714086
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '刷新表单',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
actionType: 'reload',
componentId: 'form_reload'
}
]
}
}
},
{
type: 'form',
id: 'form_reload',
debug: true,
initApi: '/api/mock2/form/initData',
body: [
{
type: 'input-text',
name: 'name',
label: '姓名:'
},
{
name: 'author',
type: 'input-text',
label: '作者:'
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await wait(500); // 等待 initApi 加载完
expect(
(container.querySelector('[name="author"]') as HTMLInputElement).value
).toEqual('fex');
const author: HTMLInputElement = container.querySelector('[name="author"]')!;
fireEvent.change(author, {
target: {value: 'amis'}
});
expect(
(container.querySelector('[name="author"]') as HTMLInputElement).value
).toEqual('amis');
// expect(container).toMatchSnapshot();
await wait(200);
expect(getByText('刷新表单')).toBeInTheDocument();
fireEvent.click(getByText('刷新表单'));
await wait(200);
expect(
(container.querySelector('[name="author"]') as HTMLInputElement).value
).toEqual('fex');
// expect(container).toMatchSnapshot();
});
test('doAction:form reload with data', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
name: 'Amis Renderer',
author: 'fex',
date: 1688714086
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '刷新表单',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
actionType: 'reload',
componentId: 'form_reload',
data: {
age: 18
}
}
]
}
}
},
{
type: 'form',
id: 'form_reload',
debug: true,
initApi: '/api/mock2/form/initData',
body: [
{
type: 'input-text',
name: 'name',
label: '姓名:'
},
{
name: 'author',
type: 'input-text',
label: '作者:'
},
{
name: 'age',
type: 'input-text',
label: '年龄:'
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await wait(500);
const author: HTMLInputElement = container.querySelector('[name="author"]')!;
expect(author).toBeInTheDocument();
fireEvent.change(author, {
target: {value: 'amis'}
});
await wait(200);
expect(author.value).toEqual('amis');
expect(getByText('刷新表单')).toBeInTheDocument();
fireEvent.click(getByText('刷新表单'));
await wait(200);
expect(author.value).toEqual('fex');
expect((container.querySelector('[name="age"]') as any)?.value).toEqual('18');
});
test('doAction:form reset', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
name: 'Amis Renderer',
author: 'fex',
date: 1688714086
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '重置表单',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
actionType: 'reset',
componentId: 'form_reset'
}
]
}
}
},
{
type: 'form',
id: 'form_reset',
initApi: '/api/mock2/form/initData',
body: [
{
type: 'input-text',
name: 'name',
label: '姓名:'
},
{
name: 'author',
type: 'input-text',
label: '作者:'
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
fireEvent.change(container.querySelector('[name="author"]')!, {
target: {value: 'amis'}
});
await waitFor(() => {
expect((container.querySelector('[name="author"]') as any)?.value).toEqual(
'amis'
);
});
expect(container).toMatchSnapshot();
await waitFor(() => {
expect(getByText('重置表单')).toBeInTheDocument();
});
fireEvent.click(getByText(/重置表单/));
await waitFor(() => {
expect((container.querySelector('[name="author"]') as any)?.value).toEqual(
'fex'
);
});
expect(container).toMatchSnapshot();
});
test('doAction:form clear', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
name: 'Amis Renderer',
author: 'fex',
date: 1688714086
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '清空表单',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
actionType: 'clear',
componentId: 'form_clear'
}
]
}
}
},
{
type: 'form',
id: 'form_clear',
debug: true,
initApi: '/api/mock2/form/initData',
body: [
{
type: 'input-text',
name: 'name',
label: '姓名:'
},
{
name: 'author',
type: 'hidden',
label: '作者:'
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await wait(500);
await waitFor(() => {
expect(getByText('清空表单')).toBeInTheDocument();
});
fireEvent.click(getByText(/清空表单/));
await waitFor(() => {
expect(
container.querySelector('[value="Amis Renderer"]')
).not.toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:form static&nonstatic', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
name: 'Amis Renderer',
author: 'fex',
date: 1688714086
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '静态模式',
className: 'mr-2 mb-2',
onEvent: {
click: {
actions: [
{
actionType: 'static',
componentId: 'form_static'
}
]
}
}
},
{
type: 'button',
label: '编辑模式',
className: 'mr-2 mb-2',
onEvent: {
click: {
actions: [
{
actionType: 'nonstatic',
componentId: 'form_static'
}
]
}
}
},
{
type: 'form',
id: 'form_static',
title: '表单',
body: [
{
type: 'input-text',
name: 'text',
label: '输入框',
mode: 'horizontal',
value: 'text'
}
]
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(getByText('静态模式')).toBeInTheDocument();
});
fireEvent.click(getByText(/静态模式/));
await waitFor(() => {
expect(container.querySelector('.cxd-Form-static')).toBeInTheDocument();
expect(
container.querySelector('.cxd-TextControl-input')
).not.toBeInTheDocument();
});
expect(container).toMatchSnapshot();
await waitFor(() => {
expect(getByText('编辑模式')).toBeInTheDocument();
});
fireEvent.click(getByText(/编辑模式/));
await waitFor(() => {
expect(
container.querySelector('.cxd-TextControl-input')
).toBeInTheDocument();
expect(container.querySelector('.cxd-Form-static')).not.toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:form valdiate requiredOn', async () => {
const onSubmit = jest.fn();
const {container, getByText} = render(
amisRender(
{
type: 'form',
submitText: '提交表单',
body: [
{
type: 'switch',
name: 'a',
label: 'a'
},
{
type: 'input-text',
name: 'b',
requiredOn: '${a}',
label: 'b'
}
]
},
{
onSubmit
},
makeEnv({})
)
);
fireEvent.click(getByText('提交表单'));
await wait(200);
expect(onSubmit).toBeCalled();
expect(container.querySelector('.cxd-Switch')!).toBeInTheDocument();
fireEvent.click(container.querySelector('.cxd-Switch')!);
await wait(300);
fireEvent.click(getByText('提交表单'));
await wait(300);
const ul = container.querySelector('.cxd-Form-feedback');
expect(ul).toBeInTheDocument();
fireEvent.change(container.querySelector('input[name="b"]')!, {
target: {value: '123'}
});
fireEvent.click(getByText('提交表单'));
await wait(200);
expect(onSubmit).toBeCalledTimes(2);
expect(onSubmit.mock.calls[1][0]).toMatchObject({
a: true,
b: '123'
});
expect(container.querySelector('.cxd-Form-feedback')).toBeNull();
fireEvent.change(container.querySelector('input[name="b"]')!, {
target: {value: ''}
});
await wait(300);
expect(container.querySelector('.cxd-Form-feedback')).toBeInTheDocument();
fireEvent.click(container.querySelector('.cxd-Switch')!);
await wait(300);
expect(container.querySelector('.cxd-Form-feedback')).toBeNull();
});

View File

@@ -0,0 +1,119 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../../src';
import {render as amisRender} from '../../../../src';
import {makeEnv, wait} from '../../../helper';
test('EventAction:locationPicker', async () => {
const notify = jest.fn();
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'location-picker',
id: 'location-picker',
ak: 'LiZT5dVbGTsPI91tFGcOlSpe5FDehpf7',
coordinatesType: 'bd09',
name: 'location',
resetValue: {
address: '北京市东城区天安门广场中央',
lat: 39.910966,
lng: 116.404165,
city: '北京'
},
onEvent: {
change: {
actions: [
{
actionType: 'setValue',
componentId: 'form',
args: {
value: {
location: '${event.data.value | json}'
}
}
}
]
}
}
},
{
type: 'form',
id: 'form',
debug: true,
body: [
{
type: 'input-text',
label: '位置信息:',
name: 'location',
id: 'input-text'
}
]
},
{
type: 'button',
label: '赋值',
id: 'u:d2662e693f82',
className: 'button-set-value',
onEvent: {
click: {
actions: [
{
actionType: 'setValue',
args: {
value: {
address: '北京市东城区天安门内',
lat: 39.914912,
lng: 116.403982,
city: '北京'
}
},
componentId: 'location-picker'
}
]
}
}
},
{
type: 'button',
label: '重置',
id: 'u:a1d92fc92cbb',
className: 'button-reset',
onEvent: {
click: {
actions: [
{
actionType: 'reset',
componentId: 'location-picker'
}
]
}
}
}
]
},
{},
makeEnv({
notify
})
)
);
await wait(500);
const buttonSetValue = container.querySelector('.button-set-value');
fireEvent.click(buttonSetValue!);
await wait(300);
await waitFor(() => {
expect(getByText('北京市东城区天安门内')).toBeInTheDocument();
});
const buttonReset = container.querySelector('.button-reset');
fireEvent.click(buttonReset!);
await wait(300);
await waitFor(() => {
expect(getByText('北京市东城区天安门广场中央')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,167 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
// import {fireEvent as fireEvent2} from '@testing-library/dom';
import '../../../../src';
import {render as amisRender} from '../../../../src';
import {makeEnv, wait} from '../../../helper';
test('EventAction:inputRange', async () => {
const notify = jest.fn();
const {container} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'input-range',
name: 'range',
value: 10,
min: 0,
showInput: true,
clearabled: true,
onEvent: {
change: {
actions: [
{
actionType: 'setValue',
componentId: 'form_data',
args: {
value: {
rangeEvent: '触发了change',
rangeValue: '值为${range}'
}
}
}
]
},
blur: {
actions: [
{
actionType: 'setValue',
componentId: 'form_data',
args: {
value: {
rangeEvent: '触发了blur',
rangeValue: '值为${range}'
}
}
}
]
},
focus: {
actions: [
{
actionType: 'setValue',
componentId: 'form_data',
args: {
value: {
rangeEvent: '触发了focus',
rangeValue: '值为${range}'
}
}
}
]
}
}
},
{
type: 'form',
id: 'form_data',
title: '表单',
debug: true,
data: {
rangeValue: '什么都没有',
rangeEvent: '什么都没触发'
},
body: [
{
type: 'input-text',
label: '滑块值',
name: 'rangeValue',
disabled: true,
className: 'input-text-range-value'
},
{
type: 'input-text',
label: '滑块事件',
name: 'rangeEvent',
disabled: true,
className: 'input-text-range-event'
}
]
}
]
},
{},
makeEnv({
notify
})
)
);
await wait(500);
const inputs = container.querySelector('.cxd-InputRange-input input')!;
// input change
const valueChange = 23;
fireEvent.change(inputs, {
target: {
value: valueChange
}
});
await wait(300);
await waitFor(() => {
expect(
container.querySelector('[value="触发了change"]')
).toBeInTheDocument();
expect(
container.querySelector(`[value="值为${valueChange}"]`)
).toBeInTheDocument();
});
/**
* 滑动 change case先注释掉,因为fireEvent.mouseMove时,event中拿不到pageX。
// 滑动 change
const slider = container.querySelector('.cxd-InputRange-handle-icon')!;
fireEvent.mouseDown(slider);
fireEvent.mouseMove(slider, {
clientX: 0,
clientY: 0
});
fireEvent.mouseUp(slider);
await wait(300);
await waitFor(() => {
expect(
container.querySelector('[value="触发了change"]')
).toBeInTheDocument();
expect(container.querySelector(`[value="值为0"]`)).toBeInTheDocument();
});
*/
// 临时替代滑动 change case
fireEvent.change(inputs, {
target: {
value: 0
}
});
await wait(300);
// focus
fireEvent.focus(inputs);
await wait(300);
await waitFor(() => {
expect(
container.querySelector('[value="触发了focus"]')
).toBeInTheDocument();
expect(container.querySelector(`[value="值为0"]`)).toBeInTheDocument();
});
// blur
fireEvent.blur(inputs);
await wait(300);
await waitFor(() => {
expect(container.querySelector('[value="触发了blur"]')).toBeInTheDocument();
expect(container.querySelector(`[value="值为0"]`)).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
}, 10000);

View File

@@ -0,0 +1,102 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../../src';
import {render as amisRender} from '../../../../src';
import {makeEnv, wait} from '../../../helper';
test('EventAction:switch', async () => {
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
id: 1
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '修改开关的值',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'u:6613bfa3a18e',
actionType: 'setValue',
args: {
value: true
}
}
]
}
},
id: 'u:9d7d695145bb'
},
{
type: 'form',
title: '表单',
debug: true,
body: [
{
label: '开启',
type: 'switch',
name: 'switch',
id: 'u:6613bfa3a18e',
value: false,
mode: 'inline',
onEvent: {
change: {
actions: [
{
actionType: 'ajax',
api: '/api/mock2/form/saveForm?switch=${switch}'
}
]
}
}
}
],
id: 'u:82d44e407eb0',
actions: [
{
type: 'submit',
label: '提交',
primary: true
}
]
}
]
},
{},
makeEnv({
fetcher
})
)
);
await waitFor(() => {
expect(getByText('修改开关的值')).toBeInTheDocument();
});
fireEvent.click(getByText(/修改开关的值/));
await waitFor(() => {
expect(container.querySelector('.is-checked')).toBeInTheDocument();
});
fireEvent.click(container.querySelector('.is-checked')!);
await waitFor(() => {
expect(fetcher).toHaveBeenCalled();
expect(fetcher.mock.calls[0][0].url).toEqual(
'/api/mock2/form/saveForm?switch=false'
);
});
});

View File

@@ -0,0 +1,290 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../../src';
import {render as amisRender} from '../../../../src';
import {makeEnv, wait} from '../../../helper';
test('doAction:formItem validate', async () => {
const notify = jest.fn();
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '校验name',
onEvent: {
click: {
actions: [
{
actionType: 'validateFormItem',
componentId: 'name_validate',
outputVar: 'form_validate_result'
},
{
actionType: 'setValue',
componentId: 'validate_info',
args: {
value: '${event.data.form_validate_result|json}'
}
}
]
}
}
},
{
type: 'button',
label: '校验email',
onEvent: {
click: {
actions: [
{
actionType: 'validateFormItem',
componentId: 'email_validate',
outputVar: 'form_validate_result'
},
{
actionType: 'setValue',
componentId: 'validate_info',
args: {
value: '${event.data.form_validate_result|json}'
}
}
]
}
}
},
{
type: 'input-text',
name: 'validate_info',
id: 'validate_info',
label: '验证信息:'
},
{
type: 'input-text',
name: 'validate_res',
id: 'validate_res',
label: '验证结果:'
},
{
type: 'form',
onEvent: {
formItemValidateSucc: {
actions: [
{
expression: '${event.data.__formName === "name"}',
actionType: 'setValue',
componentId: 'validate_res',
args: {
value: 'validate name success'
}
},
{
expression: '${event.data.__formName === "email"}',
actionType: 'setValue',
componentId: 'validate_res',
args: {
value: 'validate email success'
}
}
]
},
formItemValidateError: {
actions: [
{
expression: '${event.data.__formName === "name"}',
actionType: 'setValue',
componentId: 'validate_res',
args: {
value: 'validate name fail'
}
},
{
expression: '${event.data.__formName === "email"}',
actionType: 'setValue',
componentId: 'validate_res',
args: {
value: 'validate email fail'
}
}
]
}
},
body: [
{
type: 'input-text',
id: 'name_validate',
name: 'name',
label: '姓名:',
required: true
},
{
name: 'email',
type: 'input-text',
id: 'email_validate',
label: '邮箱:',
required: true,
validations: {
isEmail: true
}
}
]
}
]
},
{},
makeEnv({
notify
})
)
);
// 校验 name
await waitFor(() => {
expect(getByText('校验name')).toBeInTheDocument();
});
fireEvent.click(getByText(/校验name/));
await wait(300);
await waitFor(() => {
expect(
(container.querySelector('[name="validate_info"]') as any)?.value
).toEqual(`{ "error": "这是必填项"}`);
});
await waitFor(() => {
expect(
(container.querySelector('[name="validate_res"]') as any)?.value
).toEqual('validate name fail');
});
expect(container).toMatchSnapshot();
// 校验 email 必填
await wait(300);
await waitFor(() => {
expect(getByText('校验email')).toBeInTheDocument();
});
fireEvent.click(getByText(/校验email/));
await wait(300);
await waitFor(() => {
expect(
(container.querySelector('[name="validate_info"]') as any)?.value
).toEqual(`{ "error": "这是必填项"}`);
});
await waitFor(() => {
expect(
(container.querySelector('[name="validate_res"]') as any)?.value
).toEqual('validate email fail');
});
expect(container).toMatchSnapshot();
// 校验 email 格式
fireEvent.change(container.querySelector('[name="email"]')!, {
target: {value: 'invalid_email'}
});
await wait(300);
await waitFor(() => {
expect(
container.querySelector('[name="email"][value="invalid_email"]')
).toBeInTheDocument();
});
await waitFor(() => {
expect(getByText('校验email')).toBeInTheDocument();
});
fireEvent.click(getByText(/校验email/));
await wait(300);
await waitFor(() => {
expect(
(container.querySelector('[name="validate_info"]') as any)?.value
).toEqual(`{ "error": "Email 格式不正确", "value": "invalid_email"}`);
});
await waitFor(() => {
expect(
(container.querySelector('[name="validate_res"]') as any)?.value
).toEqual('validate email fail');
});
expect(container).toMatchSnapshot();
// 填写值
fireEvent.change(container.querySelector('[name="name"]')!, {
target: {value: 'amis'}
});
await wait(300);
fireEvent.change(container.querySelector('[name="email"]')!, {
target: {value: 'amis@baidu.com'}
});
await wait(300);
await waitFor(() => {
expect(container.querySelector('[value="amis"]')).toBeInTheDocument();
expect(
container.querySelector('[value="amis@baidu.com"]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
// 清空错误信息
fireEvent.change(container.querySelector('[name="validate_info"]')!, {
target: {value: ''}
});
await wait(300);
await waitFor(() => {
expect(
(container.querySelector('[name="validate_info"]') as any)?.value
).toEqual('');
});
// 重新校验name
fireEvent.click(getByText(/校验name/));
await wait(300);
await waitFor(() => {
expect(
container.querySelector('.is-error .is-required .has-error--isRequired')
).not.toBeInTheDocument();
});
await waitFor(() => {
expect(
(container.querySelector('[name="validate_info"]') as any)?.value
).toEqual(`{ "error": "", "value": "amis"}`);
});
await waitFor(() => {
expect(
(container.querySelector('[name="validate_res"]') as any)?.value
).toEqual('validate name success');
});
expect(container).toMatchSnapshot();
// 重新校验email
fireEvent.click(getByText(/校验email/));
await wait(300);
await waitFor(() => {
expect(
container.querySelector('.is-error .is-required .has-error--isRequired')
).not.toBeInTheDocument();
});
await waitFor(() => {
expect(
(container.querySelector('[name="validate_info"]') as any)?.value
).toEqual(`{ "error": "", "value": "amis@baidu.com"}`);
});
await waitFor(() => {
expect(
(container.querySelector('[name="validate_res"]') as any)?.value
).toEqual('validate email success');
});
expect(container).toMatchSnapshot();
}, 10000);

View File

@@ -0,0 +1,87 @@
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv} from '../../helper';
test('EventAction:icon', async () => {
const notify = jest.fn();
const {getByText, container}: any = render(
amisRender(
{
type: 'page',
data: {
btnDisabled: true,
btnNotDisabled: false
},
body: [
{
type: 'icon',
icon: 'cloud',
onEvent: {
click: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发点击事件'
}
}
]
},
mouseenter: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发鼠标移入事件'
}
}
]
},
mouseleave: {
actions: [
{
actionType: 'toast',
args: {
msgType: 'info',
msg: '派发鼠标移出事件'
}
}
]
}
}
}
]
},
{},
makeEnv({
notify
})
)
);
const icon = container.querySelector('.cloud');
fireEvent.click(icon);
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发点击事件', {
msg: '派发点击事件',
msgType: 'info'
});
});
fireEvent.mouseEnter(icon);
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发鼠标移入事件', {
msg: '派发鼠标移入事件',
msgType: 'info'
});
});
fireEvent.mouseLeave(icon);
await waitFor(() => {
expect(notify).toHaveBeenCalledWith('info', '派发鼠标移出事件', {
msg: '派发鼠标移出事件',
msgType: 'info'
});
});
});

View File

@@ -0,0 +1,285 @@
import React = require('react');
import * as renderer from 'react-test-renderer';
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv} from '../../helper';
test('doAction:page reload', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
title: 'Test Page Component',
date: '2017-10-13'
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
initApi: '/api/mock2/page/initData',
id: 'page_reload',
body: [
{
type: 'button',
label: '刷新Page数据加载请求',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'page_reload',
actionType: 'reload'
}
]
}
}
},
{
type: 'input-text',
label: '我的标题:',
name: 'title'
},
{
type: 'input-text',
label: '我的年龄:',
name: 'age'
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(getByText('刷新Page数据加载请求')).toBeInTheDocument();
expect(
container.querySelector('[name="title"][value="Test Page Component"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[data-testid="spinner"]')
).not.toBeInTheDocument();
});
expect(container).toMatchSnapshot();
fireEvent.click(getByText(/刷新Page数据加载请求/));
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual('/api/mock2/page/initData');
expect(fetcher.mock.calls[1][0].url).toEqual('/api/mock2/page/initData');
expect(
container.querySelector('[name="title"][value="Test Page Component"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value=""]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:page reload with data', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
title: 'Test Page Component',
date: '2017-10-13'
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
initApi: '/api/mock2/page/initData',
id: 'page_reload',
body: [
{
type: 'button',
label: '刷新Page数据加载请求,同时把年龄更新为18',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'page_reload',
actionType: 'reload',
data: {
age: '18'
}
}
]
}
}
},
{
type: 'input-text',
label: '我的标题:',
name: 'title'
},
{
type: 'input-text',
label: '我的年龄:',
name: 'age'
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(
getByText('刷新Page数据加载请求,同时把年龄更新为18')
).toBeInTheDocument();
expect(
container.querySelector('[name="title"][value="Test Page Component"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[data-testid="spinner"]')
).not.toBeInTheDocument();
});
expect(container).toMatchSnapshot();
fireEvent.click(getByText(/刷新Page数据加载请求,同时把年龄更新为18/));
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual('/api/mock2/page/initData');
expect(fetcher.mock.calls[1][0].url).toEqual('/api/mock2/page/initData');
expect(
container.querySelector('[name="title"][value="Test Page Component"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value="18"]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:page reload with data(same name)', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
title: 'Test Page Component',
date: '2017-10-13'
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
initApi: '/api/mock2/page/initData',
id: 'page_reload',
body: [
{
type: 'button',
label: '刷新Page数据加载请求,请求返回的date会覆盖动作传递的date',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'page_reload',
actionType: 'reload',
data: {
date: '2023-07-07'
}
}
]
}
}
},
{
type: 'input-text',
label: '我的标题:',
name: 'title'
},
{
type: 'input-text',
label: '日期:',
name: 'date'
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(
getByText('刷新Page数据加载请求,请求返回的date会覆盖动作传递的date')
).toBeInTheDocument();
expect(
container.querySelector('[name="title"][value="Test Page Component"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="date"][value="2017-10-13"]')
).toBeInTheDocument();
expect(
container.querySelector('[data-testid="spinner"]')
).not.toBeInTheDocument();
});
expect(container).toMatchSnapshot();
fireEvent.click(
getByText(/刷新Page数据加载请求,请求返回的date会覆盖动作传递的date/)
);
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual('/api/mock2/page/initData');
expect(fetcher.mock.calls[1][0].url).toEqual('/api/mock2/page/initData');
expect(
container.querySelector('[name="title"][value="Test Page Component"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="date"][value="2017-10-13"]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});

View File

@@ -0,0 +1,159 @@
import {fireEvent, render} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv, wait} from '../../helper';
test('pagination: pageNum change event', async () => {
const mockFn = jest.fn();
const pageChange = jest.fn();
const {container} = render(
amisRender(
{
type: 'pagination',
layout: 'total,perPage,pager,go',
mode: 'normal',
activePage: 2,
lastPage: 99999,
total: 999,
perPage: 10,
maxButtons: 7,
showPerPage: true,
perPageAvailable: [10, 20, 50, 100],
showPageInput: true,
disabled: false,
id: 'u:1bf323bc4dbd',
onPageChange: pageChange,
onEvent: {
change: {
weight: 0,
actions: [
{
actionType: 'custom',
script: mockFn
}
]
}
}
},
{},
makeEnv({})
)
);
// 当前页码改变
const prev = container.querySelector('.cxd-Pagination-prev')!;
fireEvent.click(prev); // 上一页
await wait(100);
expect(mockFn).toBeCalledTimes(1);
await wait(200);
expect(pageChange).toBeCalled();
expect(pageChange.mock.calls[0]).toEqual([1, 10]);
const next = container.querySelector('.cxd-Pagination-next')!;
fireEvent.click(next); // 下一页
await wait(300);
expect(mockFn).toBeCalledTimes(2);
await wait(200);
expect(pageChange).toBeCalled();
expect(pageChange.mock.calls[1]).toEqual([3, 10]);
const go = container.querySelector('.cxd-Pagination-inputGroup')!;
fireEvent.change(go.querySelector('.cxd-Pagination-inputGroup-input')!, {
target: {value: 9}
});
await wait(400);
fireEvent.click(go.querySelector('.cxd-Pagination-inputGroup-right')!); // 输入后点击go
await wait(500);
expect(mockFn).toBeCalledTimes(3);
await wait(200);
expect(pageChange).toBeCalled();
expect(pageChange.mock.calls[2]).toEqual([9, 10]);
function getPagerItem() {
const pager = container.querySelectorAll(
'.cxd-Pagination > .cxd-Pagination-pager-item'
);
return pager[3];
}
fireEvent.click(getPagerItem()!); // 点击页码切换
await wait(600);
expect(mockFn).toBeCalledTimes(4);
await wait(200);
expect(pageChange).toBeCalled();
expect(pageChange.mock.calls[3]).toEqual([4, 10]);
});
test('pagination: prevent pageNum change event ', async () => {
const mockFn = jest.fn();
const pageChange = jest.fn();
const {container} = render(
amisRender(
{
type: 'pagination',
layout: 'total,perPage,pager,go',
mode: 'normal',
activePage: 2,
lastPage: 99999,
total: 999,
perPage: 10,
maxButtons: 7,
showPerPage: true,
perPageAvailable: [10, 20, 50, 100],
showPageInput: true,
disabled: false,
id: 'u:1bf323bc4dbd',
onPageChange: pageChange,
onEvent: {
change: {
weight: 0,
actions: [
{
actionType: 'custom',
preventDefault: true, // 阻止事件默认行为
script: mockFn
}
]
}
}
},
{},
makeEnv({})
)
);
// 当前页码改变
const prev = container.querySelector('.cxd-Pagination-prev')!;
fireEvent.click(prev); // 上一页
await wait(100);
expect(mockFn).toBeCalledTimes(1);
await wait(200);
expect(pageChange).not.toBeCalled();
const next = container.querySelector('.cxd-Pagination-next')!;
fireEvent.click(next); // 下一页
await wait(300);
expect(mockFn).toBeCalledTimes(2);
await wait(200);
expect(pageChange).not.toBeCalled();
const go = container.querySelector('.cxd-Pagination-inputGroup')!;
fireEvent.change(go.querySelector('.cxd-Pagination-inputGroup-input')!, {
target: {value: 9}
});
await wait(400);
fireEvent.click(go.querySelector('.cxd-Pagination-inputGroup-right')!); // 输入后点击go
await wait(500);
expect(mockFn).toBeCalledTimes(3);
await wait(200);
expect(pageChange).not.toBeCalled();
function getPagerItem() {
const pager = container.querySelectorAll(
'.cxd-Pagination > .cxd-Pagination-pager-item'
);
return pager[3];
}
fireEvent.click(getPagerItem()!); // 点击页码切换
await wait(600);
expect(mockFn).toBeCalledTimes(4);
await wait(200);
expect(pageChange).not.toBeCalled();
});

View File

@@ -0,0 +1,85 @@
import {fireEvent, render} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv, wait} from '../../helper';
test('paginationWrapper: service + crud', async () => {
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
data: {
items: [
{
label: '110101',
name: '东城区',
sale: 46861
},
{
label: '110102',
name: '西城区',
sale: 44882
}
]
}
}
})
);
const {container} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'service',
id: 'u:ff652047d747',
api: {
method: 'get',
url: 'https://yapi.baidu-int.com/mock/42601/amis-chart/chart/sales/data2'
},
body: [
{
type: 'pagination-wrapper',
body: [
{
type: 'crud',
source: '${items}',
columns: [
{
name: 'label',
label: '地区',
type: 'text',
id: 'u:331ab3342710'
},
{
name: 'sale',
label: '销售',
type: 'text',
id: 'u:3dba120eda1d'
}
],
id: 'u:b3c77cb44fc8',
perPageAvailable: [10]
}
],
inputName: 'items',
outputName: 'items',
perPage: 20,
position: 'bottom'
}
]
}
]
},
{},
makeEnv({
fetcher
})
)
);
await wait(200);
const tds = [].slice
.call(container.querySelectorAll('tbody td'))
.map((td: any) => td.textContent);
expect(tds).toEqual(['110101', '46861', '110102', '44882']);
});

View File

@@ -0,0 +1,515 @@
import React = require('react');
import * as renderer from 'react-test-renderer';
import {fireEvent, render, waitFor} from '@testing-library/react';
import '../../../src';
import {render as amisRender} from '../../../src';
import {makeEnv, replaceReactAriaIds, wait} from '../../helper';
test('doAction:service reload', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
name: 'Amis Renderer',
author: 'fex',
date: 1688871102,
info: ''
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'form',
body: [
{
type: 'input-text',
label: '名字:',
name: 'name',
value: 'amis'
},
{
type: 'input-text',
label: '年龄:',
name: 'age',
value: 18
},
{
type: 'button',
label: '刷新Service数据加载请求',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'service_reload',
actionType: 'reload'
}
]
}
}
}
]
},
{
type: 'service',
api: '/api/mock2/form/initData',
body: [
{
type: 'input-text',
label: '我的名字:',
name: 'name'
},
{
type: 'input-text',
label: '我的年龄:',
name: 'age'
}
],
id: 'service_reload'
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(getByText('刷新Service数据加载请求')).toBeInTheDocument();
expect(
container.querySelector('[name="name"][value="Amis Renderer"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[data-testid="spinner"]')
).not.toBeInTheDocument();
});
replaceReactAriaIds(container);
expect(container).toMatchSnapshot();
fireEvent.click(getByText(/刷新Service数据加载请求/));
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual('/api/mock2/form/initData');
expect(fetcher.mock.calls[1][0].url).toEqual('/api/mock2/form/initData');
expect(
container.querySelector('[name="name"][value="Amis Renderer"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value=""]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:service reload with data', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
name: 'Amis Renderer',
author: 'fex',
date: 1688871102,
info: ''
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '刷新Service数据加载请求,同时把年龄更新为18',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'service_reload',
actionType: 'reload',
data: {
age: '18'
}
}
]
}
}
},
{
type: 'service',
api: '/api/mock2/form/initData',
body: [
{
type: 'input-text',
label: '我的名字:',
name: 'name'
},
{
type: 'input-text',
label: '我的年龄:',
name: 'age'
}
],
id: 'service_reload'
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(
getByText('刷新Service数据加载请求,同时把年龄更新为18')
).toBeInTheDocument();
expect(
container.querySelector('[name="name"][value="Amis Renderer"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[data-testid="spinner"]')
).not.toBeInTheDocument();
});
replaceReactAriaIds(container);
expect(container).toMatchSnapshot();
fireEvent.click(getByText(/刷新Service数据加载请求,同时把年龄更新为18/));
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual('/api/mock2/form/initData');
expect(fetcher.mock.calls[1][0].url).toEqual('/api/mock2/form/initData');
expect(
container.querySelector('[name="name"][value="Amis Renderer"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value="18"]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:service(schemaApi) reload', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
controls: [
{
type: 'input-text',
label: '我的名字:',
name: 'name'
},
{
type: 'input-text',
label: '我的年龄:',
name: 'age'
}
]
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '刷新Service数据加载请求',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'service_reload',
actionType: 'reload'
}
]
}
}
},
{
type: 'service',
schemaApi: '/api/schemaApi',
id: 'service_reload'
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(getByText('刷新Service数据加载请求')).toBeInTheDocument();
expect(
container.querySelector('[name="name"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[data-testid="spinner"]')
).not.toBeInTheDocument();
});
replaceReactAriaIds(container);
expect(container).toMatchSnapshot();
fireEvent.click(getByText(/刷新Service数据加载请求/));
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual('/api/schemaApi?_replace=1');
expect(fetcher.mock.calls[1][0].url).toEqual('/api/schemaApi?_replace=1');
expect(
container.querySelector('[name="name"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value=""]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:service(schemaApi) reload with data', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
controls: [
{
type: 'input-text',
label: '我的名字:',
name: 'name'
},
{
type: 'input-text',
label: '我的年龄:',
name: 'age'
}
]
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '刷新Service数据加载请求,同时把年龄更新为18',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'service_reload',
actionType: 'reload',
data: {
age: '18'
}
}
]
}
}
},
{
type: 'service',
schemaApi: '/api/schemaApi',
id: 'service_reload'
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(
getByText('刷新Service数据加载请求,同时把年龄更新为18')
).toBeInTheDocument();
expect(
container.querySelector('[name="name"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[data-testid="spinner"]')
).not.toBeInTheDocument();
});
replaceReactAriaIds(container);
expect(container).toMatchSnapshot();
fireEvent.click(getByText(/刷新Service数据加载请求,同时把年龄更新为18/));
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual('/api/schemaApi?_replace=1');
expect(fetcher.mock.calls[1][0].url).toEqual('/api/schemaApi?_replace=1');
expect(
container.querySelector('[name="name"][value=""]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value="18"]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});
test('doAction:service(schemaApi+data) reload with data', async () => {
const notify = jest.fn();
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
data: {
name: 'amis',
age: 20
},
controls: [
{
type: 'input-text',
label: '我的名字:',
name: 'name'
},
{
type: 'input-text',
label: '我的年龄:',
name: 'age'
}
]
}
}
})
);
const {container, getByText} = render(
amisRender(
{
type: 'page',
body: [
{
type: 'button',
label: '刷新Service数据加载请求,同时把年龄更新为18',
level: 'primary',
className: 'mb-2',
onEvent: {
click: {
actions: [
{
componentId: 'service_reload',
actionType: 'reload',
data: {
age: '18'
}
}
]
}
}
},
{
type: 'service',
schemaApi: '/api/schemaApi',
id: 'service_reload'
}
]
},
{},
makeEnv({
notify,
fetcher
})
)
);
await waitFor(() => {
expect(
getByText('刷新Service数据加载请求,同时把年龄更新为18')
).toBeInTheDocument();
expect(
container.querySelector('[name="name"][value="amis"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value="20"]')
).toBeInTheDocument();
expect(
container.querySelector('[data-testid="spinner"]')
).not.toBeInTheDocument();
});
replaceReactAriaIds(container);
expect(container).toMatchSnapshot();
fireEvent.click(getByText(/刷新Service数据加载请求,同时把年龄更新为18/));
await waitFor(() => {
expect(fetcher).toHaveBeenCalledTimes(2);
expect(fetcher.mock.calls[0][0].url).toEqual('/api/schemaApi?_replace=1');
expect(fetcher.mock.calls[1][0].url).toEqual('/api/schemaApi?_replace=1');
expect(
container.querySelector('[name="name"][value="amis"]')
).toBeInTheDocument();
expect(
container.querySelector('[name="age"][value="18"]')
).toBeInTheDocument();
});
expect(container).toMatchSnapshot();
});