Skip to content

Commit

Permalink
πŸ‘ŒπŸšš move stubZoneJs in its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Dec 9, 2022
1 parent 669b1ec commit 25b3365
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/browser/addEventListener.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stubZoneJs } from '../../test/specHelper'
import { stubZoneJs } from '../../test/stubZoneJs'
import { noop } from '../tools/utils'

import { addEventListener, DOM_EVENT } from './addEventListener'
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/tools/getZoneJsOriginalValue.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stubZoneJs } from '../../test/specHelper'
import { stubZoneJs } from '../../test/stubZoneJs'

import { getZoneJsOriginalValue } from './getZoneJsOriginalValue'
import { noop } from './utils'
Expand Down
29 changes: 0 additions & 29 deletions packages/core/test/specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { resetNavigationStart } from '../src/tools/timeUtils'
import { buildUrl } from '../src/tools/urlPolyfill'
import { noop, objectEntries, assign } from '../src/tools/utils'
import type { BrowserWindowWithEventBridge } from '../src/transport'
import type { BrowserWindowWithZoneJs } from '../src/tools/getZoneJsOriginalValue'

// to simulate different build env behavior
export interface BuildEnvWindow {
Expand Down Expand Up @@ -486,31 +485,3 @@ export function interceptRequests() {
},
}
}

export function stubZoneJs() {
const browserWindow = window as BrowserWindowWithZoneJs
const restorers: Array<() => void> = []

function getSymbol(name: string) {
return `__zone_symbol__${name}`
}

browserWindow.Zone = { __symbol__: getSymbol }

return {
restore: () => {
delete browserWindow.Zone
restorers.forEach((restorer) => restorer())
},
getSymbol,
replaceProperty<Target, Name extends keyof Target & string>(target: Target, name: Name, replacement: Target[Name]) {
const original = target[name]
target[name] = replacement
;(target as any)[getSymbol(name)] = original
restorers.push(() => {
delete (target as any)[getSymbol(name)]
target[name] = original
})
},
}
}
29 changes: 29 additions & 0 deletions packages/core/test/stubZoneJs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { BrowserWindowWithZoneJs } from '../src/tools/getZoneJsOriginalValue'

export function stubZoneJs() {
const browserWindow = window as BrowserWindowWithZoneJs
const restorers: Array<() => void> = []

function getSymbol(name: string) {
return `__zone_symbol__${name}`
}

browserWindow.Zone = { __symbol__: getSymbol }

return {
restore: () => {
delete browserWindow.Zone
restorers.forEach((restorer) => restorer())
},
getSymbol,
replaceProperty<Target, Name extends keyof Target & string>(target: Target, name: Name, replacement: Target[Name]) {
const original = target[name]
target[name] = replacement
;(target as any)[getSymbol(name)] = original
restorers.push(() => {
delete (target as any)[getSymbol(name)]
target[name] = original
})
},
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isIE } from '@datadog/browser-core'
import { stubZoneJs } from '../../../core/test/specHelper'
import { stubZoneJs } from '../../../core/test/stubZoneJs'
import { createDOMMutationObservable, getMutationObserverConstructor } from './domMutationObservable'

// The MutationObserver invokes its callback in an event loop microtask, making this asynchronous.
Expand Down

0 comments on commit 25b3365

Please sign in to comment.