import { Nullable } from "@reins/types"; import { FormAdapter } from "../form-adapter"; type InitialValues = { name: string | null; surname: string | null; age: number | null; address: Nullable<{ street: string; city: string; country: string; }>; }; const initialValues: InitialValues = { name: null, surname: null, age: null, address: null, }; describe("Form Adapter [ Values ]", () => { let form = new FormAdapter(initialValues); beforeEach(() => { form = new FormAdapter(initialValues); }); it("should allow to set correct values", async () => { form.setFieldValue("name", "John"); form.setFieldValue("surname", "Doe"); form.setFieldValue("age", 30); form.setFieldValue("address", { street: "New street", city: "New city", country: "New country", }); expect(form.getValues()).toEqual({ name: "John", surname: "Doe", age: 30, address: { street: "New street", city: "New city", country: "New country", }, }); }); it("should throw when assigning values to not defined value", async () => { // Since initial value is null, we cannot assign nested values expect(() => form.setFieldValue("address.street", "Some street")).toThrow(); expect(() => form.setFieldValue("address.city", "Some city")).toThrow(); expect(() => form.setFieldValue("address.country", "Some country")).toThrow(); }); });