-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* ♻️ move clearAllCookies to its usage * ♻️ extract browserChecks * ♻️ extract buildEnv * ♻️ extract requests.specHelper * ♻️ extract createNewEvent * ♻️ extract location.specHelper * ♻️ extract mockClock * ♻️ rename specHelper to simpleTestDoubles * ♻️ move capturedExceptions to tracekit * 👌 remove specHelper prefix for files under test directory * 👌 extract test/emulate directory * 👌 split simpleTestDoubles
- Loading branch information
Showing
18 changed files
with
256 additions
and
243 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function isSafari() { | ||
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent) | ||
} | ||
|
||
export function isFirefox() { | ||
return navigator.userAgent.toLowerCase().indexOf('firefox') > -1 | ||
} | ||
|
||
export function isAdoptedStyleSheetsSupported() { | ||
return Boolean((document as any).adoptedStyleSheets) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// to simulate different build env behavior | ||
export interface BuildEnvWindow { | ||
__BUILD_ENV__SDK_VERSION__: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function stubCookie() { | ||
let cookie = '' | ||
return { | ||
getSpy: spyOnProperty(Document.prototype, 'cookie', 'get').and.callFake(() => cookie), | ||
setSpy: spyOnProperty(Document.prototype, 'cookie', 'set').and.callFake((newCookie) => { | ||
cookie = newCookie | ||
}), | ||
currentValue: () => cookie, | ||
setCurrentValue: (newCookie: string) => { | ||
cookie = newCookie | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { objectEntries } from '../../src' | ||
|
||
export function createNewEvent<P extends Record<string, unknown>>(eventName: 'click', properties?: P): MouseEvent & P | ||
export function createNewEvent<P extends Record<string, unknown>>( | ||
eventName: 'pointerup', | ||
properties?: P | ||
): PointerEvent & P | ||
export function createNewEvent(eventName: string, properties?: { [name: string]: unknown }): Event | ||
export function createNewEvent(eventName: string, properties: { [name: string]: unknown } = {}) { | ||
let event: Event | ||
if (typeof Event === 'function') { | ||
event = new Event(eventName) | ||
} else { | ||
event = document.createEvent('Event') | ||
event.initEvent(eventName, true, true) | ||
} | ||
objectEntries(properties).forEach(([name, value]) => { | ||
// Setting values directly or with a `value` descriptor seems unsupported in IE11 | ||
Object.defineProperty(event, name, { | ||
get() { | ||
return value | ||
}, | ||
}) | ||
}) | ||
return event | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { BrowserWindowWithEventBridge } from '../../src/transport' | ||
|
||
export function initEventBridgeStub(allowedWebViewHosts: string[] = [window.location.hostname]) { | ||
const eventBridgeStub = { | ||
send: (_msg: string) => undefined, | ||
getAllowedWebViewHosts: () => JSON.stringify(allowedWebViewHosts), | ||
} | ||
;(window as BrowserWindowWithEventBridge).DatadogEventBridge = eventBridgeStub | ||
return eventBridgeStub | ||
} | ||
|
||
export function deleteEventBridgeStub() { | ||
delete (window as BrowserWindowWithEventBridge).DatadogEventBridge | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { assign, buildUrl } from '../../src' | ||
|
||
export function mockLocation(initialUrl: string) { | ||
const fakeLocation = buildLocation(initialUrl) | ||
spyOn(history, 'pushState').and.callFake((_: any, __: string, pathname: string) => { | ||
assign(fakeLocation, buildLocation(pathname, fakeLocation.href)) | ||
}) | ||
|
||
function hashchangeCallBack() { | ||
fakeLocation.hash = window.location.hash | ||
fakeLocation.href = fakeLocation.href.replace(/#.*/, '') + window.location.hash | ||
} | ||
|
||
window.addEventListener('hashchange', hashchangeCallBack) | ||
return { | ||
location: fakeLocation, | ||
cleanup: () => { | ||
window.removeEventListener('hashchange', hashchangeCallBack) | ||
window.location.hash = '' | ||
}, | ||
} | ||
} | ||
|
||
export function buildLocation(url: string, base = location.href) { | ||
const urlObject = buildUrl(url, base) | ||
return { | ||
hash: urlObject.hash, | ||
href: urlObject.href, | ||
pathname: urlObject.pathname, | ||
search: urlObject.search, | ||
} as Location | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { resetNavigationStart } from '../../src/tools/timeUtils' | ||
|
||
export type Clock = ReturnType<typeof mockClock> | ||
|
||
export function mockClock(date?: Date) { | ||
jasmine.clock().install() | ||
jasmine.clock().mockDate(date) | ||
const start = Date.now() | ||
spyOn(performance, 'now').and.callFake(() => Date.now() - start) | ||
spyOnProperty(performance.timing, 'navigationStart', 'get').and.callFake(() => start) | ||
resetNavigationStart() | ||
return { | ||
tick: (ms: number) => jasmine.clock().tick(ms), | ||
setDate: (date: Date) => jasmine.clock().mockDate(date), | ||
cleanup: () => { | ||
jasmine.clock().uninstall() | ||
resetNavigationStart() | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function setNavigatorOnLine(onLine: boolean) { | ||
Object.defineProperty(navigator, 'onLine', { | ||
get() { | ||
return onLine | ||
}, | ||
configurable: true, | ||
}) | ||
} | ||
|
||
export function restoreNavigatorOnLine() { | ||
delete (navigator as any).onLine | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/core/test/stubZoneJs.ts → packages/core/test/emulate/stubZoneJs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
packages/core/test/syntheticsWorkerValues.ts → ...re/test/emulate/syntheticsWorkerValues.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function setPageVisibility(visibility: 'visible' | 'hidden') { | ||
Object.defineProperty(document, 'visibilityState', { | ||
get() { | ||
return visibility | ||
}, | ||
configurable: true, | ||
}) | ||
} | ||
|
||
export function restorePageVisibility() { | ||
delete (document as any).visibilityState | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { instrumentMethod, noop } from '../../src' | ||
|
||
/** | ||
* Opt out of jasmine uncaught error interception during test. This is useful for tests that are | ||
* instrumenting `window.onerror`. See https://github.com/jasmine/jasmine/pull/1860 for more | ||
* information. | ||
*/ | ||
export function disableJasmineUncaughtErrorHandler() { | ||
const { stop } = instrumentMethod(window, 'onerror', () => noop) | ||
return { | ||
reset: stop, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
export * from './capturedExceptions' | ||
export * from './browserChecks' | ||
export * from './buildEnv' | ||
export * from './collectAsyncCalls' | ||
export * from './stubReportApis' | ||
export * from './stubZoneJs' | ||
export * from './syntheticsWorkerValues' | ||
export * from './specHelper' | ||
export * from './requests' | ||
export * from './emulate/createNewEvent' | ||
export * from './emulate/location' | ||
export * from './emulate/mockClock' | ||
export * from './emulate/stubReportApis' | ||
export * from './emulate/stubZoneJs' | ||
export * from './emulate/syntheticsWorkerValues' | ||
export * from './emulate/visibilityState' | ||
export * from './emulate/navigatorOnLine' | ||
export * from './emulate/navigatorOnLine' | ||
export * from './emulate/eventBridge' | ||
export * from './emulate/eventBridge' | ||
export * from './emulate/windowOnError' | ||
export * from './emulate/cookie' |
Oops, something went wrong.