Repository navigation
amounts: format with each currency's decimal places #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
22344ac
fix: format amounts with the currency's decimal places
antonio-xm 9bb848c
Address review: ISO minor units, significant-decimal validation, erro…
antonio-xm 75854f7
Use invalidRequest error code for unusable stored amounts
antonio-xm d77eed3
retrigger vercel preview
antonio-xm 67e914a
Fix review findings: strict string decimals, four-decimal prices
antonio-xm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { budgetAmounts, parseBudgetAmount } from "./budget"; | ||
| import { budgetFor, parsePrice } from "./payment-choice"; | ||
|
|
||
| describe("budgetAmounts", () => { | ||
| it("formats each option with the currency's decimals", () => { | ||
| expect(budgetAmounts("USD").map((m) => m.value)).toEqual(["20.00", "50.00", "100.00"]); | ||
| expect(budgetAmounts("KWD").map((m) => m.value)).toEqual(["20.000", "50.000", "100.000"]); | ||
| expect(budgetAmounts("CLP", { value: "10000", currency: "CLP" })).toEqual([ | ||
| { value: "10000", currency: "CLP" }, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("parseBudgetAmount", () => { | ||
| it("emits a zero-decimal currency without decimals", () => { | ||
| expect(parseBudgetAmount("10000", "CLP")).toEqual({ | ||
| amount: { value: "10000", currency: "CLP" }, | ||
| }); | ||
| expect(parseBudgetAmount("500", "JPY")).toEqual({ | ||
| amount: { value: "500", currency: "JPY" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("emits a three-decimal currency with three decimals", () => { | ||
| expect(parseBudgetAmount("1.25", "KWD")).toEqual({ | ||
| amount: { value: "1.250", currency: "KWD" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("rejects decimals the currency does not allow", () => { | ||
| expect(parseBudgetAmount("10000.5", "CLP")).toHaveProperty("problem"); | ||
| expect(parseBudgetAmount("10.005", "USD")).toHaveProperty("problem"); | ||
| }); | ||
|
|
||
| it("accepts zero-padded decimals the currency does not show", () => { | ||
| expect(parseBudgetAmount("10000.00", "CLP")).toEqual({ | ||
| amount: { value: "10000", currency: "CLP" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("reads a comma followed by three digits as a thousands separator", () => { | ||
| expect(parseBudgetAmount("10,000", "KWD")).toEqual({ | ||
| amount: { value: "10000.000", currency: "KWD" }, | ||
| }); | ||
| expect(parseBudgetAmount("1,234", "USD")).toEqual({ | ||
| amount: { value: "1234.00", currency: "USD" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("reads a comma followed by fewer digits as a decimal point", () => { | ||
| expect(parseBudgetAmount("10,50", "USD")).toEqual({ | ||
| amount: { value: "10.50", currency: "USD" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("keeps USD at two decimals", () => { | ||
| expect(parseBudgetAmount("75", "USD")).toEqual({ | ||
| amount: { value: "75.00", currency: "USD" }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("parsePrice", () => { | ||
| it("parses catalog prices with up to four decimals", () => { | ||
| expect(parsePrice("35.99 USD")).toEqual({ amount: "35.99", currency: "USD" }); | ||
| expect(parsePrice("1.250 KWD")).toEqual({ amount: "1.250", currency: "KWD" }); | ||
| expect(parsePrice("1.2345 CLF")).toEqual({ amount: "1.2345", currency: "CLF" }); | ||
| expect(parsePrice("1.23456 CLF")).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("budgetFor", () => { | ||
| it("pads the price into the currency's decimals", () => { | ||
| expect(budgetFor({ amount: "8000", currency: "CLP" })).toEqual({ | ||
| value: "10000", | ||
| currency: "CLP", | ||
| }); | ||
| expect(budgetFor({ amount: "100", currency: "USD" })).toEqual({ | ||
| value: "125.00", | ||
| currency: "USD", | ||
| }); | ||
| }); | ||
|
|
||
| it("formats a three-decimal price and budget", () => { | ||
| expect(budgetFor({ amount: "1.250", currency: "KWD" })).toEqual({ | ||
| value: "12.000", | ||
| currency: "KWD", | ||
| }); | ||
| }); | ||
|
|
||
| it("falls back to two decimals for a currency code it does not know", () => { | ||
| expect(budgetFor({ amount: "100", currency: "ZZZ" })).toEqual({ | ||
| value: "125.00", | ||
| currency: "ZZZ", | ||
| }); | ||
| expect(budgetAmounts("ZZZ").map((m) => m.value)).toEqual(["20.00", "50.00", "100.00"]); | ||
| expect(parseBudgetAmount("75", "ZZZ")).toEqual({ | ||
| amount: { value: "75.00", currency: "ZZZ" }, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.