Skip to content

Commit

Permalink
Merge pull request #238 from spring-media/TRAC-2100_brandstory_scroll…
Browse files Browse the repository at this point in the history
…depth

TRAC-2100: Brandstory scrolldepth tests added
  • Loading branch information
mehakraza authored Nov 13, 2024
2 parents 539dae4 + 6555b35 commit 9c0f26e
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
17 changes: 16 additions & 1 deletion extensions/brandstory/brandstory_scrolldepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const scrollArray = [];

/* If scroll depth is 50, 75 or 100 the request should be triggered once
for each number. To prevent multiple requests for each, we set trigger flags */
var triggered50, triggered75, triggered100 = false;
var triggered50 = false;
var triggered75 = false;
var triggered100 = false;

// Scroll event listener
window.addEventListener('scroll', function () {
Expand Down Expand Up @@ -82,3 +84,16 @@ window.addEventListener('scroll', function () {
//sendLinkEvent(scrollDepth, window.utag.data.page_platform, window.utag.data.adobe_pageName, tagNumber);
}
});

// Create a reference to members of this unit which need to be exposed for unit testing.
const exportedFunctions = {
getCookie,
getDomainTagValue,
sendLinkEvent

Check failure on line 92 in extensions/brandstory/brandstory_scrolldepth.js

View workflow job for this annotation

GitHub Actions / Run Unit Tests

'sendLinkEvent' is not defined
};

// Evaluate runtime environment (Browser or Node.js)
if (typeof exports === 'object') {
// Expose reference to members for unit testing.
module.exports = exportedFunctions;
}
129 changes: 129 additions & 0 deletions tests/brandstory/brsndstory_scrolldepth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
const { getCookie, getDomainTagValue, sendLinkEvent } = require('../../extensions/brandstory/brandstory_scrolldepth');

beforeEach(() => {
// Mock document.cookie
Object.defineProperty(document, 'cookie', {
value: '',
writable: true,
});

// Mock window.location.hostname
jest.spyOn(window, 'location', 'get').mockReturnValue({
hostname: ''
});

// Mock window.utag.link
if (!window.utag) {
window.utag = {};
}
window.utag.link = jest.fn();
});

afterEach(() => {
jest.restoreAllMocks();
});


describe('getCookie', () => {

test('returns the value from the cookie', () => {
document.cookie = 'testCookie=someValue,value2';
expect(getCookie('testCookie')).toBe('value2');
});

test('returns an empty string if the cookie is not found', () => {
document.cookie = 'anotherCookie=value';
expect(getCookie('testCookie')).toBe('');
});
});

describe('getDomainTagValue', () => {
test('returns [206] for welt.de domain', () => {
expect(getDomainTagValue('www.welt.de')).toEqual([206]);
});

test('returns [10] for bild.de domain', () => {
expect(getDomainTagValue('www.bild.de')).toEqual([10]);
});

test('returns [] for other domains', () => {
expect(getDomainTagValue('www.example.com')).toEqual([]);
});
});

describe('sendLinkEvent', () => {

test('sends link event with correct parameters', () => {
const scrollDepth = 50;
const platform = 'web';
const pageName = 'homepage';
const tagNumber = [10];

sendLinkEvent(scrollDepth, platform, pageName, tagNumber);

expect(window.utag.link).toHaveBeenCalledWith({
'event_name': 'scroll depth',
'event_action': 'view50',
'page_platform': platform,
'adobe_pageName': pageName
}, null, tagNumber);
});
});

describe('scroll event listener', () => {
let scrollArray;
let triggered50;
let triggered75;
let triggered100;

beforeEach(() => {
jest.clearAllMocks();

window.utag.data = {
page_platform: 'web',
adobe_pageName: 'homepage'
};
});

const triggerScroll = (scrollDepth, hostname) => {
document.cookie = `s_ppv=test,${scrollDepth}`;

window.location.hostname = hostname;

// Simulate scroll event
const event = new Event('scroll');
window.dispatchEvent(event);
};

test('triggers event at 50% scroll depth', () => {
triggerScroll(50, 'www.welt.de');

expect(window.utag.link).toHaveBeenCalled();
});

test('triggers event at 75% scroll depth', () => {
triggerScroll(75, 'www.bild.de');

expect(window.utag.link).toHaveBeenCalled();
});

test('triggers event at 100% scroll depth', () => {
triggerScroll(100, 'www.example.com');

expect(window.utag.link).toHaveBeenCalled();
});

test('does not trigger event if scroll depth is not 50, 75, or 100', () => {
triggerScroll(60, 'www.welt.de');

expect(window.utag.link).not.toHaveBeenCalled();
});

test('does not trigger event if already triggered', () => {
triggered50 = true;
triggerScroll(50, 'www.welt.de');

expect(window.utag.link).not.toHaveBeenCalled();
});
});

0 comments on commit 9c0f26e

Please sign in to comment.