diff --git a/extensions/brandstory/brandstory_scrolldepth.js b/extensions/brandstory/brandstory_scrolldepth.js index c21b0ac5..db4c6952 100644 --- a/extensions/brandstory/brandstory_scrolldepth.js +++ b/extensions/brandstory/brandstory_scrolldepth.js @@ -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 () { @@ -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 +}; + +// Evaluate runtime environment (Browser or Node.js) +if (typeof exports === 'object') { + // Expose reference to members for unit testing. + module.exports = exportedFunctions; +} diff --git a/tests/brandstory/brsndstory_scrolldepth.test.js b/tests/brandstory/brsndstory_scrolldepth.test.js new file mode 100644 index 00000000..93452791 --- /dev/null +++ b/tests/brandstory/brsndstory_scrolldepth.test.js @@ -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(); + }); +}); +