-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathCookiePreferencesManager.test.ts
executable file
·88 lines (81 loc) · 2.88 KB
/
CookiePreferencesManager.test.ts
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
87
88
#! /usr/bin/env jest
import {
PreferenceType,
parseRawCookieValue,
parseDate,
parsePreferences,
updatePreference,
getPreferenceValue,
serializeState,
arePreferencesOutdated,
} from "./cookiePreferences.js"
describe("cookie preferences", () => {
const preferences = [
{
type: PreferenceType.Analytics,
value: true,
},
]
const date = 20201009
const serializedState = "a:1-20201009"
it("parses raw cookie value", () => {
expect(parseRawCookieValue()).toBeUndefined()
expect(parseRawCookieValue("")).toBeUndefined()
expect(parseRawCookieValue("abcd")).toBeUndefined()
expect(parseRawCookieValue("a:1")).toBeUndefined()
expect(parseRawCookieValue("a:1-46")).toBeUndefined()
expect(parseRawCookieValue("a:1-2020")).toBeUndefined()
expect(parseRawCookieValue("1-20201009")).toBeUndefined()
expect(parseRawCookieValue(":1-20201009")).toBeUndefined()
expect(parseRawCookieValue("x:1-20201009")).toBeUndefined()
expect(parseRawCookieValue(serializedState)).toEqual({
preferences,
date,
})
})
it("parses date", () => {
expect(parseDate()).toBeUndefined()
expect(parseDate("")).toBeUndefined()
expect(parseDate("abcd")).toBeUndefined()
expect(parseDate("2020")).toBeUndefined()
expect(parseDate("20201032")).toBeUndefined()
expect(parseDate("20201001")).toEqual(20201001)
})
it("parses preferences", () => {
expect(parsePreferences()).toEqual([])
expect(parsePreferences("")).toEqual([])
expect(parsePreferences("a:1")).toEqual(preferences)
expect(parsePreferences("x:1")).toEqual([])
expect(parsePreferences("a:1|m:0")).toEqual([
...preferences,
{ type: PreferenceType.Marketing, value: false },
])
})
it("updates a preference", () => {
expect(
updatePreference(PreferenceType.Analytics, false, preferences)
).toEqual([
{
type: PreferenceType.Analytics,
value: false,
},
])
})
it("gets a preference value", () => {
expect(
getPreferenceValue(PreferenceType.Analytics, preferences)
).toEqual(true)
expect(
getPreferenceValue(PreferenceType.Marketing, preferences)
).toEqual(false)
})
it("serializes state", () => {
expect(serializeState({ preferences, date })).toEqual(serializedState)
})
it("checks if preferences are outdated", () => {
expect(arePreferencesOutdated(date - 1, date)).toEqual(true)
expect(arePreferencesOutdated(date, date)).toEqual(false)
expect(arePreferencesOutdated(date + 1, date)).toEqual(false)
expect(arePreferencesOutdated(undefined, date)).toEqual(false)
})
})