Skip to content

Commit

Permalink
Fix ResizeObserver initialization if document.body does not exist yet (
Browse files Browse the repository at this point in the history
…close #1311)

PR #1322
  • Loading branch information
EricHeitmuller-Gusto authored and matus-tomlein committed Jul 2, 2024
1 parent bea8358 commit ddbf17a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-tracker-core",
"comment": "Fix ResizeObserver initialization if document.body does not exist yet (#1311)",
"type": "none"
}
],
"packageName": "@snowplow/browser-tracker-core"
}
3 changes: 3 additions & 0 deletions libraries/browser-tracker-core/src/helpers/browser_props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function initializeResizeObserver() {
if (resizeObserverInitialized) {
return;
}
if(!document || !document.body || !document.documentElement) {
return;
}
resizeObserverInitialized = true;

const resizeObserver = new ResizeObserver((entries) => {
Expand Down
16 changes: 15 additions & 1 deletion libraries/browser-tracker-core/test/browser_props.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { floorDimensionFields } from '../src/helpers/browser_props';
import { floorDimensionFields, getBrowserProperties } from '../src/helpers/browser_props';

describe('Browser props', () => {
it('floorDimensionFields correctly floors dimension type values', () => {
Expand All @@ -10,4 +10,18 @@ describe('Browser props', () => {
const testFractionalDimensions = '100.2x100.1';
expect(floorDimensionFields(testFractionalDimensions)).toEqual('100x100');
});

describe('#getBrowserProperties', () => {
describe('with undefined document', () => {
beforeAll(() => {
// @ts-expect-error
document = undefined;
});

it('does not invoke the resize observer if the document is null', () => {
const browserProperties = getBrowserProperties();
expect(browserProperties).not.toEqual(null);
});
});
});
});

0 comments on commit ddbf17a

Please sign in to comment.