fix: restore portfolio loader and safe consent revocation

This commit is contained in:
Evandro Fonseca Junior committed 2026-09-04 21:40:35 -03:00
1 parent bff3e4d735
commit a4e5e5c048
4 files changed
+315 -13

No files matched your search

+27
View File
@@ -223,4 +223,31 @@ describe("GoogleAnalytics consent", () => {
).not.toBeInTheDocument();
});
});
it("reports when revocation cannot be persisted", async () => {
const user = userEvent.setup();
localStorage.setItem(CONSENT_KEY, "granted");
jest.spyOn(Storage.prototype, "setItem").mockImplementation(() => {
throw new Error("storage blocked");
});
render(<GoogleAnalytics />);
await screen.findByTestId("google-analytics-script");
await user.click(
screen.getByRole("button", { name: "Preferências de analytics" }),
);
await user.click(screen.getByRole("button", { name: "Revogar analytics" }));
expect(
screen.getByRole("alert", {
name: "Falha ao salvar a revogação",
}),
).toHaveTextContent(
"Não foi possível salvar a revogação. Tente novamente para concluir.",
);
expect(analyticsWindow[GA_DISABLE_KEY]).not.toBe(true);
expect(
screen.getByRole("button", { name: "Revogar analytics" }),
).toBeVisible();
});
});
+17 -1
View File
@@ -98,6 +98,7 @@ const buttonClassName =
function GoogleAnalyticsConsent() {
const [consent, setConsent] = useState<AnalyticsConsent | null>();
const [preferencesOpen, setPreferencesOpen] = useState(false);
const [revokeError, setRevokeError] = useState<string | null>(null);
const configuredRef = useRef(false);
const preferencesButtonRef = useRef<HTMLButtonElement>(null);
@@ -150,7 +151,13 @@ function GoogleAnalyticsConsent() {
}
function revokeAnalytics() {
persistConsent("denied");
if (!persistConsent("denied")) {
setRevokeError(
"Não foi possível salvar a revogação. Tente novamente para concluir.",
);
return;
}
setRevokeError(null);
window[GA_DISABLE_KEY] = true;
window.gtag?.("consent", "update", deniedConsent);
expireAnalyticsCookies();
@@ -250,6 +257,15 @@ function GoogleAnalyticsConsent() {
Analytics está {consent === "granted" ? "ativo" : "desativado"}.
Dados de publicidade permanecem negados.
</p>
{revokeError && (
<p
role="alert"
aria-label="Falha ao salvar a revogação"
className="mt-sp-3 text-[13px] leading-6 text-[var(--slash)]"
>
{revokeError}
</p>
)}
<div className="mt-sp-4 flex flex-wrap gap-sp-2">
{consent === "granted" ? (
<button