Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: should not be named store but engine #359

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { TemplateService } from "./core/template/TemplateService";
import { getTemplateService } from "./core/template/getTemplateService";
import { retryForm } from "./core/template/retryForm";
import { executeTemplate } from "./core/template/templateParser";
import { settingsStore } from "./store/store";
import { settingsStore } from "./store/SettngsStore";
import { FormPickerModal } from "./suggesters/FormPickerModal";
import { NewNoteModal } from "./suggesters/NewNoteModal";
import { log_error, log_notice, notifyWarning } from "./utils/Log";
Expand Down
File renamed without changes.
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.

8 changes: 4 additions & 4 deletions src/views/EditFormView.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ModalFormPlugin from "../main";
import { ItemView, type ViewStateResult, WorkspaceLeaf } from "obsidian";
import type { FormDefinition, EditableFormDefinition } from "../core/formDefinition";
import FormEditor from './FormBuilder.svelte'
import { settingsStore } from "src/store/SettngsStore";
import { log_notice } from "src/utils/Log";
import { settingsStore } from "src/store/store";
import type { EditableFormDefinition, FormDefinition } from "../core/formDefinition";
import ModalFormPlugin from "../main";
import FormEditor from './FormBuilder.svelte';

export const EDIT_FORM_VIEW = "modal-form-edit-form-view";

Expand Down
Loading
Loading