mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 03:58:07 +08:00
Merge pull request #7684 from 2betop/chore-api803
chore: 调整 responseData 当接口返回错误但是有数据返回时也有用 Close: #7680
This commit is contained in:
commit
c2abaec30e
@ -14,7 +14,8 @@ import {
|
||||
extendObject,
|
||||
qsparse,
|
||||
uuid,
|
||||
JSONTraverse
|
||||
JSONTraverse,
|
||||
isEmpty
|
||||
} from './helper';
|
||||
import isPlainObject from 'lodash/isPlainObject';
|
||||
import {debug, warning} from './debug';
|
||||
@ -432,19 +433,11 @@ export function responseAdaptor(ret: fetcherResult, api: ApiObject) {
|
||||
|
||||
debug('api', 'response', payload);
|
||||
|
||||
if (payload.ok && api.responseData) {
|
||||
if (api.responseData && (payload.ok || !isEmpty(payload.data))) {
|
||||
debug('api', 'before dataMapping', payload.data);
|
||||
const responseData = dataMapping(
|
||||
api.responseData,
|
||||
|
||||
createObject(
|
||||
{api},
|
||||
(Array.isArray(payload.data)
|
||||
? {
|
||||
items: payload.data
|
||||
}
|
||||
: payload.data) || {}
|
||||
),
|
||||
createObject({api}, normalizeApiResponseData(payload.data)),
|
||||
undefined,
|
||||
api.convertKeyToPath
|
||||
);
|
||||
|
@ -509,3 +509,139 @@ test('api:requestAdaptor2', async () => {
|
||||
expect(container.querySelector('input[name="id"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id"]') as any).value).toBe('2');
|
||||
});
|
||||
|
||||
test('api:responseData1', 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: 'form',
|
||||
id: 'form_submit',
|
||||
submitText: '提交表单',
|
||||
api: {
|
||||
method: 'post',
|
||||
url: '/api/mock2/form/saveForm',
|
||||
responseData: {
|
||||
id: '${id}',
|
||||
id2: '${id + 1}'
|
||||
}
|
||||
},
|
||||
body: [
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'id',
|
||||
label: 'Id'
|
||||
},
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'id2',
|
||||
label: 'Id2'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
notify,
|
||||
fetcher
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getByText('提交表单')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(getByText(/提交表单/));
|
||||
await wait(300);
|
||||
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
expect(container.querySelector('input[name="id"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id"]') as any).value).toBe('1');
|
||||
|
||||
expect(container.querySelector('input[name="id2"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id2"]') as any).value).toBe('2');
|
||||
});
|
||||
|
||||
test('api:responseData2', async () => {
|
||||
const notify = jest.fn();
|
||||
const fetcher = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
status: 500,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
id: 1
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'page',
|
||||
body: [
|
||||
{
|
||||
type: 'form',
|
||||
id: 'form_submit',
|
||||
submitText: '提交表单',
|
||||
api: {
|
||||
method: 'post',
|
||||
url: '/api/mock2/form/saveForm',
|
||||
responseData: {
|
||||
id: '${id}',
|
||||
id2: '${id + 1}'
|
||||
}
|
||||
},
|
||||
body: [
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'id',
|
||||
label: 'Id'
|
||||
},
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'id2',
|
||||
label: 'Id2'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
notify,
|
||||
fetcher
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getByText('提交表单')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(getByText(/提交表单/));
|
||||
await wait(300);
|
||||
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
expect(container.querySelector('input[name="id"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id"]') as any).value).toBe('1');
|
||||
|
||||
expect(container.querySelector('input[name="id2"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id2"]') as any).value).toBe('2');
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user