@openobserve/browser-core 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/boot/init.js +1 -1
- package/cjs/domain/configuration/endpointBuilder.js +1 -1
- package/cjs/domain/tags.js +1 -1
- package/cjs/domain/telemetry/telemetry.js +1 -1
- package/cjs/domain/telemetry/telemetryEvent.types.d.ts +1 -1
- package/esm/boot/init.js +1 -1
- package/esm/domain/configuration/endpointBuilder.js +1 -1
- package/esm/domain/tags.js +1 -1
- package/esm/domain/telemetry/telemetry.js +1 -1
- package/esm/domain/telemetry/telemetryEvent.types.d.ts +1 -1
- package/package.json +2 -2
- package/src/boot/displayAlreadyInitializedError.spec.ts +18 -0
- package/src/boot/init.spec.ts +50 -0
- package/src/browser/addEventListener.spec.ts +141 -0
- package/src/browser/cookie.spec.ts +53 -0
- package/src/browser/fetchObservable.spec.ts +334 -0
- package/src/browser/pageMayExitObservable.spec.ts +56 -0
- package/src/browser/xhrObservable.spec.ts +405 -0
- package/src/domain/allowedTrackingOrigins.spec.ts +210 -0
- package/src/domain/bufferedData.spec.ts +34 -0
- package/src/domain/configuration/configuration.spec.ts +236 -0
- package/src/domain/configuration/endpointBuilder.spec.ts +173 -0
- package/src/domain/configuration/transportConfiguration.spec.ts +130 -0
- package/src/domain/connectivity/connectivity.spec.ts +50 -0
- package/src/domain/console/consoleObservable.spec.ts +151 -0
- package/src/domain/context/contextManager.spec.ts +152 -0
- package/src/domain/context/contextUtils.spec.ts +26 -0
- package/src/domain/context/storeContextManager.spec.ts +103 -0
- package/src/domain/contexts/accountContext.spec.ts +99 -0
- package/src/domain/contexts/globalContext.spec.ts +88 -0
- package/src/domain/contexts/userContext.spec.ts +156 -0
- package/src/domain/error/error.spec.ts +294 -0
- package/src/domain/error/trackRuntimeError.spec.ts +343 -0
- package/src/domain/eventRateLimiter/createEventRateLimiter.spec.ts +119 -0
- package/src/domain/extension/extensionUtils.spec.ts +47 -0
- package/src/domain/report/reportObservable.spec.ts +71 -0
- package/src/domain/session/oldCookiesMigration.spec.ts +76 -0
- package/src/domain/session/sessionManager.spec.ts +684 -0
- package/src/domain/session/sessionState.spec.ts +88 -0
- package/src/domain/session/sessionStore.spec.ts +647 -0
- package/src/domain/session/sessionStoreOperations.spec.ts +234 -0
- package/src/domain/session/storeStrategies/sessionInCookie.spec.ts +228 -0
- package/src/domain/session/storeStrategies/sessionInLocalStorage.spec.ts +75 -0
- package/src/domain/synthetics/syntheticsWorkerValues.spec.ts +82 -0
- package/src/domain/tags.spec.ts +74 -0
- package/src/domain/telemetry/telemetry.spec.ts +494 -0
- package/src/domain/telemetry/telemetryEvent.types.ts +1 -1
- package/src/domain/trackingConsent.spec.ts +44 -0
- package/src/tools/abstractHooks.spec.ts +76 -0
- package/src/tools/abstractLifeCycle.spec.ts +46 -0
- package/src/tools/boundedBuffer.spec.ts +40 -0
- package/src/tools/catchUserErrors.spec.ts +20 -0
- package/src/tools/encoder.spec.ts +112 -0
- package/src/tools/experimentalFeatures.spec.ts +60 -0
- package/src/tools/getZoneJsOriginalValue.spec.ts +36 -0
- package/src/tools/instrumentMethod.spec.ts +381 -0
- package/src/tools/matchOption.spec.ts +41 -0
- package/src/tools/mergeInto.spec.ts +198 -0
- package/src/tools/monitor.spec.ts +172 -0
- package/src/tools/observable.spec.ts +259 -0
- package/src/tools/queueMicrotask.spec.ts +24 -0
- package/src/tools/readBytesFromStream.spec.ts +62 -0
- package/src/tools/requestIdleCallback.spec.ts +65 -0
- package/src/tools/serialisation/jsonStringify.spec.ts +76 -0
- package/src/tools/serialisation/sanitize.spec.ts +284 -0
- package/src/tools/stackTrace/capturedExceptions.specHelper.ts +316 -0
- package/src/tools/stackTrace/computeStackTrace.spec.ts +1007 -0
- package/src/tools/stackTrace/handlingStack.spec.ts +20 -0
- package/src/tools/taskQueue.spec.ts +60 -0
- package/src/tools/timer.spec.ts +76 -0
- package/src/tools/utils/browserDetection.spec.ts +120 -0
- package/src/tools/utils/byteUtils.spec.ts +29 -0
- package/src/tools/utils/functionUtils.spec.ts +229 -0
- package/src/tools/utils/numberUtils.spec.ts +27 -0
- package/src/tools/utils/stringUtils.spec.ts +74 -0
- package/src/tools/utils/typeUtils.spec.ts +25 -0
- package/src/tools/utils/urlPolyfill.spec.ts +55 -0
- package/src/tools/valueHistory.spec.ts +180 -0
- package/src/transport/batch.spec.ts +261 -0
- package/src/transport/eventBridge.spec.ts +90 -0
- package/src/transport/flushController.spec.ts +267 -0
- package/src/transport/httpRequest.spec.ts +400 -0
- package/src/transport/sendWithRetryStrategy.spec.ts +393 -0
package/cjs/boot/init.js
CHANGED
|
@@ -7,7 +7,7 @@ const monitor_1 = require("../tools/monitor");
|
|
|
7
7
|
const display_1 = require("../tools/display");
|
|
8
8
|
function makePublicApi(stub) {
|
|
9
9
|
const publicApi = {
|
|
10
|
-
version: "0.3.
|
|
10
|
+
version: "0.3.4",
|
|
11
11
|
// This API method is intentionally not monitored, since the only thing executed is the
|
|
12
12
|
// user-provided 'callback'. All SDK usages executed in the callback should be monitored, and
|
|
13
13
|
// we don't want to interfere with the user uncaught exceptions.
|
|
@@ -60,7 +60,7 @@ function buildEndpointParameters({ clientToken, internalAnalyticsSubdomain, sour
|
|
|
60
60
|
const parameters = [
|
|
61
61
|
`o2source=${source}`,
|
|
62
62
|
`o2-api-key=${clientToken}`,
|
|
63
|
-
`o2-evp-origin-version=${encodeURIComponent("0.3.
|
|
63
|
+
`o2-evp-origin-version=${encodeURIComponent("0.3.4")}`,
|
|
64
64
|
'o2-evp-origin=browser',
|
|
65
65
|
`o2-request-id=${(0, stringUtils_1.generateUUID)()}`,
|
|
66
66
|
].concat(extraParameters);
|
package/cjs/domain/tags.js
CHANGED
|
@@ -9,7 +9,7 @@ const display_1 = require("../tools/display");
|
|
|
9
9
|
exports.TAG_SIZE_LIMIT = 200;
|
|
10
10
|
function buildTags(configuration) {
|
|
11
11
|
const { env, service, version, datacenter, sdkVersion, variant } = configuration;
|
|
12
|
-
const tags = [buildTag('sdk_version', sdkVersion !== null && sdkVersion !== void 0 ? sdkVersion : "0.3.
|
|
12
|
+
const tags = [buildTag('sdk_version', sdkVersion !== null && sdkVersion !== void 0 ? sdkVersion : "0.3.4")];
|
|
13
13
|
if (env) {
|
|
14
14
|
tags.push(buildTag('env', env));
|
|
15
15
|
}
|
|
@@ -105,7 +105,7 @@ function startTelemetryCollection(telemetryService, configuration, hooks, observ
|
|
|
105
105
|
type: 'telemetry',
|
|
106
106
|
date: clockNow.timeStamp,
|
|
107
107
|
service: telemetryService,
|
|
108
|
-
version: "0.3.
|
|
108
|
+
version: "0.3.4",
|
|
109
109
|
source: 'browser',
|
|
110
110
|
_oo: {
|
|
111
111
|
format_version: 2,
|
|
@@ -218,7 +218,7 @@ export type TelemetryConfigurationEvent = CommonTelemetryProperties & {
|
|
|
218
218
|
/**
|
|
219
219
|
* A list of selected tracing propagators
|
|
220
220
|
*/
|
|
221
|
-
selected_tracing_propagators?: ('tracecontext' | 'b3' | 'b3multi')[];
|
|
221
|
+
selected_tracing_propagators?: ('openobserve' | 'tracecontext' | 'b3' | 'b3multi')[];
|
|
222
222
|
/**
|
|
223
223
|
* Session replay default privacy level
|
|
224
224
|
*/
|
package/esm/boot/init.js
CHANGED
|
@@ -3,7 +3,7 @@ import { setDebugMode } from '../tools/monitor';
|
|
|
3
3
|
import { display } from '../tools/display';
|
|
4
4
|
export function makePublicApi(stub) {
|
|
5
5
|
const publicApi = {
|
|
6
|
-
version: "0.3.
|
|
6
|
+
version: "0.3.4",
|
|
7
7
|
// This API method is intentionally not monitored, since the only thing executed is the
|
|
8
8
|
// user-provided 'callback'. All SDK usages executed in the callback should be monitored, and
|
|
9
9
|
// we don't want to interfere with the user uncaught exceptions.
|
|
@@ -56,7 +56,7 @@ function buildEndpointParameters({ clientToken, internalAnalyticsSubdomain, sour
|
|
|
56
56
|
const parameters = [
|
|
57
57
|
`o2source=${source}`,
|
|
58
58
|
`o2-api-key=${clientToken}`,
|
|
59
|
-
`o2-evp-origin-version=${encodeURIComponent("0.3.
|
|
59
|
+
`o2-evp-origin-version=${encodeURIComponent("0.3.4")}`,
|
|
60
60
|
'o2-evp-origin=browser',
|
|
61
61
|
`o2-request-id=${generateUUID()}`,
|
|
62
62
|
].concat(extraParameters);
|
package/esm/domain/tags.js
CHANGED
|
@@ -2,7 +2,7 @@ import { DOCS_ORIGIN, MORE_DETAILS, display } from '../tools/display';
|
|
|
2
2
|
export const TAG_SIZE_LIMIT = 200;
|
|
3
3
|
export function buildTags(configuration) {
|
|
4
4
|
const { env, service, version, datacenter, sdkVersion, variant } = configuration;
|
|
5
|
-
const tags = [buildTag('sdk_version', sdkVersion !== null && sdkVersion !== void 0 ? sdkVersion : "0.3.
|
|
5
|
+
const tags = [buildTag('sdk_version', sdkVersion !== null && sdkVersion !== void 0 ? sdkVersion : "0.3.4")];
|
|
6
6
|
if (env) {
|
|
7
7
|
tags.push(buildTag('env', env));
|
|
8
8
|
}
|
|
@@ -218,7 +218,7 @@ export type TelemetryConfigurationEvent = CommonTelemetryProperties & {
|
|
|
218
218
|
/**
|
|
219
219
|
* A list of selected tracing propagators
|
|
220
220
|
*/
|
|
221
|
-
selected_tracing_propagators?: ('tracecontext' | 'b3' | 'b3multi')[];
|
|
221
|
+
selected_tracing_propagators?: ('openobserve' | 'tracecontext' | 'b3' | 'b3multi')[];
|
|
222
222
|
/**
|
|
223
223
|
* Session replay default privacy level
|
|
224
224
|
*/
|
package/package.json
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { InitConfiguration } from '../domain/configuration'
|
|
2
|
+
import { display } from '../tools/display'
|
|
3
|
+
import { displayAlreadyInitializedError } from './displayAlreadyInitializedError'
|
|
4
|
+
|
|
5
|
+
describe('displayAlreadyInitializedError', () => {
|
|
6
|
+
it('should display an error', () => {
|
|
7
|
+
const displayErrorSpy = spyOn(display, 'error')
|
|
8
|
+
displayAlreadyInitializedError('OO_RUM', {} as InitConfiguration)
|
|
9
|
+
expect(displayErrorSpy).toHaveBeenCalledTimes(1)
|
|
10
|
+
expect(displayErrorSpy).toHaveBeenCalledWith('OO_RUM is already initialized.')
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('should not display an error if the "silentMultipleInit" option is used', () => {
|
|
14
|
+
const displayErrorSpy = spyOn(display, 'error')
|
|
15
|
+
displayAlreadyInitializedError('OO_RUM', { silentMultipleInit: true } as InitConfiguration)
|
|
16
|
+
expect(displayErrorSpy).not.toHaveBeenCalled()
|
|
17
|
+
})
|
|
18
|
+
})
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { display } from '../tools/display'
|
|
2
|
+
import { defineGlobal } from './init'
|
|
3
|
+
|
|
4
|
+
describe('defineGlobal', () => {
|
|
5
|
+
it('adds new property to the global object', () => {
|
|
6
|
+
const myGlobal = {} as any
|
|
7
|
+
const value = 'my value'
|
|
8
|
+
defineGlobal(myGlobal, 'foo', value)
|
|
9
|
+
expect(myGlobal.foo).toBe(value)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('overrides property if exists on the global object', () => {
|
|
13
|
+
const myGlobal = { foo: 'old value' }
|
|
14
|
+
const value = 'my value'
|
|
15
|
+
defineGlobal(myGlobal, 'foo', value)
|
|
16
|
+
expect(myGlobal.foo).toBe(value)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('run the queued callbacks on the old value', () => {
|
|
20
|
+
const fn1 = jasmine.createSpy()
|
|
21
|
+
const fn2 = jasmine.createSpy()
|
|
22
|
+
const myGlobal: any = {
|
|
23
|
+
foo: {
|
|
24
|
+
q: [fn1, fn2],
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
const value = 'my value'
|
|
28
|
+
defineGlobal(myGlobal, 'foo', value)
|
|
29
|
+
expect(myGlobal.foo).toBe(value)
|
|
30
|
+
expect(fn1).toHaveBeenCalled()
|
|
31
|
+
expect(fn2).toHaveBeenCalled()
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('catches the errors thrown by the queued callbacks', () => {
|
|
35
|
+
const myError = 'Ooops!'
|
|
36
|
+
const onReady = () => {
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/only-throw-error
|
|
38
|
+
throw myError
|
|
39
|
+
}
|
|
40
|
+
const myGlobal: any = {
|
|
41
|
+
foo: {
|
|
42
|
+
q: [onReady],
|
|
43
|
+
},
|
|
44
|
+
}
|
|
45
|
+
const displaySpy = spyOn(display, 'error')
|
|
46
|
+
|
|
47
|
+
defineGlobal(myGlobal, 'foo', {})
|
|
48
|
+
expect(displaySpy).toHaveBeenCalledWith('onReady callback threw an error:', myError)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// import type { Configuration } from '@openobserve/browser-core'
|
|
2
|
+
// import { stubZoneJs } from '../../test'
|
|
3
|
+
import type { Configuration } from '../domain/configuration'
|
|
4
|
+
import { createNewEvent, mockZoneJs, registerCleanupTask } from '../../test'
|
|
5
|
+
import type { MockZoneJs } from '../../test'
|
|
6
|
+
import { noop } from '../tools/utils/functionUtils'
|
|
7
|
+
import { addEventListener, DOM_EVENT } from './addEventListener'
|
|
8
|
+
|
|
9
|
+
describe('addEventListener', () => {
|
|
10
|
+
let configuration: Configuration
|
|
11
|
+
|
|
12
|
+
describe('Zone.js support', () => {
|
|
13
|
+
let zoneJs: MockZoneJs
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
configuration = { allowUntrustedEvents: false } as Configuration
|
|
17
|
+
zoneJs = mockZoneJs()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('uses the original addEventListener method instead of the method patched by Zone.js', () => {
|
|
21
|
+
const zoneJsPatchedAddEventListener = jasmine.createSpy()
|
|
22
|
+
const eventTarget = document.createElement('div')
|
|
23
|
+
zoneJs.replaceProperty(eventTarget, 'addEventListener', zoneJsPatchedAddEventListener)
|
|
24
|
+
|
|
25
|
+
addEventListener(configuration, eventTarget, DOM_EVENT.CLICK, noop)
|
|
26
|
+
expect(zoneJsPatchedAddEventListener).not.toHaveBeenCalled()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('uses the original removeEventListener method instead of the method patched by Zone.js', () => {
|
|
30
|
+
const zoneJsPatchedRemoveEventListener = jasmine.createSpy()
|
|
31
|
+
const eventTarget = document.createElement('div')
|
|
32
|
+
zoneJs.replaceProperty(eventTarget, 'removeEventListener', zoneJsPatchedRemoveEventListener)
|
|
33
|
+
|
|
34
|
+
const { stop } = addEventListener(configuration, eventTarget, DOM_EVENT.CLICK, noop)
|
|
35
|
+
stop()
|
|
36
|
+
expect(zoneJsPatchedRemoveEventListener).not.toHaveBeenCalled()
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('Use the EventTarget.prototype.addEventListener when the eventTarget is an instance of EventTarget', () => {
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
42
|
+
const originalAddEventListener = EventTarget.prototype.addEventListener
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
44
|
+
const originalRemoveEventListener = EventTarget.prototype.removeEventListener
|
|
45
|
+
|
|
46
|
+
EventTarget.prototype.addEventListener = jasmine.createSpy()
|
|
47
|
+
EventTarget.prototype.removeEventListener = jasmine.createSpy()
|
|
48
|
+
|
|
49
|
+
registerCleanupTask(() => {
|
|
50
|
+
EventTarget.prototype.addEventListener = originalAddEventListener
|
|
51
|
+
EventTarget.prototype.removeEventListener = originalRemoveEventListener
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const htmlDivElement = document.createElement('div')
|
|
55
|
+
htmlDivElement.addEventListener = jasmine.createSpy()
|
|
56
|
+
htmlDivElement.removeEventListener = jasmine.createSpy()
|
|
57
|
+
|
|
58
|
+
const { stop } = addEventListener({ allowUntrustedEvents: false }, htmlDivElement, DOM_EVENT.CLICK, noop)
|
|
59
|
+
|
|
60
|
+
const event = createNewEvent(DOM_EVENT.CLICK)
|
|
61
|
+
htmlDivElement.dispatchEvent(event)
|
|
62
|
+
stop()
|
|
63
|
+
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
65
|
+
expect(htmlDivElement.addEventListener).not.toHaveBeenCalled()
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
67
|
+
expect(htmlDivElement.removeEventListener).not.toHaveBeenCalled()
|
|
68
|
+
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
70
|
+
expect(EventTarget.prototype.addEventListener).toHaveBeenCalled()
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
72
|
+
expect(EventTarget.prototype.removeEventListener).toHaveBeenCalled()
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('Use the addEventListener method when the eventTarget is not an instance of EventTarget', () => {
|
|
76
|
+
const listener = jasmine.createSpy()
|
|
77
|
+
|
|
78
|
+
const customEventTarget = {
|
|
79
|
+
addEventListener: jasmine.createSpy(),
|
|
80
|
+
removeEventListener: jasmine.createSpy(),
|
|
81
|
+
} as unknown as HTMLElement
|
|
82
|
+
|
|
83
|
+
const { stop } = addEventListener({ allowUntrustedEvents: false }, customEventTarget, 'change', listener)
|
|
84
|
+
stop()
|
|
85
|
+
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
87
|
+
expect(customEventTarget.addEventListener).toHaveBeenCalled()
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
89
|
+
expect(customEventTarget.removeEventListener).toHaveBeenCalled()
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
describe('Untrusted event', () => {
|
|
93
|
+
beforeEach(() => {
|
|
94
|
+
configuration = { allowUntrustedEvents: false } as Configuration
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('should be ignored if __ooIsTrusted is absent', () => {
|
|
98
|
+
const listener = jasmine.createSpy()
|
|
99
|
+
const eventTarget = document.createElement('div')
|
|
100
|
+
addEventListener(configuration, eventTarget, DOM_EVENT.CLICK, listener)
|
|
101
|
+
|
|
102
|
+
const event = createNewEvent(DOM_EVENT.CLICK, { __ooIsTrusted: undefined })
|
|
103
|
+
eventTarget.dispatchEvent(event)
|
|
104
|
+
expect(listener).not.toHaveBeenCalled()
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('should be ignored if __ooIsTrusted is false', () => {
|
|
108
|
+
const listener = jasmine.createSpy()
|
|
109
|
+
const eventTarget = document.createElement('div')
|
|
110
|
+
addEventListener(configuration, eventTarget, DOM_EVENT.CLICK, listener)
|
|
111
|
+
|
|
112
|
+
const event = createNewEvent(DOM_EVENT.CLICK, { __ooIsTrusted: false })
|
|
113
|
+
eventTarget.dispatchEvent(event)
|
|
114
|
+
expect(listener).not.toHaveBeenCalled()
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('should not be ignored if __ooIsTrusted is true', () => {
|
|
118
|
+
const listener = jasmine.createSpy()
|
|
119
|
+
const eventTarget = document.createElement('div')
|
|
120
|
+
addEventListener(configuration, eventTarget, DOM_EVENT.CLICK, listener)
|
|
121
|
+
|
|
122
|
+
const event = createNewEvent(DOM_EVENT.CLICK, { __ooIsTrusted: true })
|
|
123
|
+
eventTarget.dispatchEvent(event)
|
|
124
|
+
|
|
125
|
+
expect(listener).toHaveBeenCalled()
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('should not be ignored if allowUntrustedEvents is true', () => {
|
|
129
|
+
const listener = jasmine.createSpy()
|
|
130
|
+
const eventTarget = document.createElement('div')
|
|
131
|
+
configuration = { allowUntrustedEvents: true } as Configuration
|
|
132
|
+
|
|
133
|
+
addEventListener(configuration, eventTarget, DOM_EVENT.CLICK, listener)
|
|
134
|
+
|
|
135
|
+
const event = createNewEvent(DOM_EVENT.CLICK, { __ooIsTrusted: undefined })
|
|
136
|
+
eventTarget.dispatchEvent(event)
|
|
137
|
+
|
|
138
|
+
expect(listener).toHaveBeenCalled()
|
|
139
|
+
})
|
|
140
|
+
})
|
|
141
|
+
})
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { mockCookies } from '../../test'
|
|
2
|
+
import { getCurrentSite, resetGetCurrentSite } from './cookie'
|
|
3
|
+
|
|
4
|
+
describe('cookie', () => {
|
|
5
|
+
describe('getCurrentSite', () => {
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
resetGetCurrentSite()
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('returns the eTLD+1 for example.com', () => {
|
|
11
|
+
mockCookies()
|
|
12
|
+
expect(getCurrentSite('example.com')).toBe('example.com')
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('returns the eTLD+1 for example.co.uk', () => {
|
|
16
|
+
mockCookies({
|
|
17
|
+
filter: (cookie) => cookie.domain !== '.co.uk',
|
|
18
|
+
})
|
|
19
|
+
expect(getCurrentSite('example.co.uk')).toBe('example.co.uk')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('returns the eTLD+1 for foo.bar.baz.example.com', () => {
|
|
23
|
+
mockCookies()
|
|
24
|
+
expect(getCurrentSite('foo.bar.baz.example.com')).toBe('example.com')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('does not left any cookies', () => {
|
|
28
|
+
const { getCookies } = mockCookies()
|
|
29
|
+
expect(getCurrentSite('example.com')).toBe('example.com')
|
|
30
|
+
expect(getCookies()).toEqual([])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('falls back to the referrer when the hostname is empty', () => {
|
|
34
|
+
mockCookies()
|
|
35
|
+
expect(getCurrentSite('', 'https://example.com')).toBe('example.com')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('returns undefined when the referrer is empty', () => {
|
|
39
|
+
mockCookies()
|
|
40
|
+
expect(getCurrentSite('', '')).toBeUndefined()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('caches the result', () => {
|
|
44
|
+
const { setter } = mockCookies()
|
|
45
|
+
|
|
46
|
+
expect(getCurrentSite('example.com')).toBe('example.com')
|
|
47
|
+
expect(setter).toHaveBeenCalledTimes(2)
|
|
48
|
+
|
|
49
|
+
expect(getCurrentSite('example.com')).toBe('example.com')
|
|
50
|
+
expect(setter).toHaveBeenCalledTimes(2)
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
})
|