diff --git a/.gitignore b/.gitignore index f043532..35a1c19 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /.idea/sonarlint/ /.idea/jsLibraryMappings.xml /.idea/sonarlint.xml +/gxcko.me/coverage/ diff --git a/gxcko.me/src/components/CookiePopup.test.tsx b/gxcko.me/src/components/CookiePopup.test.tsx index 09ca47e..2bc7890 100644 --- a/gxcko.me/src/components/CookiePopup.test.tsx +++ b/gxcko.me/src/components/CookiePopup.test.tsx @@ -1,8 +1,14 @@ import {describe, expect} from "vitest"; import {fireEvent, screen} from "@testing-library/react"; -import CookiePopup from "./CookiePopup.tsx"; +import CookiePopup, {getCookie} from "./CookiePopup.tsx"; import {renderWithProviders} from "../../testHelpers/functions/renderWithProviders.tsx"; import {clearCookies} from "../../testHelpers/functions/clearCookies.tsx"; +import * as Sentry from "@sentry/browser"; + +// Mock the entire Sentry module +vi.mock('@sentry/browser', () => ({ + captureException: vi.fn(), +})); describe('CookiePopup Component Functionality', () => { beforeEach(clearCookies) @@ -126,4 +132,30 @@ describe('CookiePopup Development Artefacts', () => { expect(consoleLogSpy).not.toHaveBeenCalled(); }); +}); + +describe('CookiePopup Error Handling', () => { + beforeEach(() => { + // Mock document.cookie to throw a SecurityError + Object.defineProperty(document, 'cookie', { + get: () => { throw new DOMException('Failed to read the \'cookie\' property from \'Document\': Cookies are disabled inside \'data:\' URLs.', 'SecurityError'); } + }); + }); + + afterEach(() => { + // Restore document.cookie and any other mocks after each test + vi.restoreAllMocks(); + }); + + it('should throw a Sentry exception when cookie storage cannot be accessed', () => { + const result = getCookie('cookiesAccepted'); + + // Assert that Sentry.captureException was called with the right error and options + expect(Sentry.captureException).toHaveBeenCalledWith(expect.any(DOMException), { + level: 'error', + tags: { handled: true } + }); + + expect(result).toBeNull(); + }); }); \ No newline at end of file diff --git a/sonar-project.properties b/sonar-project.properties index 8a1f4b9..ffa2db3 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -2,7 +2,7 @@ sonar.projectKey=SmartGecko44_SmartGecko44.github.io sonar.organization=smartgecko44 sonar.javascript.lcov.reportPaths=./gxcko.me/coverage/lcov.info -sonar.exclusions=./gxcko.me/coverage/**, **/*.test.* +sonar.exclusions=**/*.test.* # This is the name and version displayed in the SonarCloud UI. #sonar.projectName=SmartGecko44.github.io