Skip to content

Commit

Permalink
tests: add tests for creating new note from template that updates fro…
Browse files Browse the repository at this point in the history
…ntmatter after creation and tests for properties showing in live preview on insert of template with frontmatter

refs: #1253, #1309
  • Loading branch information
Zachatoo committed Feb 10, 2024
1 parent 630bbeb commit 0142f79
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/InternalTemplates/InternalModuleHooks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from "chai";
import TestTemplaterPlugin from "../../main.test";
import { properties_are_visible } from "../utils.test";

export function InternalModuleHooksTests(t: TestTemplaterPlugin) {
t.test(
"tp.hooks.on_all_templates_executed shows properties in live preview",
async () => {
const template = `<%*
tp.hooks.on_all_templates_executed(async () => {
const file = tp.file.find_tfile(tp.file.path(true));
await app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter["key"] = "value";
});
});
%>
TEXT THAT SHOULD STAY`;
await t.run_in_new_leaf(template, "", true);
expect(properties_are_visible()).to.be.true;
await expect(
t.run_and_get_output(template, "", true)
).to.eventually.equal("\nTEXT THAT SHOULD STAY");
await expect(
t.create_new_note_from_template_and_get_output(template)
).to.eventually.equal(
"---\nkey: value\n---\n\nTEXT THAT SHOULD STAY"
);
}
);
}
13 changes: 13 additions & 0 deletions tests/Templater.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from "chai";
import TestTemplaterPlugin from "../main.test";
import { properties_are_visible } from "./utils.test";

export function TemplaterTests(t: TestTemplaterPlugin) {
t.test(
"append_template_to_active_file shows properties in live preview",
async () => {
await t.run_in_new_leaf("---\nkey: value\n---\nText");
expect(properties_are_visible()).to.be.true;
}
);
}
42 changes: 42 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
import { InternalModuleFileTests } from "./InternalTemplates/InternalModuleFile.test";
import { InternalModuleDateTests } from "./InternalTemplates/InternalModuleDate.test";
import { InternalModuleFrontmatterTests } from "./InternalTemplates/InternalModuleFrontmatter.test";
import { InternalModuleHooksTests } from "./InternalTemplates/InternalModuleHooks.test";
import { InternalModuleSystemTests } from "./InternalTemplates/InternalModuleSystem.test";
import { InternalModuleConfigTests } from "./InternalTemplates/InternalModuleConfig.test";
import { TemplaterTests } from "./Templater.test";

chai.use(chaiAsPromised);

Expand Down Expand Up @@ -106,8 +108,10 @@ export default class TestTemplaterPlugin extends Plugin {
InternalModuleFileTests(this);
InternalModuleDateTests(this);
InternalModuleFrontmatterTests(this);
InternalModuleHooksTests(this);
InternalModuleSystemTests(this);
InternalModuleConfigTests(this);
TemplaterTests(this);
}

test(name: string, fn: () => Promise<void>) {
Expand Down Expand Up @@ -196,4 +200,42 @@ export default class TestTemplaterPlugin extends Plugin {
);
return content;
}

async create_new_note_from_template_and_get_output(
template_content: string,
delay_ms = 300
): Promise<string | undefined> {
const file = await this.plugin.templater.create_new_note_from_template(
template_content
);
if (file) {
this.active_files.push(file);
await delay(delay_ms);
const content = await this.app.vault.read(file);
return content;
}
}

async run_in_new_leaf(
template_content: string,
target_content = "",
waitCache = false,
skip_modify = false
): Promise<void> {
await this.app.vault.modify(this.template_file, template_content);
if (!skip_modify) {
await this.app.vault.modify(this.target_file, target_content);
}
if (waitCache) {
await cache_update(this);
}

await this.app.workspace.getLeaf(true).openFile(this.target_file);

await this.plugin.templater.append_template_to_active_file(
this.template_file
);

await delay(300);
}
}
6 changes: 6 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ export function cache_update(t: TestTemplaterPlugin): Promise<void> {
t.app.metadataCache.on("changed", resolve_promise);
});
}

export function properties_are_visible(): boolean {
return !!document.querySelector(
".workspace-leaf.mod-active .metadata-properties .metadata-property"
);
}

0 comments on commit 0142f79

Please sign in to comment.