Skip to content

Commit

Permalink
chore: should not be named store but engine
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Dec 19, 2024
1 parent fa213c0 commit 1a05490
Show file tree
Hide file tree
Showing 20 changed files with 749 additions and 734 deletions.
1,130 changes: 565 additions & 565 deletions EXAMPLE_VAULT/.obsidian/plugins/modal-form/data.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/FormModal.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { App } from "obsidian";
import { FormDefinition } from "src/core/formDefinition";
import { makeFormEngine } from "src/store/formStore";
import { makeFormEngine } from "src/store/formEngine";
import RenderField from "./views/components/Form/RenderField.svelte";
export let app: App;
export let reportFormErrors: (errors: string[]) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/FormModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import FormModalComponent from "./FormModal.svelte";
import FormResult, { type ModalFormData } from "./core/FormResult";
import { formDataFromFormDefaults } from "./core/formDataFromFormDefaults";
import type { FormDefinition, FormOptions } from "./core/formDefinition";
import { FormEngine, makeFormEngine } from "./store/formStore";
import { FormEngine, makeFormEngine } from "./store/formEngine";
import { log_notice } from "./utils/Log";

export type SubmitFn = (formResult: FormResult) => void;
Expand Down
168 changes: 168 additions & 0 deletions src/store/formEngine.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { get } from "svelte/store";
import { makeFormEngine } from "./formEngine";

describe("Form Engine", () => {
it("should update form fields correctly", () => {
const onSubmitMock = jest.fn();
const formEngine = makeFormEngine({
onSubmit: onSubmitMock,
onCancel: console.log,
});

// Add fields to the form
const field1 = formEngine.addField({ name: "fieldName1" });
const field2 = formEngine.addField({ name: "fieldName2" });
// Update field values
field1.value.set("value1");
field2.value.set("value2");

// Trigger form submission
formEngine.triggerSubmit();
// Assert that the onSubmit callback is called with the correct values
expect(onSubmitMock).toHaveBeenCalledWith({
fieldName1: "value1",
fieldName2: "value2",
});
});

it("should handle field errors correctly", () => {
const onSubmitMock = jest.fn();
const formEngine = makeFormEngine({
onSubmit: onSubmitMock,
onCancel: console.log,
});
// Add a field to the form
const field1 = formEngine.addField({
name: "fieldName1",
isRequired: true,
});
// Update field value with an empty string
field1.value.set("");
// Trigger form submission
formEngine.triggerSubmit();
// Assert that the form is not valid
expect(get(formEngine.isValid)).toBe(false);
// Assert that the onSubmit callback is not called
expect(onSubmitMock).not.toHaveBeenCalled();
// Assert that the field has errors
expect(get(field1.errors)).toEqual(["'fieldName1' is required"]);
});
it("field errors should prefer field label over field name", () => {
const onSubmitMock = jest.fn();
const formEngine = makeFormEngine({
onSubmit: onSubmitMock,
onCancel: console.log,
});
// Add a field to the form
const field1 = formEngine.addField({
name: "fieldName1",
label: "Field Label",
isRequired: true,
});
// Update field value with an empty string
field1.value.set("");
// Trigger form submission
formEngine.triggerSubmit();
// Assert that the form is not valid
expect(get(formEngine.isValid)).toBe(false);
// Assert that the onSubmit callback is not called
expect(onSubmitMock).not.toHaveBeenCalled();
// Assert that the field has errors
expect(get(field1.errors)).toEqual(["'Field Label' is required"]);
});
it("Clears the errors when a value is set", () => {
const onSubmitMock = jest.fn();
const formEngine = makeFormEngine({
onSubmit: onSubmitMock,
onCancel: console.log,
});
// Add a field to the form
const field1 = formEngine.addField({
name: "fieldName1",
label: "Field Label",
isRequired: true,
});
// Update field value with an empty string
field1.value.set("");
formEngine.triggerSubmit();
expect(get(formEngine.isValid)).toBe(false);
expect(onSubmitMock).not.toHaveBeenCalled();
expect(get(field1.errors)).toEqual(["'Field Label' is required"]);
// Set the field value, clearing the errors
field1.value.set("value");
// Assert that the field errors are cleared
expect(get(field1.errors)).toEqual([]);
formEngine.triggerSubmit();
expect(get(formEngine.isValid)).toBe(true);
// 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({
onSubmit: onSubmitMock,
defaultValues,
onCancel: console.log,
});

// 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({
onSubmit: onSubmitMock,
defaultValues,
onCancel: console.log,
});

// Add fields to the form
const field1 = formEngine.addField({ name: "fieldName1" });
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",
});
});
it("should flag the form as cancelled and call the onCancel callback when the cancel button is clicked", () => {
const onCancelMock = jest.fn();
const formEngine = makeFormEngine({
onSubmit: console.log,
onCancel: onCancelMock,
});
// Add a field to the form
const field1 = formEngine.addField({ name: "fieldName1" });
// Update field value with an empty string
field1.value.set("");
// Trigger form submission
formEngine.triggerCancel();
// Assert that the onCancel callback is called
expect(onCancelMock).toHaveBeenCalled();
// Assert that the form is not valid
// expect(get(formEngine.isValid)).toBe(false); // is it worth checking this?
});
});
File renamed without changes.
153 changes: 0 additions & 153 deletions src/store/formStore.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/views/components/Form/DocumentBlock.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as TE from "fp-ts/TaskEither";
import { App, sanitizeHTMLToDom } from "obsidian";
import { input } from "src/core";
import { FieldValue, FormEngine } from "src/store/formStore";
import { FieldValue, FormEngine } from "src/store/formEngine";
import { notifyError } from "src/utils/Log";
import { Subscriber } from "svelte/store";
type GetSubscription<T> = T extends Subscriber<infer U> ? U : never;
Expand Down
2 changes: 1 addition & 1 deletion src/views/components/Form/InputDataview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { App } from "obsidian";
import { input as I } from "src/core";
import { FieldDefinition } from "src/core/formDefinition";
import { FieldValue } from "src/store/formStore";
import { FieldValue } from "src/store/formEngine";
import { DataviewSuggest } from "src/suggesters/suggestFromDataview";
import { Readable, Writable } from "svelte/store";
import ObsidianInputWrapper from "./ObsidianInputWrapper.svelte";
Expand Down
2 changes: 1 addition & 1 deletion src/views/components/Form/InputField.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
/** This component is only meant for the most basic input types. If any of those types starts to require more fancy setup it should be moved to it's own separated input*/
import { type FieldValue } from "src/store/formStore";
import { type FieldValue } from "src/store/formEngine";
import { Writable } from "svelte/store";
export let value: Writable<FieldValue>;
export let inputType: "number" | "text" | "date" | "time" | "datetime" | "email" | "tel";
Expand Down
2 changes: 1 addition & 1 deletion src/views/components/Form/InputFolder.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { App, SearchComponent, Setting } from "obsidian";
import { FieldDefinition } from "src/core/formDefinition";
import { FieldValue } from "src/store/formStore";
import { FieldValue } from "src/store/formEngine";
import { FolderSuggest } from "src/suggesters/suggestFolder";
import { Writable } from "svelte/store";
import { useSetting } from "./useObsidianSetting";
Expand Down
2 changes: 1 addition & 1 deletion src/views/components/Form/InputNote.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { App } from "obsidian";
import { input as I } from "src/core";
import { FieldDefinition } from "src/core/formDefinition";
import { FieldValue } from "src/store/formStore";
import { FieldValue } from "src/store/formEngine";
import { FileSuggest } from "src/suggesters/suggestFile";
import { Readable, Writable } from "svelte/store";
import ObsidianInputWrapper from "./ObsidianInputWrapper.svelte";
Expand Down
2 changes: 1 addition & 1 deletion src/views/components/Form/InputTag.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { App } from "obsidian";
import { input as I } from "src/core";
import { FieldValue } from "src/store/formStore";
import { FieldValue } from "src/store/formEngine";
import { Readable, Writable } from "svelte/store";
import MultiSelect from "../MultiSelect.svelte";
import { MultiSelectTags } from "../MultiSelectModel";
Expand Down
Loading

0 comments on commit 1a05490

Please sign in to comment.