|
| 1 | +import { describe, expect, it, beforeEach } from "vitest"; |
| 2 | +import { FormatSyntaxSuggester } from "./formatSyntaxSuggester"; |
| 3 | + |
| 4 | +function ensureObsidianDomPolyfills(): void { |
| 5 | + (globalThis as any).createDiv ??= (cls?: string) => { |
| 6 | + const div = document.createElement("div"); |
| 7 | + if (cls) div.className = cls; |
| 8 | + return div; |
| 9 | + }; |
| 10 | + |
| 11 | + const proto = HTMLElement.prototype as any; |
| 12 | + |
| 13 | + proto.createDiv ??= function (arg?: string | { cls?: string }) { |
| 14 | + const div = document.createElement("div"); |
| 15 | + if (typeof arg === "string") div.className = arg; |
| 16 | + else if (arg && typeof arg === "object" && typeof arg.cls === "string") |
| 17 | + div.className = arg.cls; |
| 18 | + this.appendChild(div); |
| 19 | + return div; |
| 20 | + }; |
| 21 | + |
| 22 | + proto.empty ??= function () { |
| 23 | + this.replaceChildren(); |
| 24 | + return this; |
| 25 | + }; |
| 26 | + |
| 27 | + // Obsidian adds delegated event helpers; tests don't need behavior here. |
| 28 | + proto.on ??= function () { |
| 29 | + return this; |
| 30 | + }; |
| 31 | + |
| 32 | + proto.detach ??= function () { |
| 33 | + this.remove(); |
| 34 | + }; |
| 35 | + |
| 36 | + proto.addClass ??= function (...classes: string[]) { |
| 37 | + this.classList.add(...classes); |
| 38 | + return this; |
| 39 | + }; |
| 40 | + |
| 41 | + proto.removeClass ??= function (...classes: string[]) { |
| 42 | + this.classList.remove(...classes); |
| 43 | + return this; |
| 44 | + }; |
| 45 | + |
| 46 | + proto.setAttr ??= function (name: string, value: string) { |
| 47 | + this.setAttribute(name, value); |
| 48 | + return this; |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +describe("FormatSyntaxSuggester case style suggestions", () => { |
| 53 | + beforeEach(() => { |
| 54 | + ensureObsidianDomPolyfills(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("suggests kebab when typing a |case: prefix", async () => { |
| 58 | + const app = { |
| 59 | + dom: { appContainerEl: document.body }, |
| 60 | + keymap: { pushScope: () => {}, popScope: () => {} }, |
| 61 | + } as any; |
| 62 | + const plugin = { |
| 63 | + settings: { choices: [], globalVariables: {} }, |
| 64 | + getTemplateFiles: () => [], |
| 65 | + } as any; |
| 66 | + |
| 67 | + const inputEl = document.createElement("input"); |
| 68 | + inputEl.value = "{{VALUE:title|case:k"; |
| 69 | + inputEl.selectionStart = inputEl.value.length; |
| 70 | + inputEl.selectionEnd = inputEl.value.length; |
| 71 | + |
| 72 | + const suggester = new FormatSyntaxSuggester(app, inputEl, plugin); |
| 73 | + const suggestions = await suggester.getSuggestions(inputEl.value); |
| 74 | + expect(suggestions).toEqual(["kebab"]); |
| 75 | + suggester.destroy(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("suggests all styles (including slug) when case fragment is empty", async () => { |
| 79 | + const app = { |
| 80 | + dom: { appContainerEl: document.body }, |
| 81 | + keymap: { pushScope: () => {}, popScope: () => {} }, |
| 82 | + } as any; |
| 83 | + const plugin = { |
| 84 | + settings: { choices: [], globalVariables: {} }, |
| 85 | + getTemplateFiles: () => [], |
| 86 | + } as any; |
| 87 | + |
| 88 | + const inputEl = document.createElement("input"); |
| 89 | + inputEl.value = "{{VALUE:title|case:"; |
| 90 | + inputEl.selectionStart = inputEl.value.length; |
| 91 | + inputEl.selectionEnd = inputEl.value.length; |
| 92 | + |
| 93 | + const suggester = new FormatSyntaxSuggester(app, inputEl, plugin); |
| 94 | + const suggestions = await suggester.getSuggestions(inputEl.value); |
| 95 | + expect(suggestions).toContain("slug"); |
| 96 | + expect(suggestions).toContain("kebab"); |
| 97 | + expect(suggestions).toContain("snake"); |
| 98 | + expect(suggestions).toContain("camel"); |
| 99 | + expect(suggestions).toContain("pascal"); |
| 100 | + expect(suggestions).toContain("title"); |
| 101 | + expect(suggestions).toContain("lower"); |
| 102 | + expect(suggestions).toContain("upper"); |
| 103 | + suggester.destroy(); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
0 commit comments