-
Notifications
You must be signed in to change notification settings - Fork 534
Expand file tree
/
Copy pathanalytics.test.js
More file actions
219 lines (211 loc) · 8.85 KB
/
Copy pathanalytics.test.js
File metadata and controls
219 lines (211 loc) · 8.85 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* global describe, it, expect, beforeEach, afterEach */
import { jest } from '@jest/globals'
import { composeBundlesRaw } from 'redux-bundler'
import createAnalyticsBundle from './analytics.js'
beforeEach(() => {
global.Countly = {
q: [],
opt_out: jest.fn(),
opt_in: jest.fn(),
init: jest.fn()
}
})
afterEach(() => {
delete global.Countly
})
function createStore (analyticsOpts = {}, mockAnalyticsCachedState) {
return composeBundlesRaw(
{
name: 'mockRoutesBundle',
selectRouteInfo: () => ({})
},
{
name: 'mockIpfsDesktopBundle',
selectIsIpfsDesktop: () => false,
selectDesktopCountlyActions: () => ([])
},
createAnalyticsBundle(analyticsOpts)
)({
// tests using mockAnalyticsCachedState are mimicking the createCacheBundle that sets initial state
analytics: mockAnalyticsCachedState
})
}
describe('new/returning user default behavior', () => {
// 2024-Q2: disabling and hiding all metrics for now
// https://github.com/ipfs/ipfs-webui/pull/2216
it('should disable analytics by default and hide all UI compontents', () => {
const store = createStore()
expect(store.selectAnalyticsEnabled()).toBe(false)
expect(store.selectShowAnalyticsComponents()).toBe(false)
expect(store.selectAnalyticsConsent()).toEqual([])
expect(global.Countly.opt_in).not.toHaveBeenCalled()
expect(global.Countly.opt_out).not.toHaveBeenCalled()
})
/*
it('should enable analytics by default for new user who has not opted in or out', () => {
const store = createStore()
expect(global.Countly.opt_in).toHaveBeenCalled()
expect(global.Countly.opt_in.mock.calls.length).toBe(1)
// events will be sent as consents have been given by default
expect(store.selectAnalyticsConsent()).toEqual(['sessions', 'events', 'views', 'location'])
})
*/
it('should enable existing analytics by default for returning user who was opted_in', () => {
const mockDefaultState = {
lastEnabledAt: (new Date('2022-01-02')).getTime(),
lastDisabledAt: 0,
showAnalyticsBanner: false,
optedOut: false,
consent: ['sessions', 'events', 'views']
}
const store = createStore({}, mockDefaultState)
expect(store.selectAnalyticsEnabled()).toBe(true)
expect(store.selectAnalyticsConsent()).toEqual(['sessions', 'events', 'views'])
// should not show analytics banner for these users
expect(store.selectShowAnalyticsBanner()).toBe(false)
})
/* This test is disabled because analytics are completely disabled when DISABLE_ALL_ANALYTICS is true.
See: https://github.com/ipfs/ipfs-webui/issues/2334
it('should enable analytics for returning user who opted_out prior to new opt-in by default updates', () => {
const mockDefaultState = {
lastEnabledAt: 0,
lastDisabledAt: (new Date('2022-01-02')).getTime(),
showAnalyticsBanner: false,
optedOut: false,
consent: []
}
const store = createStore({}, mockDefaultState)
expect(global.Countly.opt_in).toHaveBeenCalled()
expect(global.Countly.opt_in.mock.calls.length).toBe(1)
expect(store.selectAnalyticsConsent()).toEqual(['sessions', 'events', 'views', 'location'])
// should show analytics banner for these users
expect(store.selectShowAnalyticsBanner()).toBe(true)
}) */
it('should remain disabled by default when DISABLE_ALL_ANALYTICS is true', () => {
const store = createStore({})
expect(global.Countly.opt_in).not.toHaveBeenCalled()
expect(store.selectAnalyticsConsent()).toEqual([])
expect(store.selectAnalyticsOptedOut()).toBe(true)
expect(store.selectShowAnalyticsBanner()).toBe(false)
})
/* This test is disabled because analytics are completely disabled when DISABLE_ALL_ANALYTICS is true.
See: https://github.com/ipfs/ipfs-webui/issues/2334
it('should hide analytics banner if user has closed the banner', () => {
const mockDefaultState = {
lastEnabledAt: 0,
lastDisabledAt: (new Date('2022-01-01')).getTime(),
showAnalyticsBanner: false,
optedOut: false,
consent: []
}
const store = createStore({}, mockDefaultState)
expect(store.selectShowAnalyticsBanner()).toBe(true)
expect(store.selectAnalyticsConsent()).toEqual(['sessions', 'events', 'views', 'location'])
store.doToggleShowAnalyticsBanner(false)
expect(store.selectShowAnalyticsBanner()).toBe(false)
expect(store.selectAnalyticsOptedOutPriorToDefaultOptIn()).toBe(false)
}) */
})
describe('user enables and disables analytics', () => {
it('should enable analytics if user who has opted out explicitly enables it', () => {
const mockDefaultState = {
lastEnabledAt: 0,
lastDisabledAt: (new Date('2022-01-01')).getTime(),
showAnalyticsBanner: false,
optedOut: true,
consent: []
}
const store = createStore({}, mockDefaultState)
store.doEnableAnalytics()
expect(store.selectAnalyticsEnabled()).toBe(true)
expect(store.selectAnalyticsConsent()).toEqual(['sessions', 'events', 'views', 'location'])
expect(global.Countly.opt_in).toHaveBeenCalled()
expect(global.Countly.opt_out).not.toHaveBeenCalled()
expect(global.Countly.opt_in.mock.calls.length).toBe(1)
})
it('should disable analytics if user explicitly disables it', () => {
const store = createStore()
store.doDisableAnalytics()
expect(store.selectAnalyticsEnabled()).toBe(false)
expect(store.selectAnalyticsConsent()).toEqual([])
expect(global.Countly.opt_out).toHaveBeenCalled()
expect(global.Countly.opt_out.mock.calls.length).toBe(1)
})
})
describe('user manages all analytics consent with settings page toggle', () => {
it('should toggle analytics consent off for user with consent enabled', () => {
const mockDefaultState = {
lastEnabledAt: (new Date('2022-01-02')).getTime(),
lastDisabledAt: 0,
consent: ['sessions', 'events', 'views'],
optedOut: false
}
const store = createStore({}, mockDefaultState)
store.doToggleAnalytics()
expect(store.selectAnalyticsEnabled()).toBe(false)
expect(store.selectAnalyticsConsent()).toEqual([])
expect(store.selectAnalyticsOptedOut()).toBe(true)
expect(global.Countly.opt_in).not.toHaveBeenCalled()
expect(global.Countly.opt_out).toHaveBeenCalled()
expect(global.Countly.opt_out.mock.calls.length).toBe(1)
})
it('should toggle analytics consent on for user with consent disabled', () => {
const mockDefaultState = {
lastEnabledAt: (new Date('2022-01-01')).getTime(),
lastDisabledAt: (new Date('2022-01-02')).getTime(),
consent: [],
optedOut: true
}
const store = createStore({}, mockDefaultState)
expect(store.selectAnalyticsEnabled()).toBe(false)
store.doToggleAnalytics()
expect(store.selectAnalyticsEnabled()).toBe(true)
expect(store.selectAnalyticsConsent()).toEqual(['sessions', 'events', 'views', 'location'])
expect(store.selectAnalyticsOptedOut()).toBe(false)
expect(global.Countly.opt_in).toHaveBeenCalled()
expect(global.Countly.opt_out).not.toHaveBeenCalled()
expect(global.Countly.opt_in.mock.calls.length).toBe(1)
})
})
describe('user manages analytics consent with individual settings toggles', () => {
it('should toggle consent on for "crashes"', () => {
const mockDefaultState = {
lastEnabledAt: (new Date('2022-01-02')).getTime(),
lastDisabledAt: (new Date('2022-01-01')).getTime(),
consent: ['sessions', 'events', 'views', 'location'],
optedOut: false
}
const store = createStore({}, mockDefaultState)
store.doToggleConsent('crashes')
expect(store.selectAnalyticsEnabled()).toBe(true)
expect(store.selectAnalyticsConsent()).toEqual(['sessions', 'events', 'views', 'location', 'crashes'])
})
it('should toggle consent off for one ("sessions")', () => {
const mockDefaultState = {
lastEnabledAt: (new Date('2022-01-02')).getTime(),
lastDisabledAt: (new Date('2022-01-01')).getTime(),
consent: ['sessions', 'events', 'views', 'location'],
optedOut: false
}
const store = createStore({}, mockDefaultState)
store.doToggleConsent('sessions')
expect(store.selectAnalyticsEnabled()).toBe(true)
expect(store.selectAnalyticsConsent()).toEqual(['events', 'views', 'location'])
})
it('should opt_out if consent is toggled off for all existing consents', () => {
const mockDefaultState = {
lastEnabledAt: (new Date('2022-01-02')).getTime(),
lastDisabledAt: (new Date('2022-01-01')).getTime(),
consent: ['views', 'location'],
optedOut: false
}
const store = createStore({}, mockDefaultState)
store.doToggleConsent('views')
store.doToggleConsent('location')
expect(store.selectAnalyticsEnabled()).toBe(false)
expect(store.selectAnalyticsOptedOut()).toBe(true)
expect(store.selectAnalyticsConsent()).toEqual([])
expect(global.Countly.opt_out).toHaveBeenCalled()
expect(global.Countly.opt_out.mock.calls.length).toBe(1)
})
})