|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { act } from "react" |
| 4 | +import { createRoot, type Root } from "react-dom/client" |
| 5 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest" |
| 6 | +import { CapiDatasetCard } from "@/features/meta-conversions/components/capi-dataset-card" |
| 7 | + |
| 8 | +vi.mock("next-intl", () => ({ |
| 9 | + useTranslations: () => (key: string) => key, |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock("next/navigation", () => ({ |
| 13 | + useRouter: () => ({ refresh: vi.fn() }), |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock("sonner", () => ({ |
| 17 | + toast: { error: vi.fn(), success: vi.fn() }, |
| 18 | +})) |
| 19 | + |
| 20 | +// The card calls `useAction` twice, in order: setDataset then provision. Each |
| 21 | +// call gets its own `execute` spy so a test can assert which action ran. |
| 22 | +const executes: ReturnType<typeof vi.fn>[] = [] |
| 23 | +vi.mock("next-safe-action/hooks", () => ({ |
| 24 | + useAction: () => { |
| 25 | + const execute = vi.fn() |
| 26 | + executes.push(execute) |
| 27 | + return { execute, isPending: false } |
| 28 | + }, |
| 29 | +})) |
| 30 | + |
| 31 | +const SAVE_LABEL = "metaConversions.finalize.save" |
| 32 | +const CREATE_LABEL = "metaConversions.finalize.createDataset" |
| 33 | + |
| 34 | +describe("CapiDatasetCard", () => { |
| 35 | + let container: HTMLDivElement |
| 36 | + let root: Root |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }) |
| 40 | + container = document.createElement("div") |
| 41 | + document.body.append(container) |
| 42 | + root = createRoot(container) |
| 43 | + executes.length = 0 |
| 44 | + }) |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + act(() => { |
| 48 | + root.unmount() |
| 49 | + }) |
| 50 | + container.remove() |
| 51 | + }) |
| 52 | + |
| 53 | + function render() { |
| 54 | + const setDataset = vi.fn() |
| 55 | + const provision = vi.fn() |
| 56 | + act(() => { |
| 57 | + root.render( |
| 58 | + <CapiDatasetCard |
| 59 | + actions={ |
| 60 | + { setDataset, provision } as unknown as Parameters< |
| 61 | + typeof CapiDatasetCard |
| 62 | + >[0]["actions"] |
| 63 | + } |
| 64 | + integrationId="int-1" |
| 65 | + workspaceId="ws-1" |
| 66 | + />, |
| 67 | + ) |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + // Each render pushes a fresh [setDataset, provision] execute pair; the button |
| 72 | + // click runs the latest render's pair. |
| 73 | + function latestExecutes() { |
| 74 | + return { |
| 75 | + setDatasetExecute: executes.at(-2), |
| 76 | + provisionExecute: executes.at(-1), |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + function button(): HTMLButtonElement { |
| 81 | + const found = container.querySelector("button") |
| 82 | + if (!found) { |
| 83 | + throw new Error("button not rendered") |
| 84 | + } |
| 85 | + return found |
| 86 | + } |
| 87 | + |
| 88 | + function typeDatasetId(value: string) { |
| 89 | + const input = container.querySelector("input") |
| 90 | + if (!input) { |
| 91 | + throw new Error("input not rendered") |
| 92 | + } |
| 93 | + const setter = Object.getOwnPropertyDescriptor( |
| 94 | + window.HTMLInputElement.prototype, |
| 95 | + "value", |
| 96 | + )?.set |
| 97 | + act(() => { |
| 98 | + setter?.call(input, value) |
| 99 | + input.dispatchEvent(new Event("input", { bubbles: true })) |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + test("shows Create Dataset and auto-provisions when the field is empty", () => { |
| 104 | + render() |
| 105 | + |
| 106 | + expect(button().textContent).toBe(CREATE_LABEL) |
| 107 | + |
| 108 | + act(() => { |
| 109 | + button().click() |
| 110 | + }) |
| 111 | + |
| 112 | + const { setDatasetExecute, provisionExecute } = latestExecutes() |
| 113 | + expect(provisionExecute).toHaveBeenCalledWith() |
| 114 | + expect(setDatasetExecute).not.toHaveBeenCalled() |
| 115 | + }) |
| 116 | + |
| 117 | + test("shows Save and validates the pasted id when the field is filled", () => { |
| 118 | + render() |
| 119 | + |
| 120 | + typeDatasetId(" 913591454696998 ") |
| 121 | + expect(button().textContent).toBe(SAVE_LABEL) |
| 122 | + |
| 123 | + act(() => { |
| 124 | + button().click() |
| 125 | + }) |
| 126 | + |
| 127 | + const { setDatasetExecute, provisionExecute } = latestExecutes() |
| 128 | + expect(setDatasetExecute).toHaveBeenCalledWith({ |
| 129 | + datasetId: "913591454696998", |
| 130 | + }) |
| 131 | + expect(provisionExecute).not.toHaveBeenCalled() |
| 132 | + }) |
| 133 | + |
| 134 | + test("rejects a non-numeric Dataset ID with an inline error and a disabled Save", () => { |
| 135 | + render() |
| 136 | + |
| 137 | + typeDatasetId("adasdad") |
| 138 | + |
| 139 | + // Still the Save action (field is non-empty), but blocked. |
| 140 | + expect(button().textContent).toBe(SAVE_LABEL) |
| 141 | + expect(button().disabled).toBe(true) |
| 142 | + expect(container.textContent).toContain( |
| 143 | + "metaConversions.errors.invalidDatasetId", |
| 144 | + ) |
| 145 | + |
| 146 | + act(() => { |
| 147 | + button().click() |
| 148 | + }) |
| 149 | + |
| 150 | + const { setDatasetExecute, provisionExecute } = latestExecutes() |
| 151 | + expect(setDatasetExecute).not.toHaveBeenCalled() |
| 152 | + expect(provisionExecute).not.toHaveBeenCalled() |
| 153 | + }) |
| 154 | + |
| 155 | + test("reverts to Create Dataset when the field is cleared", () => { |
| 156 | + render() |
| 157 | + |
| 158 | + typeDatasetId("913591454696998") |
| 159 | + expect(button().textContent).toBe(SAVE_LABEL) |
| 160 | + |
| 161 | + typeDatasetId("") |
| 162 | + expect(button().textContent).toBe(CREATE_LABEL) |
| 163 | + }) |
| 164 | +}) |
0 commit comments