|
| 1 | +import { flushPromises, mount } from "@vue/test-utils"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import HashChip from "./HashChip.vue"; |
| 4 | + |
| 5 | +const { copy, clipboard } = vi.hoisted(() => ({ |
| 6 | + copy: vi.fn(), |
| 7 | + clipboard: { isSupported: true }, |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("vue-i18n", () => ({ |
| 11 | + useI18n: () => ({ |
| 12 | + t: (key: string, params?: Record<string, unknown>) => |
| 13 | + params ? `${key}:${JSON.stringify(params)}` : key, |
| 14 | + }), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("@/v2/composables/useClipboard", () => ({ |
| 18 | + useClipboard: () => ({ isSupported: clipboard.isSupported, copy }), |
| 19 | +})); |
| 20 | + |
| 21 | +const SHA1 = "0123456789abcdef0123456789abcdef01234567"; |
| 22 | +const ABBREVIATED = "012345…234567"; |
| 23 | + |
| 24 | +function mountChip( |
| 25 | + props: Partial<{ label: string; value: string | null }> = {}, |
| 26 | +) { |
| 27 | + return mount(HashChip, { props: { label: "SHA-1", value: SHA1, ...props } }); |
| 28 | +} |
| 29 | + |
| 30 | +async function click(wrapper: ReturnType<typeof mountChip>) { |
| 31 | + await wrapper.find("button").trigger("click"); |
| 32 | + await flushPromises(); |
| 33 | +} |
| 34 | + |
| 35 | +beforeEach(() => { |
| 36 | + copy.mockReset(); |
| 37 | + copy.mockResolvedValue(true); |
| 38 | + clipboard.isSupported = true; |
| 39 | + document.body.innerHTML = ""; |
| 40 | +}); |
| 41 | + |
| 42 | +describe("HashChip", () => { |
| 43 | + it("abbreviates a long value", () => { |
| 44 | + const wrapper = mountChip(); |
| 45 | + |
| 46 | + expect(wrapper.text()).toContain(ABBREVIATED); |
| 47 | + expect(wrapper.text()).not.toContain(SHA1); |
| 48 | + }); |
| 49 | + |
| 50 | + it("leaves a short value intact", () => { |
| 51 | + const wrapper = mountChip({ label: "CRC", value: "aabbccdd" }); |
| 52 | + |
| 53 | + expect(wrapper.text()).toContain("aabbccdd"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("keeps the full value in the title so hover still reads it", () => { |
| 57 | + const wrapper = mountChip(); |
| 58 | + |
| 59 | + expect(wrapper.find("button").attributes("title")).toContain(SHA1); |
| 60 | + }); |
| 61 | + |
| 62 | + it("copies the full value, not the abbreviation", async () => { |
| 63 | + const wrapper = mountChip(); |
| 64 | + |
| 65 | + await click(wrapper); |
| 66 | + |
| 67 | + expect(copy).toHaveBeenCalledWith(SHA1, expect.anything()); |
| 68 | + }); |
| 69 | + |
| 70 | + it("stays abbreviated when the copy succeeds", async () => { |
| 71 | + const wrapper = mountChip(); |
| 72 | + |
| 73 | + await click(wrapper); |
| 74 | + |
| 75 | + expect(wrapper.text()).toContain(ABBREVIATED); |
| 76 | + expect(wrapper.text()).not.toContain(SHA1); |
| 77 | + }); |
| 78 | + |
| 79 | + // The core of #4082: over plain HTTP the clipboard API does not exist, |
| 80 | + // so a copy can never succeed. Revealing the value is the only way the |
| 81 | + // user can get at it. |
| 82 | + it("reveals the full value instead of copying when the clipboard is unavailable", async () => { |
| 83 | + clipboard.isSupported = false; |
| 84 | + const wrapper = mountChip(); |
| 85 | + |
| 86 | + await click(wrapper); |
| 87 | + |
| 88 | + expect(copy).not.toHaveBeenCalled(); |
| 89 | + expect(wrapper.text()).toContain(SHA1); |
| 90 | + }); |
| 91 | + |
| 92 | + it("marks a revealed chip so its value can be selected", async () => { |
| 93 | + clipboard.isSupported = false; |
| 94 | + const wrapper = mountChip(); |
| 95 | + |
| 96 | + await click(wrapper); |
| 97 | + |
| 98 | + expect(wrapper.find("button").classes()).toContain( |
| 99 | + "r-v2-hash-chip--revealed", |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it("collapses the value again on a second click", async () => { |
| 104 | + clipboard.isSupported = false; |
| 105 | + const wrapper = mountChip(); |
| 106 | + |
| 107 | + await click(wrapper); |
| 108 | + await click(wrapper); |
| 109 | + |
| 110 | + expect(wrapper.text()).toContain(ABBREVIATED); |
| 111 | + expect(wrapper.text()).not.toContain(SHA1); |
| 112 | + }); |
| 113 | + |
| 114 | + it("still collapses when the page selection is outside the chip", async () => { |
| 115 | + clipboard.isSupported = false; |
| 116 | + const wrapper = mountChip(); |
| 117 | + const outside = document.createElement("p"); |
| 118 | + outside.textContent = "selected elsewhere"; |
| 119 | + document.body.appendChild(outside); |
| 120 | + |
| 121 | + await click(wrapper); |
| 122 | + window.getSelection()?.selectAllChildren(outside); |
| 123 | + await click(wrapper); |
| 124 | + |
| 125 | + expect(wrapper.text()).toContain(ABBREVIATED); |
| 126 | + expect(wrapper.text()).not.toContain(SHA1); |
| 127 | + }); |
| 128 | + |
| 129 | + it("reveals the full value when an attempted copy fails", async () => { |
| 130 | + copy.mockResolvedValue(false); |
| 131 | + const wrapper = mountChip(); |
| 132 | + |
| 133 | + await click(wrapper); |
| 134 | + |
| 135 | + expect(copy).toHaveBeenCalled(); |
| 136 | + expect(wrapper.text()).toContain(SHA1); |
| 137 | + }); |
| 138 | +}); |
0 commit comments