计算器 结果还是会被 不断 /100
This commit is contained in:
@@ -66,23 +66,66 @@ describe('边界:计算器未返回的值不覆盖、无变化不写', () => {
|
||||
expect(state.ytm).toBe(2.5);
|
||||
});
|
||||
|
||||
// 模拟 calcBondForItem:以既有三字段为代理调纯函数,再写回 item
|
||||
test('浮动腿 item 写回:仅未手填字段被覆盖', () => {
|
||||
const item = { PosiNetNoFeePrice: 99.5, PosiGrossPrice: 100.0, InitYtm: 2.6,
|
||||
// 模拟 calcBondForItem:proxy 统一用【展示态】,模型字段是【存储态】,回写时 bondCalcPriceToStorage。
|
||||
test('浮动腿 item 写回:仅未手填字段被覆盖,且存储态转换正确', () => {
|
||||
// 用户输入净价 99.5 → vue-number-input(percent:true) 存为存储态 0.995
|
||||
const item = { PosiNetNoFeePrice: 0.995, PosiGrossPrice: 1.0, InitYtm: 0.026,
|
||||
bondDriverType: 'CP', bondManual: { CP: true, DP: false, YD: false } };
|
||||
const calc = { cleanPrice: 99, dirtyPrice: 100, ytm: 2.5 };
|
||||
const calc = { cleanPrice: 99, dirtyPrice: 100, ytm: 2.5 }; // 计算器返回展示态
|
||||
const proxy = {
|
||||
cleanPrice: item.PosiNetNoFeePrice, // 净价 = 手填
|
||||
dirtyPrice: item.PosiGrossPrice,
|
||||
ytm: item.InitYtm
|
||||
cleanPrice: SwapCalc.bondPriceToCalc(item.PosiNetNoFeePrice), // 0.995 → 99.5(手填)
|
||||
dirtyPrice: SwapCalc.bondPriceToCalc(item.PosiGrossPrice), // 1.0 → 100
|
||||
ytm: SwapCalc.bondPriceToCalc(item.InitYtm) // 0.026 → 2.6
|
||||
};
|
||||
SwapCalc.applyBondCalcResult(proxy, calc, item.bondManual);
|
||||
item.PosiNetNoFeePrice = proxy.cleanPrice;
|
||||
item.PosiGrossPrice = proxy.dirtyPrice;
|
||||
item.InitYtm = proxy.ytm;
|
||||
expect(item.PosiNetNoFeePrice).toBe(99.5); // 手填(净价):保持
|
||||
expect(item.PosiGrossPrice).toBe(100); // 全价:被反算覆盖
|
||||
expect(item.InitYtm).toBe(2.5); // 收益率:被反算覆盖
|
||||
item.PosiNetNoFeePrice = SwapCalc.bondCalcPriceToStorage(proxy.cleanPrice);
|
||||
item.PosiGrossPrice = SwapCalc.bondCalcPriceToStorage(proxy.dirtyPrice);
|
||||
item.InitYtm = SwapCalc.bondCalcPriceToStorage(proxy.ytm);
|
||||
expect(item.PosiNetNoFeePrice).toBe(0.995); // 手填(净价):存储态保持 0.995(界面仍显示 99.5)
|
||||
expect(item.PosiGrossPrice).toBe(1.0); // 全价:被反算覆盖,存储态 100/100=1.0
|
||||
expect(item.InitYtm).toBe(0.025); // 收益率:被反算覆盖,存储态 2.5/100=0.025
|
||||
});
|
||||
|
||||
// 核心回归:用户输入净价 100(存储态 1.0)时,绝不能因 proxy 残留存储态而再 ÷100 变成 0.01
|
||||
test('核心回归:输入净价 100 不会被二次缩小为 1(存储态 0.01)', () => {
|
||||
const item = { PosiNetNoFeePrice: 1.0, PosiGrossPrice: null, InitYtm: null,
|
||||
bondDriverType: 'CP', bondManual: { CP: true, DP: false, YD: false } };
|
||||
const calc = { cleanPrice: 100, dirtyPrice: 102.17928767123287, ytm: 4.860445236 };
|
||||
const proxy = {
|
||||
cleanPrice: SwapCalc.bondPriceToCalc(item.PosiNetNoFeePrice), // 1.0 → 100
|
||||
dirtyPrice: SwapCalc.bondPriceToCalc(item.PosiGrossPrice),
|
||||
ytm: SwapCalc.bondPriceToCalc(item.InitYtm)
|
||||
};
|
||||
SwapCalc.applyBondCalcResult(proxy, calc, item.bondManual);
|
||||
item.PosiNetNoFeePrice = SwapCalc.bondCalcPriceToStorage(proxy.cleanPrice);
|
||||
item.PosiGrossPrice = SwapCalc.bondCalcPriceToStorage(proxy.dirtyPrice);
|
||||
item.InitYtm = SwapCalc.bondCalcPriceToStorage(proxy.ytm);
|
||||
expect(item.PosiNetNoFeePrice).toBe(1.0); // 存储态仍是 1.0(界面显示 100)
|
||||
expect(item.PosiGrossPrice).toBeCloseTo(1.0217928767123287, 9); // 102.179.../100
|
||||
expect(item.InitYtm).toBeCloseTo(0.04860445236, 9); // 4.860.../100
|
||||
});
|
||||
|
||||
// 多轮不同净价:95 / 105,确保存储态始终与界面输入一致,不会被二次缩小
|
||||
test.each([
|
||||
[95, 97.5, 5.123456],
|
||||
[105, 107.3, 4.567890],
|
||||
[100, 102.17928767123287, 4.860445236]
|
||||
])('多轮净价校验:输入净价 %s 时,存储态保持 %s/100 且不会被二次缩小', (inputClean, calcDirty, calcYtm) => {
|
||||
const item = { PosiNetNoFeePrice: inputClean / 100, PosiGrossPrice: null, InitYtm: null,
|
||||
bondDriverType: 'CP', bondManual: { CP: true, DP: false, YD: false } };
|
||||
const calc = { cleanPrice: inputClean, dirtyPrice: calcDirty, ytm: calcYtm };
|
||||
const proxy = {
|
||||
cleanPrice: SwapCalc.bondPriceToCalc(item.PosiNetNoFeePrice),
|
||||
dirtyPrice: SwapCalc.bondPriceToCalc(item.PosiGrossPrice),
|
||||
ytm: SwapCalc.bondPriceToCalc(item.InitYtm)
|
||||
};
|
||||
SwapCalc.applyBondCalcResult(proxy, calc, item.bondManual);
|
||||
item.PosiNetNoFeePrice = SwapCalc.bondCalcPriceToStorage(proxy.cleanPrice);
|
||||
item.PosiGrossPrice = SwapCalc.bondCalcPriceToStorage(proxy.dirtyPrice);
|
||||
item.InitYtm = SwapCalc.bondCalcPriceToStorage(proxy.ytm);
|
||||
expect(item.PosiNetNoFeePrice).toBe(inputClean / 100);
|
||||
expect(item.PosiGrossPrice).toBeCloseTo(calcDirty / 100, 9);
|
||||
expect(item.InitYtm).toBeCloseTo(calcYtm / 100, 9);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user