|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | +import { vi } from "vitest"; |
| 4 | +import "@testing-library/jest-dom"; |
| 5 | +import CurrencyInput from "./CurrencyInput"; |
| 6 | + |
| 7 | +describe("CurrencyInput", () => { |
| 8 | + it("allows typing a decimal point mid-entry", async () => { |
| 9 | + const setEnteredAmount = vi.fn(); |
| 10 | + const onChange = vi.fn(); |
| 11 | + |
| 12 | + render( |
| 13 | + <CurrencyInput |
| 14 | + name="amount" |
| 15 | + value="" |
| 16 | + setEnteredAmount={setEnteredAmount} |
| 17 | + onChange={onChange} |
| 18 | + /> |
| 19 | + ); |
| 20 | + |
| 21 | + const input = screen.getByRole("textbox"); |
| 22 | + await userEvent.type(input, "5."); |
| 23 | + |
| 24 | + // The input should visually show the decimal (not strip it) |
| 25 | + expect(input).toHaveDisplayValue(/5\./); |
| 26 | + }); |
| 27 | + |
| 28 | + it("calls setEnteredAmount with the float value on change", async () => { |
| 29 | + const setEnteredAmount = vi.fn(); |
| 30 | + const onChange = vi.fn(); |
| 31 | + |
| 32 | + render( |
| 33 | + <CurrencyInput |
| 34 | + name="amount" |
| 35 | + value="" |
| 36 | + setEnteredAmount={setEnteredAmount} |
| 37 | + onChange={onChange} |
| 38 | + /> |
| 39 | + ); |
| 40 | + |
| 41 | + const input = screen.getByRole("textbox"); |
| 42 | + await userEvent.type(input, "5.50"); |
| 43 | + |
| 44 | + expect(setEnteredAmount).toHaveBeenLastCalledWith(5.5); |
| 45 | + }); |
| 46 | + |
| 47 | + it("does not strip a trailing decimal when the parent re-renders with the same float", async () => { |
| 48 | + const setEnteredAmount = vi.fn(); |
| 49 | + const onChange = vi.fn(); |
| 50 | + |
| 51 | + const { rerender } = render( |
| 52 | + <CurrencyInput |
| 53 | + name="amount" |
| 54 | + value="" |
| 55 | + setEnteredAmount={setEnteredAmount} |
| 56 | + onChange={onChange} |
| 57 | + /> |
| 58 | + ); |
| 59 | + |
| 60 | + const input = screen.getByRole("textbox"); |
| 61 | + await userEvent.type(input, "5."); |
| 62 | + |
| 63 | + // userEvent.type flushes all effects before returning, so skipNextSyncRef.current |
| 64 | + // is true when rerender delivers value={5}. The effect fires, sees the flag, and |
| 65 | + // skips the sync — preserving the trailing decimal. |
| 66 | + rerender( |
| 67 | + <CurrencyInput |
| 68 | + name="amount" |
| 69 | + value={5} |
| 70 | + setEnteredAmount={setEnteredAmount} |
| 71 | + onChange={onChange} |
| 72 | + /> |
| 73 | + ); |
| 74 | + |
| 75 | + expect(input).toHaveDisplayValue(/5\./); |
| 76 | + |
| 77 | + // Simulate the parent then pushing a genuinely new value (no user typing). |
| 78 | + // The guard should allow this through and update the display. |
| 79 | + rerender( |
| 80 | + <CurrencyInput |
| 81 | + name="amount" |
| 82 | + value={7} |
| 83 | + setEnteredAmount={setEnteredAmount} |
| 84 | + onChange={onChange} |
| 85 | + /> |
| 86 | + ); |
| 87 | + |
| 88 | + expect(input).toHaveDisplayValue("7"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("clears the input when parent resets value to empty", () => { |
| 92 | + const setEnteredAmount = vi.fn(); |
| 93 | + const onChange = vi.fn(); |
| 94 | + |
| 95 | + const { rerender } = render( |
| 96 | + <CurrencyInput |
| 97 | + name="amount" |
| 98 | + value={500} |
| 99 | + setEnteredAmount={setEnteredAmount} |
| 100 | + onChange={onChange} |
| 101 | + /> |
| 102 | + ); |
| 103 | + |
| 104 | + rerender( |
| 105 | + <CurrencyInput |
| 106 | + name="amount" |
| 107 | + value="" |
| 108 | + setEnteredAmount={setEnteredAmount} |
| 109 | + onChange={onChange} |
| 110 | + /> |
| 111 | + ); |
| 112 | + |
| 113 | + expect(screen.getByRole("textbox")).toHaveDisplayValue(""); |
| 114 | + }); |
| 115 | +}); |
0 commit comments