-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(analytics-client-common): adding ampIntegrationsContext to getGlo…
…balScope (#783)
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
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,3 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
// eslint-disable-next-line no-var | ||
export var ampIntegrationContext: typeof globalThis; |
45 changes: 45 additions & 0 deletions
45
packages/analytics-client-common/test/global-scope.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
import { getGlobalScope } from '../src/global-scope'; | ||
|
||
describe('getGlobalScope', () => { | ||
let originalGlobalThis: any; | ||
|
||
beforeEach(() => { | ||
originalGlobalThis = globalThis; | ||
}); | ||
|
||
afterEach(() => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
globalThis = originalGlobalThis; | ||
delete (globalThis as any).ampIntegrationContext; | ||
}); | ||
|
||
test('returns ampIntegrationContext if it exists', () => { | ||
(globalThis as any).ampIntegrationContext = { someKey: 'someValue' }; | ||
expect(getGlobalScope()).toBe((globalThis as any).ampIntegrationContext); | ||
}); | ||
|
||
test('returns globalThis if ampIntegrationContext does not exist', () => { | ||
const scope = getGlobalScope(); | ||
// Need to use Object.is because expect(scope).toBe(globalThis) will throw an error | ||
expect(Object.is(scope, globalThis)).toBeTruthy(); | ||
}); | ||
|
||
test('should return window if globalThis is undefined and window is defined', () => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
globalThis = undefined; | ||
|
||
const scope = getGlobalScope(); | ||
|
||
// Note: We NEED to reassign globalThis to its original state because the jest expect function requires it | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
globalThis = originalGlobalThis; | ||
// Need to use Object.is because expect(scope).toBe(globalThis) will throw an error | ||
expect(Object.is(scope, window)).toBeTruthy(); | ||
}); | ||
}); |