From 632a6d628bfb1d21057ab0732733be8665c94430 Mon Sep 17 00:00:00 2001 From: Rares Preda Date: Fri, 16 Aug 2024 16:10:05 +0300 Subject: [PATCH] fix unit tests --- test/spec/modules/connatixBidAdapter_spec.js | 59 ++++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/test/spec/modules/connatixBidAdapter_spec.js b/test/spec/modules/connatixBidAdapter_spec.js index 2c2c4aa17c3..47a8227e78d 100644 --- a/test/spec/modules/connatixBidAdapter_spec.js +++ b/test/spec/modules/connatixBidAdapter_spec.js @@ -224,7 +224,7 @@ describe('connatixBidAdapter', function () { getElementByIdStub.restore(); }); - it('should return viewability percentage when element is found with valid viewabilityContainerIdentifier', () => { + it('should return 100% viewability when the element is fully within view and has a valid viewabilityContainerIdentifier', () => { const bid = { params: { viewabilityContainerIdentifier: '#validElement' }, adUnitCode: 'adUnitCode123', @@ -250,7 +250,32 @@ describe('connatixBidAdapter', function () { expect(result).to.equal(100); }); - it('should fall back to bid sizes if viewabilityContainerIdentifier is invalid', () => { + it('should return 48% viewability when the element is partially in view (48%) and has a valid viewabilityContainerIdentifier', () => { + const bid = { + params: { viewabilityContainerIdentifier: '#validElement' }, + adUnitCode: 'adUnitCode123', + mediaTypes: { video: { sizes: [[300, 250]] } }, + }; + + getBoundingClientRectStub.returns({ + top: 100, // 100 pixels from the top of the viewport + left: 400, // 400 pixels from the left of the viewport (halfway in horizontal direction) + bottom: 800, // 800 pixels from the top (extends 200 pixels outside the bottom) + right: 1000, // 1000 pixels from the left (extends 200 pixels outside the right) + width: 600, // Width of the rect is 600 pixels + height: 700, // Height of the rect is 700 pixels (only 300 pixels inside the viewport) + }); + + querySelectorStub.withArgs('#validElement').returns(element); + getElementByIdStub.returns(null); + + const result = connatixDetectViewability(bid); + + // Expected calculation: the element is partially in view, so 48% viewability + expect(result).to.equal(48); + }); + + it('should fall back to using bid sizes and adUnitCode when the viewabilityContainerIdentifier is invalid or was not provided', () => { const bid = { params: { viewabilityContainerIdentifier: '#invalidElement' }, adUnitCode: 'adUnitCode123', @@ -275,7 +300,7 @@ describe('connatixBidAdapter', function () { expect(result).to.equal(100); // Full viewability }); - it('should return null if element is not viewable', () => { + it('should return 0% viewability when the element is completely outside the viewable area', () => { const bid = { params: { viewabilityContainerIdentifier: '#someElement' }, adUnitCode: 'adUnitCode123', @@ -296,10 +321,10 @@ describe('connatixBidAdapter', function () { const result = connatixDetectViewability(bid); - expect(result).to.equal(0); // 0% viewability because the element is out of the viewport + expect(result).to.equal(0); }); - it('should handle error when trying to query element and fall back', () => { + it('should use the adUnitCode as a fallback when querying an element fails due to a browser error, and return 100% viewability because adUnitCode container is fully in view', () => { const bid = { params: { viewabilityContainerIdentifier: '#invalidElement' }, adUnitCode: 'adUnitCode123', @@ -325,6 +350,30 @@ describe('connatixBidAdapter', function () { expect(result).to.equal(100); // Expect the fallback to work and return 100% viewability }); + + it('should return null when querying the element by the provided identifier fails and the adUnitCode viewability container is unavailable', () => { + const bid = { + params: { viewabilityContainerIdentifier: '#invalidElement' }, + adUnitCode: 'adUnitCode123', + sizes: [[300, 250]] + }; + + // Simulate an error when querying the element + querySelectorStub.withArgs('#invalidElement').throws(new Error('Query failed')); + + getBoundingClientRectStub.returns({ + left: 100, + top: 100, + right: 400, + bottom: 350, + width: 300, + height: 250 + }); + + const result = connatixDetectViewability(bid); + + expect(result).to.equal(null); + }); }); describe('isBidRequestValid', function () {