Skip to content

Commit

Permalink
fix: dataview list format is now correct
Browse files Browse the repository at this point in the history
fix: add tests for result class and helpers
  • Loading branch information
danielo515 committed Oct 26, 2023
1 parent 41790ff commit e4eea18
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 14 deletions.
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "npm run check && node esbuild.config.mjs production",
"check": "svelte-check --tsconfig tsconfig.json",
"test": "jest",
"test-w": "jest --watch",
"test-w": "jest --watch --no-cache",
"version": "node version-bump.mjs && git add manifest.json versions.json"
},
"keywords": [],
Expand All @@ -31,11 +31,12 @@
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tslib": "2.4.0",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"yaml": "^2.3.3"
},
"dependencies": {
"fp-ts": "^2.16.1",
"fuse.js": "^6.6.2",
"valibot": "^0.19.0"
}
}
}
10 changes: 5 additions & 5 deletions src/FormModal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { App, Modal, Platform, Setting } from "obsidian";
import MultiSelect from "./views/components/MultiSelect.svelte";
import FormResult, { formDataFromFormOptions, type ModalFormData } from "./FormResult";
import FormResult, { formDataFromFormOptions, type ModalFormData } from "./core/FormResult";
import { exhaustiveGuard } from "./safety";
import { get_tfiles_from_folder } from "./utils/files";
import type { FormDefinition, FormOptions } from "./core/formDefinition";
Expand Down Expand Up @@ -39,7 +39,7 @@ export class FormModal extends Modal {
fieldBase.setClass('modal-form-textarea')
return fieldBase.addTextArea((textEl) => {
if (typeof initialValue === 'string') { textEl.setValue(initialValue); }
textEl.onChange(value => {
textEl.onChange((value) => {
this.formResult[definition.name] = value;
});
textEl.inputEl.rows = 6;
Expand Down Expand Up @@ -133,7 +133,7 @@ export class FormModal extends Modal {
this.formResult[definition.name] = this.formResult[definition.name] || []
const options = fieldInput.source == 'fixed'
? fieldInput.multi_select_options
: get_tfiles_from_folder(fieldInput.folder, this.app).map(file => file.basename);
: get_tfiles_from_folder(fieldInput.folder, this.app).map((file) => file.basename);
this.svelteComponents.push(new MultiSelect({
target: fieldBase.controlEl,
props: {
Expand Down Expand Up @@ -205,7 +205,7 @@ export class FormModal extends Modal {
return exhaustiveGuard(type);
}
});

const submit = () => {
this.onSubmit(new FormResult(this.formResult, "ok"));
this.close();
Expand All @@ -230,7 +230,7 @@ export class FormModal extends Modal {

onClose() {
const { contentEl } = this;
this.svelteComponents.forEach(component => component.$destroy())
this.svelteComponents.forEach((component) => component.$destroy())
contentEl.empty();
this.formResult = {};
}
Expand Down
64 changes: 64 additions & 0 deletions src/core/FormResult.test.ts
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);
});
});
});
21 changes: 18 additions & 3 deletions src/FormResult.ts → src/core/FormResult.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { stringifyYaml } from "obsidian";
import { log_error } from "./utils/Log";
import { ModalFormError } from "./utils/Error";
import { log_error } from "../utils/Log";
import { ModalFormError } from "../utils/Error";
import { object, optional, array, string, coerce } from "valibot";
import { parse } from "@std";

type ResultStatus = "ok" | "cancelled";

Expand All @@ -15,6 +17,17 @@ function isPrimitiveArray(value: unknown): value is string[] {
return Array.isArray(value) && value.every(isPrimitive)
}

const KeysSchema = array(coerce(string(), String))

const PickOmitSchema = object({
pick: optional(KeysSchema),
omit: optional(KeysSchema),
});

console.log(parse(PickOmitSchema, { pick: ['a', 'b'] }))
console.log(parse(PickOmitSchema, { pick: [11], omit: ['a', 'b'] }))
console.log(parse(PickOmitSchema, undefined))

export function formDataFromFormOptions(values: Record<string, unknown>) {
const result: ModalFormData = {};
const invalidKeys = []
Expand Down Expand Up @@ -44,7 +57,9 @@ export default class FormResult {
*/
asDataviewProperties(): string {
return Object.entries(this.data)
.map(([key, value]) => `${key}:: ${value}`)
.map(([key, value]) =>
`${key}:: ${Array.isArray(value) ? value.map((v) => JSON.stringify(v)) : value}`
)
.join("\n");
}
/**
Expand Down
4 changes: 4 additions & 0 deletions src/core/__mocks__/obsidian.ts
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)
}

0 comments on commit e4eea18

Please sign in to comment.