Skip to content

Commit

Permalink
add test on news utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-greenbids committed Nov 28, 2024
1 parent 366ca7f commit 3784955
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
102 changes: 102 additions & 0 deletions test/spec/libraries/pageInfosUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { getReferrerInfo, getPageTitle, getPageDescription, getConnectionDownLink } from './pageInfosUtils';

describe('pageInfosUtils', () => {
describe('getReferrerInfo', () => {
it('should return the referrer URL if available', () => {
const bidderRequest = {
refererInfo: {
page: 'http://example.com'
}
};
const result = getReferrerInfo(bidderRequest);
expect(result).to.equal('http://example.com');
});

it('should return an empty string if referrer URL is not available', () => {
const bidderRequest = {};
const result = getReferrerInfo(bidderRequest);
expect(result).to.equal('');
});
});

describe('getPageTitle', () => {
let topDocumentStub, documentStub;

beforeEach(() => {
topDocumentStub = sinon.stub(window.top, 'document').value({
title: 'Top Document Title',
querySelector: sinon.stub().returns(null)
});
documentStub = sinon.stub(document, 'querySelector').returns(null);
});

afterEach(() => {
topDocumentStub.restore();
documentStub.restore();
});

it('should return the title from the top-level document', () => {
const result = getPageTitle();
expect(result).to.equal('Top Document Title');
});

it('should return the title from the current document if top-level document access fails', () => {
topDocumentStub.value({
title: '',
querySelector: sinon.stub().throws(new Error('Cross-origin restriction'))
});
documentStub.returns({ content: 'Current Document Title' });
const result = getPageTitle();
expect(result).to.equal('Current Document Title');
});
});

describe('getPageDescription', () => {
let topDocumentStub, documentStub;

beforeEach(() => {
topDocumentStub = sinon.stub(window.top, 'document').value({
querySelector: sinon.stub().returns(null)
});
documentStub = sinon.stub(document, 'querySelector').returns(null);
});

afterEach(() => {
topDocumentStub.restore();
documentStub.restore();
});

it('should return the description from the top-level document', () => {
topDocumentStub.querySelector.withArgs('meta[name="description"]').returns({ content: 'Top Document Description' });
const result = getPageDescription();
expect(result).to.equal('Top Document Description');
});

it('should return the description from the current document if top-level document access fails', () => {
topDocumentStub.querySelector.throws(new Error('Cross-origin restriction'));
documentStub.withArgs('meta[name="description"]').returns({ content: 'Current Document Description' });
const result = getPageDescription();
expect(result).to.equal('Current Document Description');
});
});

describe('getConnectionDownLink', () => {
it('should return the downlink speed if available', () => {
const nav = {
connection: {
downlink: 10
}
};
const result = getConnectionDownLink(nav);
expect(result).to.equal('10');
});

it('should return an empty string if downlink speed is not available', () => {
const nav = {};
const result = getConnectionDownLink(nav);
expect(result).to.equal('');
});
});
});
54 changes: 54 additions & 0 deletions test/spec/libraries/timeToFirstBytesUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { getTimeToFirstByte } from './timeToFirstBytesUtils';

describe('getTimeToFirstByte', () => {
let win;

beforeEach(() => {
win = {
performance: {
getEntriesByType: sinon.stub(),
timing: {
responseStart: 0,
requestStart: 0
}
}
};
});

it('should return TTFB using Navigation Timing Level 2 API', () => {
win.performance.getEntriesByType.withArgs('navigation').returns([{
responseStart: 100,
requestStart: 50
}]);

const ttfb = getTimeToFirstByte(win);
expect(ttfb).to.equal('50');
});

it('should return TTFB using Navigation Timing Level 1 API', () => {
win.performance.getEntriesByType.returns([]);
win.performance.timing.responseStart = 100;
win.performance.timing.requestStart = 50;

const ttfb = getTimeToFirstByte(win);
expect(ttfb).to.equal('50');
});

it('should return an empty string if TTFB cannot be determined', () => {
win.performance.getEntriesByType.returns([]);
win.performance.timing.responseStart = 0;
win.performance.timing.requestStart = 0;

const ttfb = getTimeToFirstByte(win);
expect(ttfb).to.equal('');
});

it('should return an empty string if performance object is not available', () => {
win.performance = null;

const ttfb = getTimeToFirstByte(win);
expect(ttfb).to.equal('');
});
});

0 comments on commit 3784955

Please sign in to comment.