Skip to content

Commit

Permalink
chore: handle default values in formEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Dec 7, 2023
1 parent 49da4ad commit 960179c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
43 changes: 43 additions & 0 deletions src/store/formStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
});
});
3 changes: 2 additions & 1 deletion src/store/formStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export type OnFormSubmit<T> = (values: Record<string, T>) => void;

export function makeFormEngine<T extends FieldValue>(
onSubmit: OnFormSubmit<T>,
defaultValues: Record<string, T> = {},
): FormEngine<T> {
const formStore: Writable<FormStore<T>> = writable({ fields: {} });
// Creates helper functions to modify the store immutably
Expand All @@ -153,7 +154,7 @@ export function makeFormEngine<T extends FieldValue>(
...form,
fields: {
...form.fields,
[name]: { value: O.none, name, errors, rules },
[name]: { value: O.fromNullable(defaultValues[name]), name, errors, rules },
},
};
});
Expand Down

0 comments on commit 960179c

Please sign in to comment.