forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
366ca7f
commit 3784955
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}); | ||
}); |