-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create global.js consent script where withOneTrust is stored in globa…
…l scope (#1136)
- Loading branch information
Showing
8 changed files
with
115 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@segment/analytics-consent-wrapper-onetrust': patch | ||
--- | ||
|
||
Fix consent namespace issue |
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
31 changes: 31 additions & 0 deletions
31
packages/consent/consent-wrapper-onetrust/bundle-build-tests/global.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,31 @@ | ||
import { JSDOM } from 'jsdom' | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
|
||
describe('Global Scope Test', () => { | ||
let dom: JSDOM | ||
beforeAll(() => { | ||
// Load the built file | ||
const filePath = path.resolve( | ||
__dirname, | ||
'../dist/umd/analytics-onetrust.global.js' | ||
) | ||
const scriptContent = fs.readFileSync(filePath, 'utf-8') | ||
|
||
// Create a new JSDOM instance | ||
dom = new JSDOM(`<!DOCTYPE html><html><head></head><body></body></html>`, { | ||
runScripts: 'dangerously', | ||
resources: 'usable', | ||
}) | ||
|
||
// Execute the script in the JSDOM context | ||
const scriptElement = dom.window.document.createElement('script') | ||
scriptElement.textContent = scriptContent | ||
dom.window.document.head.appendChild(scriptElement) | ||
}) | ||
|
||
test('should expose withOneTrust in the global scope', () => { | ||
expect(dom.window.withOneTrust).toBeDefined() | ||
expect(typeof dom.window.withOneTrust).toBe('function') | ||
}) | ||
}) |
55 changes: 55 additions & 0 deletions
55
packages/consent/consent-wrapper-onetrust/bundle-build-tests/umd.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,55 @@ | ||
import { JSDOM } from 'jsdom' | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
|
||
describe('UMD Scope Test', () => { | ||
let dom: JSDOM | ||
let scriptContent: string | ||
|
||
beforeAll(() => { | ||
// Load the built file | ||
const filePath = path.resolve( | ||
__dirname, | ||
'../dist/umd/analytics-onetrust.umd.js' | ||
) | ||
scriptContent = fs.readFileSync(filePath, 'utf-8') | ||
|
||
// Create a new JSDOM instance | ||
dom = new JSDOM(`<!DOCTYPE html><html><head></head><body></body></html>`, { | ||
runScripts: 'dangerously', | ||
resources: 'usable', | ||
}) | ||
|
||
// Execute the script in the JSDOM context | ||
const scriptElement = dom.window.document.createElement('script') | ||
scriptElement.textContent = scriptContent | ||
dom.window.document.head.appendChild(scriptElement) | ||
}) | ||
|
||
test('should expose withOneTrust in the global scope', () => { | ||
expect(dom.window.withOneTrust).toBeDefined() | ||
expect(typeof dom.window.withOneTrust).toBe('function') | ||
}) | ||
|
||
test('should build a proper UMD module', () => { | ||
// Check if the module is available as a global variable | ||
expect(dom.window.AnalyticsOneTrust).toBeDefined() | ||
expect(typeof dom.window.AnalyticsOneTrust).toBe('object') | ||
expect(typeof dom.window.AnalyticsOneTrust.withOneTrust).toBe('function') | ||
|
||
// Simulate a CommonJS environment | ||
const module = { exports: {} } | ||
const evalRequire = (id: string): { withOneTrust: any } => { | ||
if (id === 'analytics-onetrust') { | ||
eval(scriptContent) | ||
return module.exports as any | ||
} | ||
throw new Error(`Cannot find module '${id}'`) | ||
} | ||
|
||
const commonJsModule = evalRequire('analytics-onetrust') | ||
expect(commonJsModule).toBeDefined() | ||
expect(commonJsModule.withOneTrust).toBeDefined() | ||
expect(typeof commonJsModule.withOneTrust).toBe('function') | ||
}) | ||
}) |
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,4 +1,9 @@ | ||
const fetch = require('node-fetch') | ||
const { TextEncoder, TextDecoder } = require('util') | ||
|
||
// fix: "ReferenceError: TextEncoder is not defined" after upgrading JSDOM | ||
global.TextEncoder = TextEncoder | ||
global.TextDecoder = TextDecoder | ||
|
||
// eslint-disable-next-line no-undef | ||
globalThis.fetch = fetch // polyfill fetch so nock will work correctly on node 18 (https://github.com/nock/nock/issues/2336) |
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
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