Skip to content

Commit

Permalink
refactor(logistics): 使用场景改为后台读取
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Jan 1, 2023
1 parent bde8ff4 commit 5ece247
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 24 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router": "^5.2.0",
"typescript": "^4.5.2"
"typescript": "^4.5.2",
"use-async-effect": "^2.2.7"
},
"ciDependencies": {
"miaoxing": "miaoxing/miaoxing-js",
Expand Down
13 changes: 13 additions & 0 deletions pages/admin/logistics-addresses/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
exports[`admin/logistics-addresses index 1`] = `
[MockFunction] {
"calls": Array [
Array [
Object {
"method": "get",
"url": "/admin-api/consts/logisticsAddressModel-type",
},
],
Array [
Object {
"method": "get",
Expand All @@ -18,6 +24,13 @@ exports[`admin/logistics-addresses index 1`] = `
"resolve": [Function],
},
},
Object {
"type": "return",
"value": Promise {
"reject": [Function],
"resolve": [Function],
},
},
],
}
`;
26 changes: 26 additions & 0 deletions pages/admin/logistics-addresses/__snapshots__/new.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ exports[`admin/logistics-addresses form 1`] = `
"url": "/admin-api/logistics-addresses/defaults",
},
],
Array [
Object {
"method": "get",
"url": "/admin-api/consts/logisticsAddressModel-type",
},
],
Array [
Object {
"method": "get",
Expand Down Expand Up @@ -57,6 +63,13 @@ exports[`admin/logistics-addresses form 1`] = `
"resolve": [Function],
},
},
Object {
"type": "return",
"value": Promise {
"reject": [Function],
"resolve": [Function],
},
},
],
}
`;
Expand All @@ -76,6 +89,12 @@ exports[`admin/logistics-addresses form 2`] = `
"url": "/admin-api/logistics-addresses/defaults",
},
],
Array [
Object {
"method": "get",
"url": "/admin-api/consts/logisticsAddressModel-type",
},
],
Array [
Object {
"method": "get",
Expand Down Expand Up @@ -148,6 +167,13 @@ exports[`admin/logistics-addresses form 2`] = `
"resolve": [Function],
},
},
Object {
"type": "return",
"value": Promise {
"reject": [Function],
"resolve": [Function],
},
},
],
}
`;
20 changes: 15 additions & 5 deletions pages/admin/logistics-addresses/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ import {CEditLink, CNewBtn} from '@mxjs/a-clink';
import {Page, PageActions} from '@mxjs/a-page';
import {LinkActions} from '@mxjs/actions';
import {Tag} from 'antd';

const typeNames = {
1: '退货',
};
import {useState} from 'react';
import useAsyncEffect from 'use-async-effect';
import api from '@mxjs/api';
import $ from 'miaoxing';

const Index = () => {
const [table] = useTable();

const [types, setTypes] = useState({});
useAsyncEffect(async () => {
const {ret} = await api.get('consts/logisticsAddressModel-type');
if (ret.isErr()) {
$.ret(ret);
return;
}
setTypes(ret.data.items);
}, []);

return (
<Page>
<PageActions>
Expand Down Expand Up @@ -43,7 +53,7 @@ const Index = () => {
title: '使用场景',
dataIndex: 'types',
render: (value) => value.length ?
value.map(type => <Tag key={type} color="orange">{typeNames[type]}</Tag>) : '-',
value.map(type => <Tag key={type} color="orange">{types?.[type]?.name}</Tag>) : '-',
},
{
title: '顺序',
Expand Down
21 changes: 18 additions & 3 deletions pages/admin/logistics-addresses/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,25 @@ describe(path, () => {

test('index', async () => {
const promise = createPromise();
const promise2 = createPromise();

$.http = jest.fn()
// 读取列表数据
// 读取使用场景
.mockImplementationOnce(() => promise.resolve({
ret: Ret.suc({
data: {
items: {
1: {
id: 1,
key: 'return',
name: '退货',
},
},
},
}),
}))
// 读取列表数据
.mockImplementationOnce(() => promise2.resolve({
ret: Ret.suc({
data: [
{
Expand Down Expand Up @@ -67,8 +82,8 @@ describe(path, () => {
await findByText('51');
await findByText('2020-01-01 00:00:00');

await Promise.all([promise]);
expect($.http).toHaveBeenCalledTimes(1);
await Promise.all([promise, promise2]);
expect($.http).toHaveBeenCalledTimes(2);
expect($.http).toMatchSnapshot();
});
});
27 changes: 19 additions & 8 deletions pages/admin/logistics-addresses/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ import {CListBtn} from '@mxjs/a-clink';
import {Page, PageActions} from '@mxjs/a-page';
import {Form, FormAction, FormItem} from '@mxjs/a-form';
import RegionCascader from '@mxjs/a-region-cascader';
import {Checkbox, Col, Row} from 'antd';
import {Checkbox} from 'antd';
import $ from 'miaoxing';
import {FormItemSort} from '@miaoxing/admin';
import Input from '@mxjs/a-input';
import {useState} from 'react';
import useAsyncEffect from 'use-async-effect';
import api from '@mxjs/api';

const New = () => {
const [types, setTypes] = useState([]);
useAsyncEffect(async () => {
const {ret} = await api.get('consts/logisticsAddressModel-type');
if (ret.isErr()) {
$.ret(ret);
return;
}
setTypes(Object.values(ret.data.items));
}, []);

return (
<Page>
<PageActions>
Expand Down Expand Up @@ -70,13 +83,11 @@ const New = () => {

<FormItem label="使用场景" name="types">
<Checkbox.Group>
<Row>
<Col span={32}>
<Checkbox value={1}>
退货
</Checkbox>
</Col>
</Row>
{types.map(type => (
<Checkbox key={type.id} value={type.id}>
{type.name}
</Checkbox>
))}
</Checkbox.Group>
</FormItem>

Expand Down
29 changes: 22 additions & 7 deletions pages/admin/logistics-addresses/new.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe(path, () => {
const promise3 = createPromise();
const promise4 = createPromise();
const promise5 = createPromise();
const promise6 = createPromise();

$.http = jest.fn()
// 读取一级地区
Expand Down Expand Up @@ -71,8 +72,22 @@ describe(path, () => {
},
}),
}))
// 读取二级地区
// 读取使用场景
.mockImplementationOnce(() => promise3.resolve({
ret: Ret.suc({
data: {
items: {
1: {
id: 1,
key: 'return',
name: '退货',
},
},
},
}),
}))
// 读取二级地区
.mockImplementationOnce(() => promise4.resolve({
ret: Ret.suc({
data: [{
id: 140200,
Expand All @@ -84,7 +99,7 @@ describe(path, () => {
}),
}))
// 读取三级地区
.mockImplementationOnce(() => promise4.resolve({
.mockImplementationOnce(() => promise5.resolve({
ret: Ret.suc({
data: [{
hasChildren: false,
Expand All @@ -96,16 +111,16 @@ describe(path, () => {
}),
}))
// 提交
.mockImplementationOnce(() => promise5.resolve({
.mockImplementationOnce(() => promise6.resolve({
ret: Ret.suc(),
}));

const {getByLabelText} = render(<MemoryRouter>
<Page/>
</MemoryRouter>);

await Promise.all([promise, promise2, promise3, promise4]);
expect($.http).toHaveBeenCalledTimes(4);
await Promise.all([promise, promise2, promise3, promise4, promise5]);
expect($.http).toHaveBeenCalledTimes(5);
expect($.http).toMatchSnapshot();

// 看到表单加载了数据
Expand All @@ -115,8 +130,8 @@ describe(path, () => {
// 提交表单
fireEvent.click(screen.getByText('提 交'));

await Promise.all([promise5]);
expect($.http).toHaveBeenCalledTimes(5);
await Promise.all([promise6]);
expect($.http).toHaveBeenCalledTimes(6);
expect($.http).toMatchSnapshot();
});
});
2 changes: 2 additions & 0 deletions src/Service/LogisticsAddressModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Miaoxing\Logistics\Metadata\LogisticsAddressTrait;
use Miaoxing\Plugin\BaseModel;
use Miaoxing\Plugin\ConstTrait;
use Miaoxing\Plugin\Model\HasAppIdTrait;
use Miaoxing\Plugin\Model\ModelTrait;
use Miaoxing\Plugin\Model\ReqQueryTrait;
Expand All @@ -16,6 +17,7 @@
*/
class LogisticsAddressModel extends BaseModel
{
use ConstTrait;
use HasAppIdTrait;
use LogisticsAddressTrait;
use ModelTrait;
Expand Down

0 comments on commit 5ece247

Please sign in to comment.