From 6e143d00ce0c37215b505d0e6b767dd57c5ddb35 Mon Sep 17 00:00:00 2001 From: Nathan Smith Date: Wed, 22 Jan 2025 23:06:14 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8=20test:=20Add=20more=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __mocks__/obsidian.ts | 2 ++ __mocks__/obsidian/setIcon.ts | 5 +++ src/inline/inline.spec.ts | 45 ++++++++++++++++++++++++ test/element.ts | 66 +++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 __mocks__/obsidian/setIcon.ts create mode 100644 src/inline/inline.spec.ts create mode 100644 test/element.ts diff --git a/__mocks__/obsidian.ts b/__mocks__/obsidian.ts index 5a64ed2..71eb0aa 100644 --- a/__mocks__/obsidian.ts +++ b/__mocks__/obsidian.ts @@ -3,6 +3,7 @@ import { PluginMock } from "./obsidian/Plugin"; import { ModalMock } from "./obsidian/Modal"; import { PluginSettingTabMock } from "./obsidian/PluginSettingTab"; import { NoticeMock } from "./obsidian/Notice"; +import { setIconMock } from "./obsidian/setIcon"; module.exports = { App: AppMock, @@ -10,4 +11,5 @@ module.exports = { Modal: ModalMock, PluginSettingTab: PluginSettingTabMock, Notice: NoticeMock, + setIcon: setIconMock, }; diff --git a/__mocks__/obsidian/setIcon.ts b/__mocks__/obsidian/setIcon.ts new file mode 100644 index 0000000..5ad2844 --- /dev/null +++ b/__mocks__/obsidian/setIcon.ts @@ -0,0 +1,5 @@ +export function setIconMock(parent: HTMLElement, iconId: string): void { + const icon = document.createElement("span"); + icon.innerText = iconId; + parent.appendChild(icon); +} diff --git a/src/inline/inline.spec.ts b/src/inline/inline.spec.ts new file mode 100644 index 0000000..410cd53 --- /dev/null +++ b/src/inline/inline.spec.ts @@ -0,0 +1,45 @@ +import { test, describe, expect, beforeAll } from "@jest/globals"; +import { elementTestSetup } from "../../test/element"; +import { createTag } from "./inline"; + +const selectors = { + org: ".github-link-inline-org", + repo: ".github-link-inline-repo", + issueTitle: ".github-link-inline-issue-title", + prTitle: ".github-link-inline-pr-title", +}; + +describe("createTag", () => { + beforeAll(() => { + elementTestSetup(); + }); + test.each([ + { url: "https://github.com/nathonius", user: "nathonius" }, + { url: "https://github.com/nathonius/", user: "nathonius" }, + { url: "https://github.com/sam.lake", user: "sam.lake" }, + { url: "https://github.com/abraham_lincoln", user: "abraham_lincoln" }, + ])("user only $user", ({ url, user }) => { + const tag = createTag(url); + expect(tag).toBeTruthy(); + const org = tag?.querySelector(selectors.org) as HTMLSpanElement; + expect(org.innerText).toEqual(user); + [selectors.repo, selectors.issueTitle, selectors.prTitle].forEach((s) => { + expect(tag?.querySelector(s)).toBeFalsy(); + }); + }); + + test.each([ + { url: "https://github.com/nathonius/obsidian-github-link", user: "nathonius", repo: "obsidian-github-link" }, + { url: "https://github.com/nathonius/obsidian.github.link", user: "nathonius", repo: "obsidian.github.link" }, + ])("repo $repo", ({ url, user, repo }) => { + const tag = createTag(url); + expect(tag).toBeTruthy(); + const org = tag?.querySelector(selectors.org) as HTMLSpanElement; + const repoEl = tag?.querySelector(selectors.repo) as HTMLSpanElement; + expect(org.innerText).toEqual(user); + expect(repoEl.innerText).toEqual(repo); + [selectors.issueTitle, selectors.prTitle].forEach((s) => { + expect(tag?.querySelector(s)).toBeFalsy(); + }); + }); +}); diff --git a/test/element.ts b/test/element.ts new file mode 100644 index 0000000..2498bfd --- /dev/null +++ b/test/element.ts @@ -0,0 +1,66 @@ +/** + * Local version of obsidian's 'createEl' function, copied from + * https://ryotaushio.github.io/the-hobbyist-dev/obsidian/api-&-internals/createel.html + */ +function createEl( + tag: K, + options?: string | DomElementInfo, + callback?: (el: HTMLElementTagNameMap[K]) => void, +): HTMLElementTagNameMap[K] { + const el = document.createElement(tag); + if (typeof options === "string") options = { cls: options }; + options = options || {}; + + if (options.cls) Array.isArray(options.cls) ? (el.className = options.cls.join(" ")) : (el.className = options.cls); + if (options.text) el.setText(options.text); + if (options.attr) { + for (const [k, v] of Object.entries(options.attr)) { + el.setAttribute(k, String(v)); + } + } + if (options.title !== undefined) el.title = options.title; + if ( + options.value !== undefined && + (el instanceof HTMLInputElement || el instanceof HTMLSelectElement || el instanceof HTMLOptionElement) + ) { + el.value = options.value; + } + if (options.type && el instanceof HTMLInputElement) el.type = options.type; + if (options.type && el instanceof HTMLStyleElement) el.setAttribute("type", options.type); + if (options.placeholder && el instanceof HTMLInputElement) el.placeholder = options.placeholder; + if ( + options.href && + (el instanceof HTMLAnchorElement || el instanceof HTMLLinkElement || el instanceof HTMLBaseElement) + ) + el.href = options.href; + callback?.(el); + if (options.parent) { + if (options.prepend) options.parent.insertBefore(el, options.parent.firstChild); + else options.parent.appendChild(el); + } + // add event listeners (e.g. { onclick: () => {} }) + for (const key in options) { + if (Object.prototype.hasOwnProperty.call(options, key) && key.startsWith("on")) { + const value = options[key as keyof DomElementInfo]; + if (typeof value === "function") el.addEventListener(key.substring(2), value); + } + } + return el; +} + +function createSpan(options?: string | DomElementInfo, callback?: (el: HTMLSpanElement) => void) { + return createEl("span", options, callback); +} + +function createDiv(options?: string | DomElementInfo, callback?: (el: HTMLDivElement) => void) { + return createEl("div", options, callback); +} + +export function elementTestSetup() { + HTMLElement.prototype.setText = function (text: string) { + this.innerText = text; + }; + window.createEl = createEl; + window.createSpan = createSpan; + window.createDiv = createDiv; +}