Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions packages/core/src/browser/cookieAccess.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Clock } from '../../test'
import { collectAsyncCalls, mockClock, registerCleanupTask, replaceMockable } from '../../test'
import type { Configuration } from '../domain/configuration'
import { detectVersion, isChromium } from '../tools/utils/browserDetection'
import { dateNow } from '../tools/utils/timeUtils'
import { dateNow, ONE_MINUTE } from '../tools/utils/timeUtils'
import type { CookieOptions } from './cookie'
import { deleteCookie, getCookie, setCookie } from './cookie'
import type { CookieStoreWindow } from './browser.types'
Expand Down Expand Up @@ -33,6 +33,7 @@ describe('cookieAccess', () => {
{
title: 'document.cookie fallback',
setup: () => {
registerCleanupTask(() => deleteCookie(COOKIE_NAME))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supposedly we already delete all cookies in the global forEach

If this is not working properly we should probably fix it there, no?

disableCookieStore()
const clock = mockClock()

Expand All @@ -59,6 +60,7 @@ describe('cookieAccess', () => {
{
title: 'CookieStore API',
setup: () => {
registerCleanupTask(() => deleteCookie(COOKIE_NAME))
const clock = mockClock()

if (!(window as CookieStoreWindow).cookieStore) {
Expand Down Expand Up @@ -108,14 +110,14 @@ describe('cookieAccess', () => {
describe('getAllAndSet', () => {
it('should pass current cookie values to callback', async () => {
const { setCookieWithCleanup } = setup()
await setCookieWithCleanup(COOKIE_NAME, 'value1', 1000)
await setCookieWithCleanup(COOKIE_NAME, 'value1', ONE_MINUTE)

const cookieAccess = createCookieAccess(COOKIE_NAME, MOCK_CONFIGURATION, COOKIE_OPTIONS)

let capturedValues: string[] | undefined
await cookieAccess.getAllAndSet((values) => {
capturedValues = values
return { value: 'new', expireDelay: 1000 }
return { value: 'new', expireDelay: ONE_MINUTE }
})

expect(capturedValues).toEqual(['value1'])
Expand All @@ -128,7 +130,7 @@ describe('cookieAccess', () => {
let capturedValues: string[] | undefined
await cookieAccess.getAllAndSet((values) => {
capturedValues = values
return { value: 'new', expireDelay: 1000 }
return { value: 'new', expireDelay: ONE_MINUTE }
})

expect(capturedValues).toEqual([])
Expand All @@ -138,7 +140,7 @@ describe('cookieAccess', () => {
setup()
const cookieAccess = createCookieAccess(COOKIE_NAME, MOCK_CONFIGURATION, COOKIE_OPTIONS)

await cookieAccess.getAllAndSet(() => ({ value: 'hello', expireDelay: 1000 }))
await cookieAccess.getAllAndSet(() => ({ value: 'hello', expireDelay: ONE_MINUTE }))

expect(getCookie(COOKIE_NAME)).toBe('hello')
})
Expand All @@ -150,15 +152,15 @@ describe('cookieAccess', () => {
}

const { setCookieWithCleanup } = setup()
await setCookieWithCleanup(COOKIE_NAME, 'value1', 1000)
await setCookieWithCleanup(COOKIE_NAME, 'value2', 1000, { secure: true, partitioned: true })
await setCookieWithCleanup(COOKIE_NAME, 'value1', ONE_MINUTE)
await setCookieWithCleanup(COOKIE_NAME, 'value2', ONE_MINUTE, { secure: true, partitioned: true })

const cookieAccess = createCookieAccess(COOKIE_NAME, MOCK_CONFIGURATION, COOKIE_OPTIONS)

let capturedValues: string[] | undefined
await cookieAccess.getAllAndSet((values) => {
capturedValues = values
return { value: 'new', expireDelay: 1000 }
return { value: 'new', expireDelay: ONE_MINUTE }
})

expect(capturedValues).toEqual(['value1', 'value2'])
Expand All @@ -173,15 +175,15 @@ describe('cookieAccess', () => {
const subscription = cookieAccess.observable.subscribe(spy)
registerCleanupTask(() => subscription.unsubscribe())

await setCookieWithCleanup(COOKIE_NAME, 'external', 1000)
await setCookieWithCleanup(COOKIE_NAME, 'external', ONE_MINUTE)
await flushObservable(spy)

expect(spy).toHaveBeenCalledTimes(1)
})

it('should notify when cookie is deleted externally', async () => {
const { flushObservable, setCookieWithCleanup } = setup()
await setCookieWithCleanup(COOKIE_NAME, 'existing', 1000)
await setCookieWithCleanup(COOKIE_NAME, 'existing', ONE_MINUTE)

const cookieAccess = createCookieAccess(COOKIE_NAME, MOCK_CONFIGURATION, COOKIE_OPTIONS)
const spy = jasmine.createSpy('observer')
Expand All @@ -196,7 +198,7 @@ describe('cookieAccess', () => {

it('should not notify when cookie value is unchanged', async () => {
const { clock, setCookieWithCleanup } = setup()
await setCookieWithCleanup(COOKIE_NAME, 'stable', 1000)
await setCookieWithCleanup(COOKIE_NAME, 'stable', ONE_MINUTE)

const cookieAccess = createCookieAccess(COOKIE_NAME, MOCK_CONFIGURATION, COOKIE_OPTIONS)
const spy = jasmine.createSpy('observer')
Expand All @@ -215,7 +217,7 @@ describe('cookieAccess', () => {
const subscription = cookieAccess.observable.subscribe(spy)
registerCleanupTask(() => subscription.unsubscribe())

await cookieAccess.getAllAndSet(() => ({ value: 'written', expireDelay: 1000 }))
await cookieAccess.getAllAndSet(() => ({ value: 'written', expireDelay: ONE_MINUTE }))
await flushObservable(spy)

expect(spy).toHaveBeenCalledTimes(1)
Expand Down
Loading