Skip to content

TRAC-2152: Extension tests #246

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions extensions/brandstory/brandstory_scrolldepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ window.addEventListener('scroll', function () {
//sendLinkEvent(scrollDepth, window.utag.data.page_platform, window.utag.data.adobe_pageName, tagNumber);
}
});

const exportedFunctions = {
getCookie,
getDomainTagValue,
};

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

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

describe('getCookie', () => {
it('should return the correct value when the cookie exists', () => {
document.cookie = 's_ppv=xyz,50';
expect(getCookie('s_ppv')).toBe('50');
});

it('should return an empty string if the cookie does not exist', () => {
document.cookie = 'another_cookie=abc,123';
expect(getCookie('s_ppv')).toBe('');
});

it('should decode the cookie value correctly', () => {
document.cookie = 's_ppv=abc%20def,75';
expect(getCookie('s_ppv')).toBe('75');
});
});

describe('getDomainTagValue', () => {
it('should return [206] for "welt.de" domain', () => {
expect(getDomainTagValue('www.welt.de')).toEqual([206]);
});

it('should return [10] for "bild.de" domain', () => {
expect(getDomainTagValue('www.bild.de')).toEqual([10]);
});

it('should return [31] for "fitbook.de" domain', () => {
expect(getDomainTagValue('www.fitbook.de')).toEqual([31]);
});

it('should return [31] for "magazine-fitbook.com" domain', () => {
expect(getDomainTagValue('www.magazine-fitbook.com')).toEqual([31]);
});

it('should return [79] for "petbook.de" domain', () => {
expect(getDomainTagValue('www.petbook.de')).toEqual([79]);
});

it('should return an empty array for an unknown domain', () => {
expect(getDomainTagValue('unknown.com')).toEqual([]);
});
});

describe('Scroll event listener', () => {
let addEventListenerSpy;
let mockUtag;

beforeEach(() => {
// window.utag mock
mockUtag = {
link: jest.fn(),
data: {
page_platform: 'desktop',
adobe_pageName: 'home',
},
};
global.window.utag = mockUtag;

// window.location.hostname mock
Object.defineProperty(window, 'location', {
value: {
hostname: 'www.welt.de',
},
writable: true,
});

// addEventListener mock
addEventListenerSpy = jest.spyOn(window, 'addEventListener');

// cookie values mock
document.cookie = 's_ppv=xyz,50';

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

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

it('should trigger the "scroll depth" event at 50% scroll depth', () => {
expect(mockUtag.link).toHaveBeenCalledWith(
expect.objectContaining({
event_name: 'scroll depth',
event_action: 'view50',
}),
null,
[206]
);
});
});
Loading