generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(form-runtime): ensure that default values are correctly compiled
- Loading branch information
1 parent
711865c
commit afae1a0
Showing
8 changed files
with
169 additions
and
37 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { FormDefinition } from "./formDefinition"; | ||
import { formDataFromFormDefaults } from "./formDataFromFormDefaults"; | ||
|
||
describe("formDataFromFormOptions", () => { | ||
const fields: FormDefinition["fields"] = [ | ||
{ name: "age", description: "", input: { type: "number" } }, | ||
{ | ||
name: "name", | ||
description: "Enter your name", | ||
input: { type: "text" }, | ||
}, | ||
{ name: "age", description: "", input: { type: "number" } }, | ||
{ | ||
name: "hobbies", | ||
description: "Select your hobbies", | ||
input: { | ||
type: "multiselect", | ||
source: "fixed", | ||
multi_select_options: ["reading", "swimming", "running"], | ||
}, | ||
}, | ||
{ name: "isEmployed", description: "", input: { type: "toggle" } }, | ||
]; | ||
|
||
it("should return the correct form data when all values are valid", () => { | ||
const values = { | ||
name: "John Doe", | ||
age: 30, | ||
hobbies: ["reading", "swimming"], | ||
isEmployed: true, | ||
}; | ||
|
||
const expectedFormData = { | ||
name: "John Doe", | ||
age: 30, | ||
hobbies: ["reading", "swimming"], | ||
isEmployed: true, | ||
}; | ||
|
||
const result = formDataFromFormDefaults(fields, values); | ||
expect(result).toEqual(expectedFormData); | ||
}); | ||
|
||
it("should ignore invalid keys and return the correct form data", () => { | ||
const values = { | ||
name: "John Doe", | ||
age: 30, | ||
hobbies: ["reading", "swimming"], | ||
isEmployed: true, | ||
invalidKey: "invalidValue", | ||
}; | ||
|
||
const expectedFormData = { | ||
name: "John Doe", | ||
age: 30, | ||
hobbies: ["reading", "swimming"], | ||
isEmployed: true, | ||
}; | ||
|
||
const result = formDataFromFormDefaults(fields, values); | ||
expect(result).toEqual(expectedFormData); | ||
}); | ||
|
||
it("should ensure toggles always have avalue even when no values are provided", () => { | ||
const values = {}; | ||
|
||
const expectedFormData = { isEmployed: false }; | ||
|
||
const result = formDataFromFormDefaults(fields, values); | ||
expect(result).toEqual(expectedFormData); | ||
}); | ||
|
||
it("should return the correct form data when some values are missing", () => { | ||
const values = { | ||
name: "John Doe", | ||
age: 30, | ||
isEmployed: true, | ||
}; | ||
|
||
const expectedFormData = { | ||
name: "John Doe", | ||
age: 30, | ||
isEmployed: true, | ||
}; | ||
|
||
const result = formDataFromFormDefaults(fields, values); | ||
expect(result).toEqual(expectedFormData); | ||
}); | ||
}); |
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,43 @@ | ||
import * as R from 'fp-ts/Record'; | ||
import * as O from 'fp-ts/Option'; | ||
import { log_error } from "../utils/Log"; | ||
import { ModalFormError } from "../utils/ModalFormError"; | ||
import { FormDefinition } from './formDefinition'; | ||
import { A, pipe } from '@std'; | ||
import { ModalFormData, isPrimitiveArray, isPrimitive, Val } from './FormResult'; | ||
|
||
|
||
/** | ||
* Given a form definition fields and a record containing the expected default values | ||
* for the form, return a record containing only the values that are present in the form definition | ||
* and filters out invalid values | ||
* */ | ||
export function formDataFromFormDefaults(fields: FormDefinition['fields'], values: Record<string, unknown>): ModalFormData { | ||
const result: ModalFormData = {}; | ||
const invalidKeys = []; | ||
for (const [key, value] of Object.entries(values)) { | ||
if (Array.isArray(value) && isPrimitiveArray(value)) { | ||
result[key] = value; | ||
} else if (isPrimitive(value)) { | ||
result[key] = value; | ||
} else { | ||
invalidKeys.push(key); | ||
} | ||
} | ||
if (invalidKeys.length > 0) { | ||
log_error(new ModalFormError(`Invalid keys in form options: ${invalidKeys.join(', ')}`)); | ||
} | ||
return pipe( | ||
fields, | ||
A.map((field) => { | ||
return pipe( | ||
result[field.name], | ||
O.fromNullable, | ||
O.match(() => field.input.type === 'toggle' ? O.some(false) : O.none, O.some), | ||
O.map((value) => [field.name, value] as [string, Val]) | ||
); | ||
}), | ||
A.compact, | ||
R.fromEntries | ||
); | ||
} |
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