-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathstoreContextManager.spec.ts
More file actions
102 lines (80 loc) · 3.92 KB
/
Copy pathstoreContextManager.spec.ts
File metadata and controls
102 lines (80 loc) · 3.92 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import type { Context } from '@datadog/js-core/assembly'
import { createNewEvent } from '../../../test'
import { DOM_EVENT } from '../../browser/addEventListener'
import { storeContextManager, buildStorageKey, removeStorageListeners } from './storeContextManager'
import { CustomerDataType } from './contextConstants'
import { createContextManager } from './contextManager'
describe('storeContextManager', () => {
const PRODUCT_KEY = 'fake'
const CUSTOMER_DATA_TYPE = CustomerDataType.User
const STORAGE_KEY = buildStorageKey(PRODUCT_KEY, CUSTOMER_DATA_TYPE)
afterEach(() => {
localStorage.clear()
removeStorageListeners()
})
it('should synchronize with local storage at start', () => {
localStorage.setItem(STORAGE_KEY, '{"bar":"foo"}')
const manager = createStoredContextManager()
expect(manager.getContext()).toEqual({ bar: 'foo' })
})
it('should synchronize with local storage on storage events', () => {
const manager = createStoredContextManager()
localStorage.setItem(STORAGE_KEY, '{"bar":"foo"}')
expect(manager.getContext()).toEqual({})
window.dispatchEvent(createNewEvent(DOM_EVENT.STORAGE, { key: 'unknown' }))
expect(manager.getContext()).toEqual({})
window.dispatchEvent(createNewEvent(DOM_EVENT.STORAGE, { key: STORAGE_KEY }))
expect(manager.getContext()).toEqual({ bar: 'foo' })
})
it('should update local storage on context updates', () => {
const manager = createStoredContextManager()
expect(localStorage.getItem(STORAGE_KEY)).toBe(null)
manager.setContext({ bar: 'foo' })
expect(localStorage.getItem(STORAGE_KEY)).toBe('{"bar":"foo"}')
manager.setContextProperty('qux', 'qix')
expect(localStorage.getItem(STORAGE_KEY)).toBe('{"bar":"foo","qux":"qix"}')
manager.removeContextProperty('qux')
expect(localStorage.getItem(STORAGE_KEY)).toBe('{"bar":"foo"}')
manager.clearContext()
expect(localStorage.getItem(STORAGE_KEY)).toBe('{}')
})
it('merges local storage data with initial context', () => {
localStorage.setItem(STORAGE_KEY, '{"bar":"foo"}')
const manager = createStoredContextManager({ initialContext: { qux: 'qix' } })
expect(manager.getContext()).toEqual({ bar: 'foo', qux: 'qix' })
expect(localStorage.getItem(STORAGE_KEY)).toBe('{"bar":"foo","qux":"qix"}')
})
it('should store different product data in different storage key', () => {
createStoredContextManager({ initialContext: { bar: 'foo' }, productKey: 'p1' })
createStoredContextManager({ initialContext: { qux: 'qix' }, productKey: 'p2' })
expect(localStorage.getItem(buildStorageKey('p1', CUSTOMER_DATA_TYPE))).toBe('{"bar":"foo"}')
expect(localStorage.getItem(buildStorageKey('p2', CUSTOMER_DATA_TYPE))).toBe('{"qux":"qix"}')
})
it('should store different data type in different storage key', () => {
createStoredContextManager({ initialContext: { bar: 'foo' }, customerDataType: CustomerDataType.User })
createStoredContextManager({ initialContext: { qux: 'qix' }, customerDataType: CustomerDataType.GlobalContext })
expect(localStorage.getItem(buildStorageKey(PRODUCT_KEY, CustomerDataType.User))).toBe('{"bar":"foo"}')
expect(localStorage.getItem(buildStorageKey(PRODUCT_KEY, CustomerDataType.GlobalContext))).toBe('{"qux":"qix"}')
})
it('should return empty context when localStorage contains invalid JSON', () => {
localStorage.setItem(STORAGE_KEY, 'not valid json')
const manager = createStoredContextManager()
expect(manager.getContext()).toEqual({})
})
function createStoredContextManager({
initialContext,
productKey = PRODUCT_KEY,
customerDataType = CUSTOMER_DATA_TYPE,
}: {
initialContext?: Context
productKey?: string
customerDataType?: CustomerDataType
} = {}) {
const manager = createContextManager('test')
if (initialContext) {
manager.setContext(initialContext)
}
storeContextManager(manager, productKey, customerDataType)
return manager
}
})