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.
fix: dataview list format is now correct
fix: add tests for result class and helpers
- Loading branch information
1 parent
41790ff
commit e4eea18
Showing
6 changed files
with
108 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,64 @@ | ||
jest.mock("obsidian"); | ||
import FormResult, { ModalFormData } from "./FormResult"; | ||
|
||
describe("FormResult", () => { | ||
const formData: ModalFormData = { | ||
name: "John Doe", | ||
age: 30, | ||
hobbies: ["reading", "swimming"], | ||
isEmployed: true, | ||
}; | ||
|
||
describe("constructor", () => { | ||
it("should create a new FormResult instance with the provided data and status", () => { | ||
const result = new FormResult(formData, "ok"); | ||
expect(result).toBeInstanceOf(FormResult); | ||
expect(result.getData()).toEqual(formData); | ||
expect(result.status).toEqual("ok"); | ||
}); | ||
}); | ||
|
||
describe("asFrontmatterString", () => { | ||
it("should return the data as a YAML frontmatter string", () => { | ||
const result = new FormResult(formData, "ok"); | ||
const expectedOutput = `name: John Doe | ||
age: 30 | ||
hobbies: | ||
- reading | ||
- swimming | ||
isEmployed: true | ||
`; | ||
expect(result.asFrontmatterString()).toEqual(expectedOutput); | ||
}); | ||
}); | ||
|
||
describe("asDataviewProperties", () => { | ||
it("should return the data as a string of dataview properties", () => { | ||
const result = new FormResult(formData, "ok"); | ||
const expectedOutput = `name:: John Doe | ||
age:: 30 | ||
hobbies:: "reading","swimming" | ||
isEmployed:: true`; | ||
expect(result.asDataviewProperties()).toEqual(expectedOutput); | ||
}); | ||
}); | ||
|
||
describe("getData", () => { | ||
it("should return a copy of the data contained in the FormResult instance", () => { | ||
const result = new FormResult(formData, "ok"); | ||
const dataCopy = result.getData(); | ||
expect(dataCopy).toEqual(formData); | ||
expect(dataCopy).not.toBe(formData); | ||
}); | ||
}); | ||
|
||
describe("asString", () => { | ||
it("should return the data formatted as a string matching the provided template", () => { | ||
const result = new FormResult(formData, "ok"); | ||
const template = "My name is {{name}}, and I am {{age}} years old."; | ||
const expectedOutput = | ||
"My name is John Doe, and I am 30 years old."; | ||
expect(result.asString(template)).toEqual(expectedOutput); | ||
}); | ||
}); | ||
}); |
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,4 @@ | ||
import { stringify } from 'yaml' | ||
export function stringifyYaml(data: unknown) { | ||
return stringify(data) | ||
} |