Commit 7f63f4de by chenwq

新增车辆到期信息样式处理及单元测试

1 parent 0f9b04c4
...@@ -150,7 +150,7 @@ ...@@ -150,7 +150,7 @@
</template> </template>
</template> </template>
<template v-else> <template v-else>
<div class="flex"> <div class="flex" :style="getExpiryTextStyle(item, detailInfo)">
<div v-if="item.className && item.className(detailInfo)" <div v-if="item.className && item.className(detailInfo)"
class="dot" class="dot"
:style="{ :style="{
...@@ -236,6 +236,7 @@ import { ...@@ -236,6 +236,7 @@ import {
} from "@/common/constants.js"; } from "@/common/constants.js";
import { deepClone } from "@/common/util"; import { deepClone } from "@/common/util";
import preview from "../../components/preview"; import preview from "../../components/preview";
import { getExpiryTextStyle } from "./vehicleInfoExpiry";
export default { export default {
components: { components: {
...@@ -308,10 +309,18 @@ export default { ...@@ -308,10 +309,18 @@ export default {
}, },
}, },
{ {
title: "交强险到期",
key: "compulsoryInsuranceExpiry",
},
{
title: "表显里程", title: "表显里程",
key: "mileageStr", key: "mileageStr",
}, },
{ {
title: "年审到期",
key: "annualInspectionExpiry",
},
{
title: "使用性质", title: "使用性质",
key: "carTypeName", key: "carTypeName",
formatter: (row) => { formatter: (row) => {
...@@ -325,6 +334,10 @@ export default { ...@@ -325,6 +334,10 @@ export default {
}, },
}, },
{ {
title: "商业险到期",
key: "commercialInsuranceExpiry",
},
{
title: "所有人性质", title: "所有人性质",
key: "ownerType", key: "ownerType",
formatter: (row) => { formatter: (row) => {
...@@ -494,6 +507,7 @@ export default { ...@@ -494,6 +507,7 @@ export default {
}; };
}, },
methods: { methods: {
getExpiryTextStyle,
getStatusItem(baseData, optKey, row, rowKey) { getStatusItem(baseData, optKey, row, rowKey) {
if (!baseData || !baseData[optKey] || !row) { if (!baseData || !baseData[optKey] || !row) {
return null; return null;
......
const expiryKeys = [
"compulsoryInsuranceExpiry",
"annualInspectionExpiry",
"commercialInsuranceExpiry",
];
function getLocalDate(value) {
if (!value) {
return null;
}
if (value instanceof Date) {
return new Date(value.getFullYear(), value.getMonth(), value.getDate());
}
const matched = String(value).match(/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})/);
if (matched) {
return new Date(
Number(matched[1]),
Number(matched[2]) - 1,
Number(matched[3]),
);
}
const date = new Date(value);
if (Number.isNaN(date.getTime())) {
return null;
}
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}
export function getExpiryTextStyle(item, row) {
if (!item || !expiryKeys.includes(item.key) || !row) {
return {};
}
const expiryDate = getLocalDate(row[item.key]);
if (!expiryDate) {
return {};
}
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const diffDays = Math.ceil((expiryDate.getTime() - today.getTime()) / 86400000);
if (diffDays < 0) {
return { color: "#EB4E4E" };
}
if (diffDays <= 30) {
return { color: "#FF8D1A" };
}
return {};
}
import { getExpiryTextStyle } from "./vehicleInfoExpiry";
describe("vehicleInfo expiry date colors", () => {
beforeEach(() => {
jest.useFakeTimers("modern");
jest.setSystemTime(new Date(2026, 6, 6));
});
afterEach(() => {
jest.useRealTimers();
});
it("marks expired expiry fields red", () => {
const style = getExpiryTextStyle(
{ key: "annualInspectionExpiry" },
{ annualInspectionExpiry: "2026-07-05" },
);
expect(style).toEqual({ color: "#EB4E4E" });
});
it("marks expiry fields within thirty days orange", () => {
const style = getExpiryTextStyle(
{ key: "commercialInsuranceExpiry" },
{ commercialInsuranceExpiry: "2026-08-05" },
);
expect(style).toEqual({ color: "#FF8D1A" });
});
it("keeps non-expiry fields and later dates default", () => {
expect(getExpiryTextStyle(
{ key: "licensePlate" },
{ licensePlate: "A12345" },
)).toEqual({});
expect(getExpiryTextStyle(
{ key: "compulsoryInsuranceExpiry" },
{ compulsoryInsuranceExpiry: "2026-08-06" },
)).toEqual({});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!