|
| 1 | +import { handleShareLinkClick } from "@posthog/ui/utils/shareLinks"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +const navigateToChannel = vi.fn(); |
| 5 | +const navigateToChannelDashboard = vi.fn(); |
| 6 | +const navigateToChannelTask = vi.fn(); |
| 7 | + |
| 8 | +vi.mock("@posthog/ui/router/navigationBridge", () => ({ |
| 9 | + navigateToChannel: (...args: unknown[]) => navigateToChannel(...args), |
| 10 | + navigateToChannelDashboard: (...args: unknown[]) => |
| 11 | + navigateToChannelDashboard(...args), |
| 12 | + navigateToChannelTask: (...args: unknown[]) => navigateToChannelTask(...args), |
| 13 | +})); |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + vi.clearAllMocks(); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("handleShareLinkClick", () => { |
| 20 | + it("navigates in-app and cancels the default open for a share link", () => { |
| 21 | + const event = { preventDefault: vi.fn() }; |
| 22 | + |
| 23 | + const handled = handleShareLinkClick( |
| 24 | + "https://us.posthog.com/code/canvas/chan1/dash1", |
| 25 | + event, |
| 26 | + ); |
| 27 | + |
| 28 | + expect(handled).toBe(true); |
| 29 | + expect(event.preventDefault).toHaveBeenCalledOnce(); |
| 30 | + expect(navigateToChannelDashboard).toHaveBeenCalledWith("chan1", "dash1"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("routes a channel thread link to the task navigator", () => { |
| 34 | + const event = { preventDefault: vi.fn() }; |
| 35 | + |
| 36 | + handleShareLinkClick( |
| 37 | + "https://us.posthog.com/code/channel/chan1/tasks/task1", |
| 38 | + event, |
| 39 | + ); |
| 40 | + |
| 41 | + expect(navigateToChannelTask).toHaveBeenCalledWith("chan1", "task1"); |
| 42 | + }); |
| 43 | + |
| 44 | + it.each([ |
| 45 | + ["meta", { metaKey: true }], |
| 46 | + ["ctrl", { ctrlKey: true }], |
| 47 | + ["shift", { shiftKey: true }], |
| 48 | + ["a middle button", { button: 1 }], |
| 49 | + ])( |
| 50 | + "leaves a %s-modified click to open in a new tab/window", |
| 51 | + (_label, modifier) => { |
| 52 | + const event = { preventDefault: vi.fn(), ...modifier }; |
| 53 | + |
| 54 | + const handled = handleShareLinkClick( |
| 55 | + "https://us.posthog.com/code/canvas/chan1/dash1", |
| 56 | + event, |
| 57 | + ); |
| 58 | + |
| 59 | + expect(handled).toBe(false); |
| 60 | + expect(event.preventDefault).not.toHaveBeenCalled(); |
| 61 | + expect(navigateToChannelDashboard).not.toHaveBeenCalled(); |
| 62 | + }, |
| 63 | + ); |
| 64 | + |
| 65 | + it("leaves an external link alone", () => { |
| 66 | + const event = { preventDefault: vi.fn() }; |
| 67 | + |
| 68 | + const handled = handleShareLinkClick("https://example.com/docs", event); |
| 69 | + |
| 70 | + expect(handled).toBe(false); |
| 71 | + expect(event.preventDefault).not.toHaveBeenCalled(); |
| 72 | + expect(navigateToChannel).not.toHaveBeenCalled(); |
| 73 | + expect(navigateToChannelDashboard).not.toHaveBeenCalled(); |
| 74 | + expect(navigateToChannelTask).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("returns false for a missing href", () => { |
| 78 | + const event = { preventDefault: vi.fn() }; |
| 79 | + |
| 80 | + expect(handleShareLinkClick(undefined, event)).toBe(false); |
| 81 | + expect(event.preventDefault).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments