From 4bdcfbdb2ef9b8fd0ca31acdb21e44a832ccbf64 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 26 Feb 2026 17:39:07 +0000 Subject: [PATCH] Refactor: Replace duplicated extractDTagValue in DeleteModal Replaced the local `extractDTagValue` function in `js/ui/components/DeleteModal.js` with the shared `getDTagValueFromTags` utility from `js/nostr/nip71.js` (exported via `js/nostr/index.js`). Added a new unit test `tests/ui/components/DeleteModal.test.mjs` to verify the d-tag extraction logic and ensure the refactor is correct and safe. This test uses `jsdom` to mock the necessary DOM environment. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- js/ui/components/DeleteModal.js | 18 +----- tests/ui/components/DeleteModal.test.mjs | 70 ++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 tests/ui/components/DeleteModal.test.mjs diff --git a/js/ui/components/DeleteModal.js b/js/ui/components/DeleteModal.js index 80ade25f..7c01ab01 100644 --- a/js/ui/components/DeleteModal.js +++ b/js/ui/components/DeleteModal.js @@ -1,20 +1,6 @@ +import { getDTagValueFromTags } from "../../nostr/index.js"; import { createModalAccessibility } from "./modalAccessibility.js"; -function extractDTagValue(tags) { - if (!Array.isArray(tags)) { - return ""; - } - for (const entry of tags) { - if (!Array.isArray(entry) || entry.length < 2) { - continue; - } - if (entry[0] === "d" && typeof entry[1] === "string") { - return entry[1]; - } - } - return ""; -} - export class DeleteModal { constructor({ removeTrackingScripts, @@ -232,7 +218,7 @@ export class DeleteModal { video && typeof video.videoRootId === "string" && video.videoRootId.trim().length ? video.videoRootId.trim() : ""; - const dTagValue = extractDTagValue(video?.tags); + const dTagValue = getDTagValueFromTags(video?.tags); if (this.subtitle) { const subtitleParts = []; diff --git a/tests/ui/components/DeleteModal.test.mjs b/tests/ui/components/DeleteModal.test.mjs new file mode 100644 index 00000000..9a089ee1 --- /dev/null +++ b/tests/ui/components/DeleteModal.test.mjs @@ -0,0 +1,70 @@ +import { test, describe, it, beforeEach, afterEach } from "node:test"; +import assert from "node:assert"; +import { JSDOM } from "jsdom"; + +describe("DeleteModal", () => { + let DeleteModal; + let modal; + let mockModalElement; + let subtitle; + let metadataList; + + beforeEach(async () => { + const dom = new JSDOM("", { + url: "http://localhost/", + }); + globalThis.document = dom.window.document; + globalThis.window = dom.window; + globalThis.HTMLElement = dom.window.HTMLElement; + globalThis.EventTarget = dom.window.EventTarget; + globalThis.Node = dom.window.Node; + globalThis.DOMParser = dom.window.DOMParser; + globalThis.Event = dom.window.Event; + globalThis.CustomEvent = dom.window.CustomEvent; + + // Dynamic import to ensure globals are set before module execution + const module = await import("../../../js/ui/components/DeleteModal.js"); + DeleteModal = module.DeleteModal; + + // Mock DOM elements + mockModalElement = document.createElement('div'); + subtitle = document.createElement('div'); + subtitle.id = "deleteModalSubtitle"; + metadataList = document.createElement('dl'); + metadataList.id = "deleteModalMetadata"; + + mockModalElement.appendChild(subtitle); + mockModalElement.appendChild(metadataList); + + modal = new DeleteModal(); + modal.cacheElements(mockModalElement); + }); + + it("should extract d-tag and display it in subtitle", () => { + const video = { + tags: [['d', 'my-d-tag']] + }; + modal.setVideo(video); + + assert.ok(subtitle.textContent.includes("d=my-d-tag"), `Subtitle should contain d-tag, got: ${subtitle.textContent}`); + }); + + it("should extract d-tag and display it in metadata", () => { + const video = { + tags: [['d', 'my-d-tag']] + }; + modal.setVideo(video); + + const metadataText = metadataList.textContent; + assert.ok(metadataText.includes("my-d-tag"), `Metadata should contain d-tag, got: ${metadataText}`); + }); + + it("should handle missing d-tag", () => { + const video = { + tags: [['t', 'hashtag']] + }; + modal.setVideo(video); + + assert.ok(!subtitle.textContent.includes("d="), `Subtitle should not contain d-tag, got: ${subtitle.textContent}`); + }); +});