Skip to content

Commit 0c2afdc

Browse files
committed
Fixes tests, hope this is legit
1 parent 7568a61 commit 0c2afdc

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

apps/web-roo-code/src/lib/analytics/__tests__/consent-manager.test.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
1+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"
12
import { hasConsent, dispatchConsentEvent, onConsentChange, CONSENT_EVENT } from "../consent-manager"
23

34
describe("Consent Manager", () => {
4-
// Store the original document.cookie
5-
let originalCookie: PropertyDescriptor | undefined
5+
let cookieSpy: vi.SpyInstance
66

77
beforeEach(() => {
8-
// Clear cookies before each test
9-
originalCookie = Object.getOwnPropertyDescriptor(Document.prototype, "cookie")
10-
Object.defineProperty(document, "cookie", {
11-
writable: true,
12-
value: "",
13-
})
14-
// Clear any event listeners
8+
// Mock document.cookie to control its value in tests
9+
cookieSpy = vi.spyOn(document, "cookie", "get")
10+
11+
// Clear mock history before each test
1512
vi.clearAllMocks()
1613
})
1714

1815
afterEach(() => {
19-
// Restore original cookie property
20-
if (originalCookie) {
21-
Object.defineProperty(Document.prototype, "cookie", originalCookie)
22-
}
16+
// Restore the original document.cookie property
17+
vi.restoreAllMocks()
2318
})
2419

2520
describe("hasConsent", () => {
2621
it("should return false when no consent cookie exists", () => {
27-
document.cookie = ""
22+
cookieSpy.mockReturnValue("")
2823
expect(hasConsent()).toBe(false)
2924
})
3025

3126
it('should return false when consent cookie is not "true"', () => {
32-
document.cookie = "roo-code-cookie-consent=false"
27+
cookieSpy.mockReturnValue("roo-code-cookie-consent=false")
3328
expect(hasConsent()).toBe(false)
3429
})
3530

3631
it('should return true when consent cookie is "true"', () => {
37-
document.cookie = "roo-code-cookie-consent=true"
32+
cookieSpy.mockReturnValue("roo-code-cookie-consent=true")
3833
expect(hasConsent()).toBe(true)
3934
})
4035

4136
it("should handle multiple cookies correctly", () => {
42-
document.cookie = "other-cookie=value; roo-code-cookie-consent=true; another-cookie=value2"
37+
cookieSpy.mockReturnValue("other-cookie=value; roo-code-cookie-consent=true; another-cookie=value2")
4338
expect(hasConsent()).toBe(true)
4439
})
4540

4641
it("should handle cookies with spaces correctly", () => {
47-
document.cookie = "other-cookie=value; roo-code-cookie-consent=true ; another-cookie=value2"
42+
cookieSpy.mockReturnValue("other-cookie=value; roo-code-cookie-consent=true ; another-cookie=value2")
4843
expect(hasConsent()).toBe(true)
4944
})
5045
})

0 commit comments

Comments
 (0)