-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to disable
performanceNavigationTiming
plugin
- Loading branch information
Showing
3 changed files
with
65 additions
and
8 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
61 changes: 61 additions & 0 deletions
61
trackers/javascript-tracker/test/unit/plugin_features.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,61 @@ | ||
import { SelfDescribingJson } from '@snowplow/tracker-core'; | ||
import { Plugins } from '../../src/features'; | ||
|
||
describe('Performance Navigation Timing', () => { | ||
let windowSpy: any; | ||
|
||
const otherContexts = { | ||
webPage: false, | ||
session: false, | ||
performanceTiming: false, | ||
gaCookies: false, | ||
geolocation: false, | ||
clientHints: false, | ||
webVitals: false, | ||
}; | ||
|
||
const hasPerformanceNavigationTimingContext = (plugins: ReturnType<typeof Plugins>): boolean => { | ||
const pluginContexts = plugins.map((plugin) => plugin[0]?.contexts?.()); | ||
const hasPerformanceContext = pluginContexts.some((contexts?: SelfDescribingJson[]) => | ||
contexts?.some( | ||
(context: { schema?: string }) => context.schema === 'iglu:org.w3/PerformanceNavigationTiming/jsonschema/1-0-0' | ||
) | ||
); | ||
return hasPerformanceContext; | ||
}; | ||
|
||
beforeEach(() => { | ||
windowSpy = jest.spyOn(global, 'window', 'get'); | ||
|
||
// The PerformanceNavigationTiming context will only be added if the plugin can: | ||
// - Access the `performance` object on the window | ||
// - See that a value is returned from `getEntriesByType` | ||
windowSpy.mockImplementation(() => ({ | ||
performance: { | ||
getEntriesByType: () => [{}], | ||
}, | ||
})); | ||
}); | ||
|
||
it('Is enabled if contexts.performanceNavigationTiming is true', () => { | ||
const plugins = Plugins({ | ||
contexts: { | ||
performanceNavigationTiming: true, | ||
...otherContexts, | ||
}, | ||
}); | ||
|
||
expect(hasPerformanceNavigationTimingContext(plugins)).toBe(true); | ||
}); | ||
|
||
it('Is disabled if contexts.performanceNavigationTiming is false', () => { | ||
const plugins = Plugins({ | ||
contexts: { | ||
performanceNavigationTiming: false, | ||
...otherContexts, | ||
}, | ||
}); | ||
|
||
expect(hasPerformanceNavigationTimingContext(plugins)).toBe(false); | ||
}); | ||
}); |