diff --git a/app/components/GoogleAnalytics.test.tsx b/app/components/GoogleAnalytics.test.tsx index 43deb40..1f8c8ee 100644 --- a/app/components/GoogleAnalytics.test.tsx +++ b/app/components/GoogleAnalytics.test.tsx @@ -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(); + + 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(); + }); }); diff --git a/app/components/GoogleAnalytics.tsx b/app/components/GoogleAnalytics.tsx index d424d6f..97c8f6a 100644 --- a/app/components/GoogleAnalytics.tsx +++ b/app/components/GoogleAnalytics.tsx @@ -98,6 +98,7 @@ const buttonClassName = function GoogleAnalyticsConsent() { const [consent, setConsent] = useState(); const [preferencesOpen, setPreferencesOpen] = useState(false); + const [revokeError, setRevokeError] = useState(null); const configuredRef = useRef(false); const preferencesButtonRef = useRef(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.

+ {revokeError && ( +

+ {revokeError} +

+ )}
{consent === "granted" ? (