-type fixes

This commit is contained in:
Florian Dold 2022-10-31 17:18:16 +01:00
parent 5a91fbe2b7
commit 0e7a0741c6
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 29 additions and 27 deletions

View File

@ -4,7 +4,7 @@
"composite": true,
"target": "ES2018",
"module": "ESNext",
"moduleResolution": "node",
"moduleResolution": "Node16",
"sourceMap": true,
"lib": ["es6", "DOM"],
"noImplicitReturns": true,

View File

@ -192,16 +192,18 @@ export class Amounts {
*
* Throws when currencies don't match.
*/
static sub(a: AmountJson, ...rest: AmountJson[]): Result {
const currency = a.currency;
let value = a.value;
let fraction = a.fraction;
static sub(a: AmountLike, ...rest: AmountLike[]): Result {
const aJ = Amounts.jsonifyAmount(a);
const currency = aJ.currency;
let value = aJ.value;
let fraction = aJ.fraction;
for (const b of rest) {
if (b.currency.toUpperCase() !== a.currency.toUpperCase()) {
throw Error(`Mismatched currency: ${b.currency} and ${currency}`);
const bJ = Amounts.jsonifyAmount(b);
if (bJ.currency.toUpperCase() !== aJ.currency.toUpperCase()) {
throw Error(`Mismatched currency: ${bJ.currency} and ${currency}`);
}
if (fraction < b.fraction) {
if (fraction < bJ.fraction) {
if (value < 1) {
return {
amount: { currency, value: 0, fraction: 0 },
@ -211,12 +213,12 @@ export class Amounts {
value--;
fraction += amountFractionalBase;
}
console.assert(fraction >= b.fraction);
fraction -= b.fraction;
if (value < b.value) {
console.assert(fraction >= bJ.fraction);
fraction -= bJ.fraction;
if (value < bJ.value) {
return { amount: { currency, value: 0, fraction: 0 }, saturated: true };
}
value -= b.value;
value -= bJ.value;
}
return { amount: { currency, value, fraction }, saturated: false };

View File

@ -61,8 +61,8 @@ export function useComponentState(
},
fee: Amounts.sub(deposit.totalDepositCost, deposit.effectiveDepositAmount)
.amount,
cost: deposit.totalDepositCost,
effective: deposit.effectiveDepositAmount,
cost: Amounts.parseOrThrow(deposit.totalDepositCost),
effective: Amounts.parseOrThrow(deposit.effectiveDepositAmount),
cancel,
};
}

View File

@ -38,11 +38,9 @@ describe("Deposit CTA states", () => {
onSuccess: async () => {
null;
},
}
};
const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(props, mock),
);
mountHook(() => useComponentState(props, mock));
{
const { status } = pullLastResultOrThrow();
@ -62,15 +60,19 @@ describe("Deposit CTA states", () => {
expect(error.message).eq("ERROR_NO-URI-FOR-DEPOSIT");
}
await assertNoPendingUpdate();
expect(handler.getCallingQueueState()).eq("empty")
expect(handler.getCallingQueueState()).eq("empty");
});
it("should be ready after loading", async () => {
const { handler, mock } = createWalletApiMock();
handler.addWalletCallResponse(WalletApiOperation.PrepareDeposit, undefined, {
effectiveDepositAmount: Amounts.parseOrThrow("EUR:1"),
totalDepositCost: Amounts.parseOrThrow("EUR:1.2"),
});
handler.addWalletCallResponse(
WalletApiOperation.PrepareDeposit,
undefined,
{
effectiveDepositAmount: "EUR:1",
totalDepositCost: "EUR:1.2",
},
);
const props = {
talerDepositUri: "payto://refund/asdasdas",
amountStr: "EUR:1",
@ -80,12 +82,10 @@ describe("Deposit CTA states", () => {
onSuccess: async () => {
null;
},
}
};
const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(props, mock),
);
mountHook(() => useComponentState(props, mock));
{
const { status } = pullLastResultOrThrow();