Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #31: Add a check if page is scrolled down. #32

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/BeaconManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class BeaconManager {
return;
}

if (BeaconUtils.isPageScrolled()) {
this.logger.logMessage('Bailing out because the page has been scrolled');
this._finalize();
return;
}

this.infiniteLoopId = setTimeout(() => {
this._handleInfiniteLoop();
}, 10000);
Expand Down
4 changes: 4 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class BeaconUtils {
);
}

static isPageScrolled() {
return window.pageYOffset > 0 || document.documentElement.scrollTop > 0;
}

}

export default BeaconUtils;
56 changes: 55 additions & 1 deletion test/BeaconManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ describe('BeaconManager', function() {
let beacon;
const config = { nonce: 'test', url: 'http://example.com', is_mobile: false, status: {atf: true}, width_threshold: 100, height_threshold: 100 };
beforeEach(function() {
//Deep copy of config
// Deep copy of config
beacon = new BeaconManager(JSON.parse(JSON.stringify(config)));

// Mock window and document objects
global.window = {
pageYOffset: 0
};
global.document = {
documentElement: {
scrollTop: 0
},
querySelector: sinon.stub().returns({
setAttribute: sinon.spy()
})
};
});

describe('#constructor()', function() {
Expand Down Expand Up @@ -109,6 +122,47 @@ describe('BeaconManager', function() {
});
});

describe('#init()', function() {
let fetchStub;
let _isValidPreconditionsStub;
let isPageCachedStub;
let _finalizeStub;

beforeEach(function() {
// Stub the global fetch method
_isValidPreconditionsStub = sinon.stub(beacon, '_isValidPreconditions');
isPageCachedStub = sinon.stub(BeaconUtils, 'isPageCached');
_finalizeStub = sinon.stub(beacon, '_finalize');
fetchStub = sinon.stub(global, 'fetch').resolves({
json: () => Promise.resolve({ data: false })
});
});

afterEach(function() {
// Restore the original fetch method
_isValidPreconditionsStub.restore();
isPageCachedStub.restore();
_finalizeStub.restore();
fetchStub.restore();
});

it('should bail out if the page is scrolled', async function() {
// Mock _isValidPreconditions
_isValidPreconditionsStub.resolves(true);
isPageCachedStub.returns(false);

// Simulate page being scrolled
global.window.pageYOffset = 100;
global.document.documentElement.scrollTop = 100;

await beacon.init();

assert.strictEqual(_isValidPreconditionsStub.calledOnce, true);
assert.strictEqual(_finalizeStub.calledOnce, true);
assert.strictEqual(fetchStub.notCalled, true);
});
});

describe('#_isValidPreconditions()', function() {
it('should return true for desktop screensize larger than threshold', async function() {
// Mocking window properties and methods since they are used in _isValidPreconditions
Expand Down
Loading