generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: should not be named store but engine
- Loading branch information
1 parent
fa213c0
commit 1a05490
Showing
20 changed files
with
749 additions
and
734 deletions.
There are no files selected for viewing
1,130 changes: 565 additions & 565 deletions
1,130
EXAMPLE_VAULT/.obsidian/plugins/modal-form/data.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.