import { assert } from "chai" import { render, screen, cleanup } from "@testing-library/react" import userEvent from "@testing-library/user-event" import NumberAnswerComponent from "../../src/answers/NumberAnswerComponent" import React, { createRef } from "react" describe("NumberAnswerComponent", function () { afterEach(function () { cleanup() }) it("records decimal number", async function () { let capturedValue: any = null render( { capturedValue = value }} /> ) const input = screen.getByRole("textbox") await userEvent.clear(input) await userEvent.type(input, "123.4") await userEvent.tab() assert.equal(capturedValue, 123.4) }) it("records whole number", async function () { let capturedValue: any = null render( { capturedValue = value }} /> ) const input = screen.getByRole("textbox") await userEvent.clear(input) await userEvent.type(input, "123") await userEvent.tab() assert.equal(capturedValue, 123) }) it("enforces decimal number", async function () { let capturedValue: any = undefined render( { capturedValue = value }} /> ) const input = screen.getByRole("textbox") await userEvent.clear(input) await userEvent.type(input, "123.4abc") await userEvent.tab() assert.equal(capturedValue, null) }) it("enforces whole number", async function () { let onChangeCalled = false render( { onChangeCalled = true }} /> ) const input = screen.getByRole("textbox") await userEvent.clear(input) await userEvent.type(input, "123.4") await userEvent.tab() assert(!onChangeCalled, "onChange should not be called for invalid whole number") }) it("validates decimal number", async function () { const ref = createRef() render( {}} /> ) const input = screen.getByRole("textbox") await userEvent.clear(input) await userEvent.type(input, "123.4") await userEvent.tab() assert(!ref.current!.validate(), "Valid decimal number should not return validation error") }) it("fails validation if invalid whole number", async function () { const ref = createRef() render( {}} /> ) const input = screen.getByRole("textbox") await userEvent.clear(input) await userEvent.type(input, "123abc") await userEvent.tab() assert(ref.current!.validate(), "Invalid whole number should return validation error") }) })