Skip to content

Commit

Permalink
test: added another test case
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Dec 28, 2024
1 parent c1f3518 commit a90ab85
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/web/hooks/__tests__/use-twinned-swap-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe("useTwinnedSwapInput", () => {
}

act(() => {
result.current.setPrice("1");
setAmount("");
result.current.setQuoteType("in-given-out");
});
Expand Down Expand Up @@ -144,20 +145,33 @@ describe("useTwinnedSwapInput", () => {
expectedToken: focused === "fiat" ? "100.12" : "100.123456",
expectedFiat: focused === "fiat" ? "100.12" : "100.123456",
},
{
scenario: "a non-standard price",
input: "100",
price: "0.125",
expectedToken: focused === "fiat" ? "800" : "100",
expectedFiat: focused === "fiat" ? "100" : "12.5",
},
])(
"should handle $scenario",
({ input, expectedToken, expectedFiat, fail }) => {
({ input, expectedToken, expectedFiat, fail, price = "1" }) => {
act(() => {
result.current.setPrice(price);
setAmount(input);
});

// Check primary inputs are set correctly
expect(result.current.tokenAmount).toBe(expectedToken);
expect(result.current.fiatAmount).toBe(expectedFiat);

// Check market conditions are set correctly
expect(result.current.quoteType).toBe(
fail ? "in-given-out" : expectedQuoteType
);

if (!fail) {
expect(result.current[updatedMarketField]).toBe(expectedToken);
expect(result.current[updatedMarketField]).toBe(
focused === "fiat" ? expectedFiat : expectedToken
);
}
}
);
Expand Down
10 changes: 10 additions & 0 deletions packages/web/hooks/use-twinned-swap-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ export const useTwinnedSwapInput = ({
return;
}

if (price.isNegative()) {
console.error("INPUT WARNING: Price is negative");
return;
}

const tokenValue = transformAmount(value, baseAssetDecimals, false);

if (
Expand Down Expand Up @@ -159,6 +164,11 @@ export const useTwinnedSwapInput = ({
return;
}

if (price.isNegative()) {
console.error("INPUT WARNING: Price is negative");
return;
}

// Fiat amount is always 2 decimal places
const fiatValue = transformAmount(value, 2, false);

Expand Down

0 comments on commit a90ab85

Please sign in to comment.