From 960179c216fdc82d672e5a7dd565a6e6999085a3 Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Thu, 7 Dec 2023 12:17:45 +0100 Subject: [PATCH] chore: handle default values in formEngine --- package.json | 2 +- src/store/formStore.test.ts | 43 +++++++++++++++++++++++++++++++++++++ src/store/formStore.ts | 3 ++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a1756f31..183ca10a 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "dev": "node esbuild.config.mjs", "build": "npm run check && node esbuild.config.mjs production", "check": "svelte-check --tsconfig tsconfig.json", - "test": "jest", + "test": "jest --detectOpenHandles", "test-w": "jest --watch --no-cache", "version": "node version-bump.mjs && git add manifest.json versions.json" }, diff --git a/src/store/formStore.test.ts b/src/store/formStore.test.ts index 63b5fb79..bfb9cd9c 100644 --- a/src/store/formStore.test.ts +++ b/src/store/formStore.test.ts @@ -85,4 +85,47 @@ describe("Form Engine", () => { // Assert that the onSubmit callback is called expect(onSubmitMock).toHaveBeenCalledWith({ fieldName1: "value" }); }); + + it("should use default values when field has not been changed, but if they change it should use the value", () => { + const onSubmitMock = jest.fn(); + const defaultValues = { + fieldName1: "default1", + fieldName2: "default2", + }; + const formEngine = makeFormEngine(onSubmitMock, defaultValues); + + // Add fields to the form + const field1 = formEngine.addField({ name: "fieldName1" }); + const field2 = formEngine.addField({ name: "fieldName2" }); + + // Assert that the default values are set + expect(get(field1.value)).toBe("default1"); + expect(get(field2.value)).toBe("default2"); + field1.value.set("value1"); + formEngine.triggerSubmit(); + // Assert that the onSubmit callback is called with the correct values + expect(onSubmitMock).toHaveBeenCalledWith({ + fieldName1: "value1", + fieldName2: "default2", + }); + }); + it("empty values that have not been modified should not be in the result", () => { + const onSubmitMock = jest.fn(); + const defaultValues = { + fieldName1: "default1", + }; + const formEngine = makeFormEngine(onSubmitMock, defaultValues); + + // Add fields to the form + const field1 = formEngine.addField({ name: "fieldName1" }); + const field2 = formEngine.addField({ name: "fieldName2" }); + + // Assert that the default values are set + expect(get(field1.value)).toBe("default1"); + formEngine.triggerSubmit(); + // Assert that the onSubmit callback is called with the correct values + expect(onSubmitMock).toHaveBeenCalledWith({ + fieldName1: "default1", + }); + }); }); diff --git a/src/store/formStore.ts b/src/store/formStore.ts index 3e4acf5c..ccfe842d 100644 --- a/src/store/formStore.ts +++ b/src/store/formStore.ts @@ -142,6 +142,7 @@ export type OnFormSubmit = (values: Record) => void; export function makeFormEngine( onSubmit: OnFormSubmit, + defaultValues: Record = {}, ): FormEngine { const formStore: Writable> = writable({ fields: {} }); // Creates helper functions to modify the store immutably @@ -153,7 +154,7 @@ export function makeFormEngine( ...form, fields: { ...form.fields, - [name]: { value: O.none, name, errors, rules }, + [name]: { value: O.fromNullable(defaultValues[name]), name, errors, rules }, }, }; });