Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rares-mihai-preda committed Aug 16, 2024
1 parent 8945d8f commit 632a6d6
Showing 1 changed file with 54 additions and 5 deletions.
59 changes: 54 additions & 5 deletions test/spec/modules/connatixBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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 () {
Expand Down

0 comments on commit 632a6d6

Please sign in to comment.