mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-30 02:48:55 +08:00
Merge pull request #10082 from hsm-lv/fix-ajaxaction
fix:发送请求动作的sendOn配置不生效
This commit is contained in:
commit
5f76ea8b5d
@ -3,6 +3,7 @@ import {normalizeApi, normalizeApiResponseData} from '../utils/api';
|
||||
import {ServerError} from '../utils/errors';
|
||||
import {createObject, isEmpty} from '../utils/helper';
|
||||
import {RendererEvent} from '../utils/renderer-event';
|
||||
import {evalExpressionWithConditionBuilder} from '../utils/tpl';
|
||||
import {
|
||||
RendererAction,
|
||||
ListenerAction,
|
||||
@ -58,6 +59,19 @@ export class AjaxAction implements RendererAction {
|
||||
const messages = (action?.api as ApiObject)?.messages;
|
||||
let api = normalizeApi(action.api);
|
||||
|
||||
if (api.sendOn !== undefined) {
|
||||
// 发送请求前,判断是否需要发送
|
||||
const sendOn = await evalExpressionWithConditionBuilder(
|
||||
api.sendOn,
|
||||
action.data ?? {},
|
||||
false
|
||||
);
|
||||
|
||||
if (!sendOn) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没配置data数据映射,则给一个空对象,避免将当前数据域作为接口请求参数
|
||||
if ((api as any)?.data == undefined) {
|
||||
api = {
|
||||
|
@ -103,3 +103,33 @@ exports[`EventAction:ajax args 1`] = `
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`EventAction:ajax sendOn 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"
|
||||
type="button"
|
||||
>
|
||||
<span>
|
||||
发送请求
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
@ -757,7 +757,6 @@ test('EventAction:ajax silent', async () => {
|
||||
|
||||
await waitFor(() => {
|
||||
expect(fetcher).toHaveBeenCalledTimes(4);
|
||||
debugger;
|
||||
expect(fetcher.mock.calls[0][0].url).toEqual('/api/xxx1');
|
||||
expect(fetcher.mock.calls[1][0].url).toEqual('/api/xxx2');
|
||||
expect(fetcher.mock.calls[2][0].url).toEqual('/api/xxx3');
|
||||
@ -765,3 +764,56 @@ test('EventAction:ajax silent', async () => {
|
||||
expect(notify).toBeCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
test('EventAction:ajax sendOn', async () => {
|
||||
const fetcher = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
status: 0,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
age: 18
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
const {getByText, container}: any = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'page',
|
||||
body: [
|
||||
{
|
||||
type: 'button',
|
||||
label: '发送请求',
|
||||
level: 'primary',
|
||||
onEvent: {
|
||||
click: {
|
||||
actions: [
|
||||
{
|
||||
actionType: 'ajax',
|
||||
api: {
|
||||
url: '/api/xxx',
|
||||
method: 'get',
|
||||
sendOn: '${1 !== 1}'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
fetcher
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
fireEvent.click(getByText('发送请求'));
|
||||
await waitFor(() => {
|
||||
expect(fetcher).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
expect(container).toMatchSnapshot();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user