diff --git a/app/components/GoogleAnalytics.test.tsx b/app/components/GoogleAnalytics.test.tsx index 1f8c8ee..f300657 100644 --- a/app/components/GoogleAnalytics.test.tsx +++ b/app/components/GoogleAnalytics.test.tsx @@ -240,7 +240,7 @@ describe("GoogleAnalytics consent", () => { expect( screen.getByRole("alert", { - name: "Falha ao salvar a revogação", + name: "Falha ao salvar o consentimento", }), ).toHaveTextContent( "Não foi possível salvar a revogação. Tente novamente para concluir.", @@ -250,4 +250,30 @@ describe("GoogleAnalytics consent", () => { screen.getByRole("button", { name: "Revogar analytics" }), ).toBeVisible(); }); + + it("reports when refusal cannot be persisted", async () => { + const user = userEvent.setup(); + jest.spyOn(Storage.prototype, "setItem").mockImplementation(() => { + throw new Error("storage blocked"); + }); + render(); + + await user.click( + await screen.findByRole("button", { name: "Recusar analytics" }), + ); + + expect( + screen.getByRole("alert", { + name: "Falha ao salvar o consentimento", + }), + ).toHaveTextContent( + "Não foi possível salvar sua recusa. Tente novamente para concluir.", + ); + expect( + screen.getByRole("button", { name: "Recusar analytics" }), + ).toBeVisible(); + expect( + screen.queryByRole("button", { name: "Preferências de analytics" }), + ).not.toBeInTheDocument(); + }); }); diff --git a/app/components/GoogleAnalytics.tsx b/app/components/GoogleAnalytics.tsx index 97c8f6a..d3a2739 100644 --- a/app/components/GoogleAnalytics.tsx +++ b/app/components/GoogleAnalytics.tsx @@ -98,7 +98,7 @@ const buttonClassName = function GoogleAnalyticsConsent() { const [consent, setConsent] = useState(); const [preferencesOpen, setPreferencesOpen] = useState(false); - const [revokeError, setRevokeError] = useState(null); + const [consentError, setConsentError] = useState(null); const configuredRef = useRef(false); const preferencesButtonRef = useRef(null); @@ -138,26 +138,38 @@ function GoogleAnalyticsConsent() { } function acceptAnalytics() { - if (!persistConsent("granted")) return; + if (!persistConsent("granted")) { + setConsentError( + "Não foi possível salvar sua aceitação. Tente novamente para concluir.", + ); + return; + } + setConsentError(null); enableAnalytics(); setConsent("granted"); closePreferences(); } function denyAnalytics() { - persistConsent("denied"); + if (!persistConsent("denied")) { + setConsentError( + "Não foi possível salvar sua recusa. Tente novamente para concluir.", + ); + return; + } + setConsentError(null); setConsent("denied"); closePreferences(); } function revokeAnalytics() { if (!persistConsent("denied")) { - setRevokeError( + setConsentError( "Não foi possível salvar a revogação. Tente novamente para concluir.", ); return; } - setRevokeError(null); + setConsentError(null); window[GA_DISABLE_KEY] = true; window.gtag?.("consent", "update", deniedConsent); expireAnalyticsCookies(); @@ -189,6 +201,15 @@ function GoogleAnalyticsConsent() { Você decide se permite métricas anônimas para melhorar este site. Publicidade e personalização continuam desativadas.

+ {consentError && ( +

+ {consentError} +

+ )}