Skip to content

Commit 32222bd

Browse files
committed
fix tests
1 parent 8a6ce18 commit 32222bd

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

packages/signals/signals/src/core/processor/__tests__/sandbox-settings.test.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,30 @@ describe(IframeSandboxSettings, () => {
1919
expect(await sandboxSettings.processSignal).toEqual(edgeFnResponseBody)
2020
})
2121

22-
test('normalizes edgeFnDownloadURL when functionHost is provided', async () => {
23-
const settings = {
22+
test('should call edgeFnDownloadURL', async () => {
23+
const settings: IframeSandboxSettingsConfig = {
2424
...baseSettings,
2525
processSignal: undefined,
26-
functionHost: 'newHost.com',
27-
edgeFnDownloadURL: 'https://original.com/download',
26+
edgeFnDownloadURL: 'https://foo.com/download',
2827
}
2928
new IframeSandboxSettings(settings)
3029
expect(baseSettings.edgeFnFetchClient).toHaveBeenCalledWith(
31-
'https://newHost.com/download'
30+
'https://foo.com/download'
3231
)
3332
})
3433

3534
test('creates default processSignal when parameters are missing', async () => {
3635
const consoleWarnSpy = jest
3736
.spyOn(console, 'warn')
3837
.mockImplementation(() => {})
39-
const settings = {
38+
const settings: IframeSandboxSettingsConfig = {
4039
...baseSettings,
4140
processSignal: undefined,
4241
edgeFnDownloadURL: undefined,
4342
}
4443
const sandboxSettings = new IframeSandboxSettings(settings)
45-
expect(await sandboxSettings.processSignal).toEqual(
46-
'globalThis.processSignal = function processSignal() {}'
44+
expect(await sandboxSettings.processSignal).toMatchInlineSnapshot(
45+
`"globalThis.processSignal = function() {}"`
4746
)
4847
expect(baseSettings.edgeFnFetchClient).not.toHaveBeenCalled()
4948
expect(consoleWarnSpy).toHaveBeenCalledWith(

packages/signals/signals/src/core/processor/sandbox.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ export type IframeSandboxSettingsConfig = Pick<
180180
'processSignal' | 'edgeFnFetchClient' | 'edgeFnDownloadURL'
181181
>
182182

183+
const consoleWarnProcessSignal = () =>
184+
console.warn(
185+
'processSignal is not defined - have you set up auto-instrumentation on app.segment.com?'
186+
)
187+
183188
export class IframeSandboxSettings {
184189
/**
185190
* Should look like:
@@ -193,11 +198,21 @@ export class IframeSandboxSettings {
193198
constructor(settings: IframeSandboxSettingsConfig) {
194199
const fetch = settings.edgeFnFetchClient ?? globalThis.fetch
195200

196-
const processSignalNormalized = settings.processSignal
197-
? Promise.resolve(settings.processSignal).then(
198-
(str) => `globalThis.processSignal = ${str}`
199-
)
200-
: fetch(settings.edgeFnDownloadURL!).then((res) => res.text())
201+
let processSignalNormalized = Promise.resolve(
202+
`globalThis.processSignal = function() {}`
203+
)
204+
205+
if (settings.processSignal) {
206+
processSignalNormalized = Promise.resolve(settings.processSignal).then(
207+
(str) => `globalThis.processSignal = ${str}`
208+
)
209+
} else if (settings.edgeFnDownloadURL) {
210+
processSignalNormalized = fetch(settings.edgeFnDownloadURL!).then((res) =>
211+
res.text()
212+
)
213+
} else {
214+
consoleWarnProcessSignal()
215+
}
201216

202217
this.processSignal = processSignalNormalized
203218
}
@@ -258,7 +273,7 @@ const processWithGlobalScopeExecutionEnv = (
258273
const processSignal: ProcessSignal = g['processSignal']
259274

260275
if (typeof processSignal == 'undefined') {
261-
console.warn('no processSignal function is defined in the global scope')
276+
consoleWarnProcessSignal()
262277
return undefined
263278
}
264279

0 commit comments

Comments
 (0)