import { assert } from "chai" import { render, screen, fireEvent, cleanup } from "@testing-library/react" import userEvent from "@testing-library/user-event" import UnitsAnswerComponent from "../../src/answers/UnitsAnswerComponent" import React from "react" import { defaultT } from "ez-localize" describe("UnitsAnswerComponent", function () { afterEach(function () { cleanup() }) it("allows changing of units", async function () { let capturedValue: any = null render( { capturedValue = value }} units={[ { id: "a", label: { _base: "en", es: "AA" }, hint: { _base: "en", es: "a-hint" } }, { id: "b", label: { _base: "en", es: "BB" } }, { id: "c", label: { _base: "en", es: "CC" } } ]} prefix={false} decimal={true} formCtx={{ T: defaultT, locale: "en" } as any} /> ) const unitSelect = screen.getByRole("combobox") fireEvent.change(unitSelect, { target: { value: "b" } }) assert.equal(capturedValue.units, "b") }) it("allows changing of decimal quantity", async function () { let capturedValue: any = null render( { capturedValue = value }} units={[ { id: "a", label: { _base: "en", es: "AA" }, hint: { _base: "en", es: "a-hint" } }, { id: "b", label: { _base: "en", es: "BB" } }, { id: "c", label: { _base: "en", es: "CC" } } ]} prefix={false} decimal={true} formCtx={{ T: defaultT, locale: "en" } as any} /> ) const quantityInput = screen.getByRole("textbox") await userEvent.clear(quantityInput) await userEvent.type(quantityInput, "13.33") await userEvent.tab() assert.equal(capturedValue.quantity, 13.33) }) it("allows changing of whole quantity", async function () { let capturedValue: any = null render( { capturedValue = value }} units={[ { id: "a", label: { _base: "en", es: "AA" }, hint: { _base: "en", es: "a-hint" } }, { id: "b", label: { _base: "en", es: "BB" } }, { id: "c", label: { _base: "en", es: "CC" } } ]} prefix={false} formCtx={{ T: defaultT, locale: "en" } as any} /> ) const quantityInput = screen.getByRole("textbox") await userEvent.clear(quantityInput) await userEvent.type(quantityInput, "13") await userEvent.tab() assert.equal(capturedValue.quantity, 13) }) it("defaults unit", async function () { let capturedValue: any = null render( { capturedValue = value }} units={[ { id: "a", label: { _base: "en", es: "AA" }, hint: { _base: "en", es: "a-hint" } }, { id: "b", label: { _base: "en", es: "BB" } }, { id: "c", label: { _base: "en", es: "CC" } } ]} prefix={false} decimal={true} formCtx={{ T: defaultT, locale: "en" } as any} /> ) const quantityInput = screen.getByRole("textbox") await userEvent.clear(quantityInput) await userEvent.type(quantityInput, "13.33") await userEvent.tab() assert.equal(capturedValue.quantity, 13.33) assert.equal(capturedValue.units, "b") }) })