Skip to content

Commit

Permalink
Merge pull request #144 from SmartGecko44/coverage-fix
Browse files Browse the repository at this point in the history
Fix coverage
  • Loading branch information
SmartGecko44 authored Nov 11, 2024
2 parents a1306b8 + 9f5963c commit ffd0916
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/.idea/sonarlint/
/.idea/jsLibraryMappings.xml
/.idea/sonarlint.xml
/gxcko.me/coverage/
34 changes: 33 additions & 1 deletion gxcko.me/src/components/CookiePopup.test.tsx
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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();
});
});
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ffd0916

Please sign in to comment.