-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathcookieObservable.spec.ts
More file actions
86 lines (68 loc) · 3.25 KB
/
cookieObservable.spec.ts
File metadata and controls
86 lines (68 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { Clock } from '../../test'
import { mockClock } from '../../test'
import type { Configuration } from '../domain/configuration'
import type { Subscription } from '../tools/observable'
import { ONE_MINUTE } from '../tools/utils/timeUtils'
import { deleteCookie, setCookie } from './cookie'
import type { CookieStoreWindow } from './cookieObservable'
import { WATCH_COOKIE_INTERVAL_DELAY, createCookieObservable } from './cookieObservable'
const COOKIE_NAME = 'cookie_name'
const COOKIE_DURATION = ONE_MINUTE
function mockConfiguration(): Configuration {
return {} as Configuration
}
describe('cookieObservable', () => {
let subscription: Subscription
let originalSupportedEntryTypes: PropertyDescriptor | undefined
let clock: Clock
beforeEach(() => {
deleteCookie(COOKIE_NAME)
clock = mockClock()
originalSupportedEntryTypes = Object.getOwnPropertyDescriptor(window, 'cookieStore')
})
afterEach(() => {
subscription?.unsubscribe()
if (originalSupportedEntryTypes) {
Object.defineProperty(window, 'cookieStore', originalSupportedEntryTypes)
}
})
it('should notify observers on cookie change', async () => {
const observable = createCookieObservable(mockConfiguration(), COOKIE_NAME)
const cookieChangePromise = new Promise((resolve) => {
subscription = observable.subscribe(resolve)
})
// When writing a cookie just after subscribing to the cookieStore 'change' event, the 'change'
// event is sometimes not triggered, making this test case flaky.
// To work around this, we get some random cookie from the cookieStore. This adds enough delay
// to ensure that the 'change' event is triggered when we write our cookie.
// This was reported here: https://issues.chromium.org/issues/420405275
const cookieStore = (window as CookieStoreWindow).cookieStore
if (cookieStore) {
// Wait for the cookieStore to be ready
await cookieStore.get('some_cookie_name')
}
setCookie(COOKIE_NAME, 'foo', COOKIE_DURATION)
clock.tick(WATCH_COOKIE_INTERVAL_DELAY)
const cookieChange = await cookieChangePromise
expect(cookieChange).toEqual('foo')
})
it('should notify observers on cookie change when cookieStore is not supported', () => {
Object.defineProperty(window, 'cookieStore', { get: () => undefined, configurable: true })
const observable = createCookieObservable(mockConfiguration(), COOKIE_NAME)
let cookieChange: string | undefined
subscription = observable.subscribe((change) => (cookieChange = change))
setCookie(COOKIE_NAME, 'foo', COOKIE_DURATION)
clock.tick(WATCH_COOKIE_INTERVAL_DELAY)
expect(cookieChange).toEqual('foo')
})
it('should not notify observers on cookie change when the cookie value as not changed when cookieStore is not supported', () => {
Object.defineProperty(window, 'cookieStore', { get: () => undefined, configurable: true })
const observable = createCookieObservable(mockConfiguration(), COOKIE_NAME)
setCookie(COOKIE_NAME, 'foo', COOKIE_DURATION)
let cookieChange: string | undefined
subscription = observable.subscribe((change) => (cookieChange = change))
setCookie(COOKIE_NAME, 'foo', COOKIE_DURATION)
clock.tick(WATCH_COOKIE_INTERVAL_DELAY)
expect(cookieChange).toBeUndefined()
})
})