useCompleteOnboarding.spec.js
1 month ago
useDateRangePicker.spec.js
2 months ago
useEmail.spec.js
2 months ago
useEngagementChartData.spec.js
2 months ago
useLicenseSettings.spec.js
2 months ago
useLink.spec.js
2 months ago
useMediaDetail.spec.js
2 months ago
useMediaLibrary.spec.js
2 months ago
useMediaList.spec.js
2 months ago
usePerformanceSettings.spec.js
2 months ago
useRegisterActivePage.spec.js
2 months ago
useSettingOption.spec.js
2 months ago
useSimpleSettingsPage.spec.js
2 months ago
useTopPerforming.spec.js
2 months ago
useTopVideosPaginated.spec.js
2 months ago
useUpgradeCTA.spec.js
2 months ago
useUserDetail.spec.js
2 months ago
useUpgradeCTA.spec.js
148 lines
| 1 | import { renderHook } from "@testing-library/react-hooks"; |
| 2 | import useUpgradeCTA from "../useUpgradeCTA"; |
| 3 | |
| 4 | const setPrestoPlayer = (overrides = {}) => { |
| 5 | global.window.prestoPlayer = { |
| 6 | isProPluginActive: false, |
| 7 | isPremium: false, |
| 8 | hasLicenseKey: false, |
| 9 | upgradeLink: "https://prestoplayer.com/pricing/", |
| 10 | renewLink: "https://my.prestomade.com/my-account/", |
| 11 | dashboardUrl: "admin.php?page=presto-dashboard", |
| 12 | ...overrides, |
| 13 | }; |
| 14 | }; |
| 15 | |
| 16 | describe("useUpgradeCTA", () => { |
| 17 | let originalLocation; |
| 18 | let originalOpen; |
| 19 | |
| 20 | beforeEach(() => { |
| 21 | originalLocation = window.location; |
| 22 | originalOpen = window.open; |
| 23 | // jsdom's window.location.href is read-only via assignment in some |
| 24 | // node versions; replace the whole object to make it observable. |
| 25 | delete window.location; |
| 26 | window.location = { href: "" }; |
| 27 | window.open = jest.fn(); |
| 28 | }); |
| 29 | |
| 30 | afterEach(() => { |
| 31 | window.location = originalLocation; |
| 32 | window.open = originalOpen; |
| 33 | delete global.window.prestoPlayer; |
| 34 | }); |
| 35 | |
| 36 | describe('"Upgrade Now" state (no Pro plugin)', () => { |
| 37 | it("returns the external upgrade label and href", () => { |
| 38 | setPrestoPlayer(); |
| 39 | const { result } = renderHook(() => useUpgradeCTA()); |
| 40 | expect(result.current.label).toBe("Upgrade Now"); |
| 41 | expect(result.current.href).toBe("https://prestoplayer.com/pricing/"); |
| 42 | expect(result.current.isProUnlicensed).toBe(false); |
| 43 | expect(result.current.isLicenseInvalid).toBe(false); |
| 44 | }); |
| 45 | |
| 46 | it("opens the pricing page in a new tab on click", () => { |
| 47 | setPrestoPlayer(); |
| 48 | const { result } = renderHook(() => useUpgradeCTA()); |
| 49 | result.current.onClick({ currentTarget: { tagName: "BUTTON" } }); |
| 50 | expect(window.open).toHaveBeenCalledWith( |
| 51 | "https://prestoplayer.com/pricing/", |
| 52 | "_blank", |
| 53 | "noopener noreferrer" |
| 54 | ); |
| 55 | }); |
| 56 | |
| 57 | it("does NOT call window.open when clicked element is an <a target=_blank>", () => { |
| 58 | setPrestoPlayer(); |
| 59 | const { result } = renderHook(() => useUpgradeCTA()); |
| 60 | result.current.onClick({ |
| 61 | currentTarget: { tagName: "A", target: "_blank" }, |
| 62 | }); |
| 63 | expect(window.open).not.toHaveBeenCalled(); |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | describe('"Activate License" state (Pro plugin active, no premium)', () => { |
| 68 | it("returns the in-app license tab label and href", () => { |
| 69 | setPrestoPlayer({ isProPluginActive: true }); |
| 70 | const { result } = renderHook(() => useUpgradeCTA()); |
| 71 | expect(result.current.label).toBe("Activate License"); |
| 72 | expect(result.current.href).toBe( |
| 73 | "admin.php?page=presto-dashboard&tab=Settings§ion=license" |
| 74 | ); |
| 75 | expect(result.current.isProUnlicensed).toBe(true); |
| 76 | }); |
| 77 | |
| 78 | it("uses history.push when a SPA history is provided", () => { |
| 79 | setPrestoPlayer({ isProPluginActive: true }); |
| 80 | const history = { push: jest.fn() }; |
| 81 | const { result } = renderHook(() => useUpgradeCTA(history)); |
| 82 | |
| 83 | const e = { preventDefault: jest.fn() }; |
| 84 | result.current.onClick(e); |
| 85 | |
| 86 | expect(e.preventDefault).toHaveBeenCalled(); |
| 87 | expect(history.push).toHaveBeenCalledWith({ |
| 88 | tab: "Settings", |
| 89 | section: "license", |
| 90 | }); |
| 91 | expect(window.location.href).toBe(""); |
| 92 | expect(window.open).not.toHaveBeenCalled(); |
| 93 | }); |
| 94 | |
| 95 | it("falls back to window.location when no history is provided", () => { |
| 96 | setPrestoPlayer({ isProPluginActive: true }); |
| 97 | const { result } = renderHook(() => useUpgradeCTA()); |
| 98 | |
| 99 | result.current.onClick({ preventDefault: jest.fn() }); |
| 100 | expect(window.location.href).toBe( |
| 101 | "admin.php?page=presto-dashboard&tab=Settings§ion=license" |
| 102 | ); |
| 103 | }); |
| 104 | |
| 105 | it("flags isLicenseInvalid only when a key exists but premium is still false", () => { |
| 106 | setPrestoPlayer({ isProPluginActive: true, hasLicenseKey: true }); |
| 107 | const { result } = renderHook(() => useUpgradeCTA()); |
| 108 | expect(result.current.isLicenseInvalid).toBe(true); |
| 109 | expect(result.current.isProUnlicensed).toBe(true); |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | describe("licensed state (premium)", () => { |
| 114 | it("still exposes flags so callers can hide the CTA", () => { |
| 115 | setPrestoPlayer({ isProPluginActive: true, isPremium: true }); |
| 116 | const { result } = renderHook(() => useUpgradeCTA()); |
| 117 | expect(result.current.isProUnlicensed).toBe(false); |
| 118 | expect(result.current.isLicenseInvalid).toBe(false); |
| 119 | expect(result.current.isPremium).toBe(true); |
| 120 | }); |
| 121 | }); |
| 122 | |
| 123 | describe("link defaults", () => { |
| 124 | it("falls back to the canonical pricing page when no upgrade URL is configured", () => { |
| 125 | // Empty config — upgradeLink/upgrade_link both missing. |
| 126 | global.window.prestoPlayer = {}; |
| 127 | const { result } = renderHook(() => useUpgradeCTA()); |
| 128 | expect(result.current.externalUpgradeLink).toBe( |
| 129 | "https://prestoplayer.com/pricing/" |
| 130 | ); |
| 131 | expect(result.current.renewLink).toBe( |
| 132 | "https://my.prestomade.com/my-account/" |
| 133 | ); |
| 134 | }); |
| 135 | |
| 136 | it("prefers `upgradeLink` over `upgrade_link` when both are set", () => { |
| 137 | setPrestoPlayer({ |
| 138 | upgradeLink: "https://example.com/a", |
| 139 | upgrade_link: "https://example.com/b", |
| 140 | }); |
| 141 | const { result } = renderHook(() => useUpgradeCTA()); |
| 142 | expect(result.current.externalUpgradeLink).toBe( |
| 143 | "https://example.com/a" |
| 144 | ); |
| 145 | }); |
| 146 | }); |
| 147 | }); |
| 148 |