import { renderHook } from "@testing-library/react"; import { numberFormatter, unwrapFirstValue, useUnwrappedHandler, useWrappedValue, wrapValue, } from "./helpers"; describe("Slider helpers", () => { describe("unwrapFirstValue", () => { it("should return undefined if the value is undefined", () => { expect(unwrapFirstValue(undefined)).toBeUndefined(); }); it("should return undefined if the value is an empty array", () => { expect(unwrapFirstValue([])).toBeUndefined(); }); it("should return the first value if the value is an array with one element", () => { expect(unwrapFirstValue([1])).toBe(1); }); it("should return the first value if the value is an array with more than one element", () => { expect(unwrapFirstValue([1, 2])).toBe(1); }); }); describe("wrapValue", () => { it("should return undefined if the value is undefined", () => { expect(wrapValue(undefined)).toBeUndefined(); }); it("should return the value if it is not an array", () => { expect(wrapValue(1)).toEqual([1]); }); it("should return an array of array if value is an array", () => { expect(wrapValue([1])).toEqual([[1]]); }); }); describe("useWrappedValue", () => { it("should return undefined if the value is undefined", () => { const { result } = renderHook(() => useWrappedValue(undefined)); expect(result.current).toBeUndefined(); }); it("should return the value if it is not an array", () => { const { result } = renderHook(() => useWrappedValue(1)); expect(result.current).toEqual([1]); }); it("should return an array of array if value is an array", () => { const { result } = renderHook(() => useWrappedValue([1])); expect(result.current).toEqual([[1]]); }); }); describe("useUnwrappedHandler", () => { it("should call the handler with the unwrapped value", () => { const handler = vi.fn(); const { result } = renderHook(() => useUnwrappedHandler(handler)); result.current([1]); expect(handler).toHaveBeenCalledWith(1); }); }); describe("numberFormatter", () => { it("returns an empty string when no params are passed", () => { expect(numberFormatter(undefined)).toEqual(""); }); it("returns the unformatted value when no formatter is present", () => { expect(numberFormatter(43)).toEqual("43"); expect(numberFormatter("QQ")).toEqual("QQ"); }); it("returns the unformatted value when format options are empty", () => { const formatOptions = {}; expect(numberFormatter(35, formatOptions)).toEqual("35"); }); it("returns a formatted value, defaulting to en-GB locale", () => { const formatOptions: Intl.NumberFormatOptions = { style: "currency", currency: "EUR", }; expect(numberFormatter(35.4, formatOptions)).toEqual("€35.40"); }); it("respects a locale if supplied", () => { const formatOptions: Intl.NumberFormatOptions = { style: "currency", currency: "EUR", }; expect(numberFormatter(35.4, formatOptions, "de-DE")).toEqual("35,40 €"); }); }); });