import { CoinPretty } from "@keplr-wallet/unit"; import { AmountConfig } from "./amount"; import { EmptyAmountError, ZeroAmountError } from "./errors"; const currency = { coinDenom: "RUNE", coinMinimalDenom: "rune", coinDecimals: 8, }; function createAmountConfig(): AmountConfig { const modularChainInfo = { currencies: [currency], findCurrency: (coinMinimalDenom: string) => { return coinMinimalDenom === currency.coinMinimalDenom ? currency : undefined; }, }; const balance = { currency, response: { data: {} }, isFetching: false, balance: new CoinPretty(currency, "100000000"), }; const config = new AmountConfig( { getModularChain: () => modularChainInfo, hasModularChain: () => true, } as any, { get: () => ({ queryBalances: { getQueryBech32Address: () => ({ balances: [balance] }), }, }), } as any, "thorchain-1", { sender: "thor1sender" } as any, false ); config.setCurrency(currency); return config; } describe("AmountConfig zero amount policy", () => { test("rejects zero amount by default", () => { const config = createAmountConfig(); config.setValue("0"); expect(config.uiProperties.error).toBeInstanceOf(ZeroAmountError); }); test("accepts zero amount only after an explicit opt-in", () => { const config = createAmountConfig(); config.setValue("0"); config.setZeroAmountAllowed(true); expect(config.uiProperties.error).toBeUndefined(); }); test("still rejects an empty amount after the opt-in", () => { const config = createAmountConfig(); config.setZeroAmountAllowed(true); expect(config.uiProperties.error).toBeInstanceOf(EmptyAmountError); }); test("rejects a positive amount that truncates to zero", () => { const config = createAmountConfig(); config.setValue("0.000000001"); config.setZeroAmountAllowed(true); expect(config.uiProperties.error).toBeInstanceOf(ZeroAmountError); }); });