import React from "react"; import { render, cleanup } from "@testing-library/react"; import FormattedNumber from "../FormattedNumber"; describe("FormattedNumber Tests", () => { beforeEach(() => cleanup()); afterEach(() => cleanup()); describe("emptyPlaceHolder", () => { it("no props where provided", () => { const { getByText } = render(); expect(getByText("N/A")).toBeTruthy(); }); it("invalid value - empty string", () => { const { getByText } = render(); expect(getByText("N/A")).toBeTruthy(); }); it("invalid value - mixed content", () => { const { getByText } = render(); expect(getByText("N/A")).toBeTruthy(); }); it("invalid value - mixed content", () => { const emptyPlaceHolderText = "Test"; const { getByText } = render(); expect(getByText(emptyPlaceHolderText)).toBeTruthy(); }); }); describe("Correct format", () => { const smallNumber = 98; const largeNumber = 987654321; const decimalNumber = 987654321.123456; it("should format small number without sign", () => { const expectedText = "98"; const { getByText } = render(); expect(getByText(expectedText)).toBeTruthy(); }); it("should format large number with sign", () => { const expectedText = "987.6543M"; const { getByText } = render(); expect(getByText(expectedText)).toBeTruthy(); }); it("should format large number without sign", () => { const expectedText = "987,654,321"; const { getByText } = render(); expect(getByText(expectedText)).toBeTruthy(); }); it("should format large number without sign and limited decimal numbers", () => { const expectedText = "987,654,321.123"; const { getByText } = render( ); expect(getByText(expectedText)).toBeTruthy(); }); it("should format with MIN precision for precision below MIN", () => { const expectedText = "987,654,321"; const { getByText } = render( ); expect(getByText(expectedText)).toBeTruthy(); }); it("should format with MAX precision for precision above MAX", () => { const expectedText = "987,654,321.123456"; const { getByText } = render( ); expect(getByText(expectedText)).toBeTruthy(); }); }); describe("local support", () => { const value = 456; it("should use handle unsupported local", () => { const badLocal = "bad"; const { getByText } = render(); expect(getByText("456")).toBeTruthy(); }); }); describe("forward ref", () => { it("should be able to forward ref", () => { const ref = React.createRef(); render(); expect(ref.current.classList.contains("ref-class-name")).toEqual(true); }); }); describe("Prefix && Suffix", () => { const value = 456; const prefix = "pref"; const suffix = "suf"; it("should render predix and suffix in the correct order if provided", () => { const { container } = render(); const { childNodes } = container.firstChild; expect(childNodes.length).toBe(3); expect(childNodes[0].textContent).toBe(prefix); expect(childNodes[2].textContent).toBe(suffix); }); it("should render rtl", () => { const { container } = render( ); const { childNodes } = container.firstChild; expect(childNodes.length).toBe(3); expect(childNodes[0].textContent).toBe(suffix); expect(childNodes[2].textContent).toBe(prefix); }); }); });