forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not load external js if renderer defined on adUnit (prebid#3284)
* do not load external js if renderer defined on adUnit * Added unit test
- Loading branch information
1 parent
82c2b0a
commit 1dfe02f
Showing
4 changed files
with
152 additions
and
79 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
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
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
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 |
---|---|---|
@@ -1,96 +1,131 @@ | ||
import { expect } from 'chai'; | ||
import { Renderer } from 'src/Renderer'; | ||
|
||
describe('Renderer: A renderer installed on a bid response', function () { | ||
let testRenderer1; | ||
let testRenderer2; | ||
let spyRenderFn; | ||
let spyEventHandler; | ||
|
||
beforeEach(function () { | ||
testRenderer1 = Renderer.install({ | ||
url: 'https://httpbin.org/post', | ||
config: { test: 'config1' }, | ||
id: 1 | ||
import * as utils from 'src/utils'; | ||
|
||
describe('Renderer', function () { | ||
describe('Renderer: A renderer installed on a bid response', function () { | ||
let testRenderer1; | ||
let testRenderer2; | ||
let spyRenderFn; | ||
let spyEventHandler; | ||
|
||
beforeEach(function () { | ||
testRenderer1 = Renderer.install({ | ||
url: 'https://httpbin.org/post', | ||
config: { test: 'config1' }, | ||
id: 1 | ||
}); | ||
testRenderer2 = Renderer.install({ | ||
url: 'https://httpbin.org/post', | ||
config: { test: 'config2' }, | ||
id: 2 | ||
}); | ||
|
||
spyRenderFn = sinon.spy(); | ||
spyEventHandler = sinon.spy(); | ||
}); | ||
testRenderer2 = Renderer.install({ | ||
url: 'https://httpbin.org/post', | ||
config: { test: 'config2' }, | ||
id: 2 | ||
|
||
it('is an instance of Renderer', function () { | ||
expect(testRenderer1 instanceof Renderer).to.equal(true); | ||
}); | ||
|
||
spyRenderFn = sinon.spy(); | ||
spyEventHandler = sinon.spy(); | ||
}); | ||
it('has expected properties ', function () { | ||
expect(testRenderer1.url).to.equal('https://httpbin.org/post'); | ||
expect(testRenderer1.config).to.deep.equal({ test: 'config1' }); | ||
expect(testRenderer1.id).to.equal(1); | ||
}); | ||
|
||
it('is an instance of Renderer', function () { | ||
expect(testRenderer1 instanceof Renderer).to.equal(true); | ||
}); | ||
it('returns config from getConfig method', function () { | ||
expect(testRenderer1.getConfig()).to.deep.equal({ test: 'config1' }); | ||
expect(testRenderer2.getConfig()).to.deep.equal({ test: 'config2' }); | ||
}); | ||
|
||
it('has expected properties ', function () { | ||
expect(testRenderer1.url).to.equal('https://httpbin.org/post'); | ||
expect(testRenderer1.config).to.deep.equal({ test: 'config1' }); | ||
expect(testRenderer1.id).to.equal(1); | ||
}); | ||
it('sets a render function with setRender method', function () { | ||
testRenderer1.setRender(spyRenderFn); | ||
expect(typeof testRenderer1.render).to.equal('function'); | ||
testRenderer1.render(); | ||
expect(spyRenderFn.called).to.equal(true); | ||
}); | ||
|
||
it('returns config from getConfig method', function () { | ||
expect(testRenderer1.getConfig()).to.deep.equal({ test: 'config1' }); | ||
expect(testRenderer2.getConfig()).to.deep.equal({ test: 'config2' }); | ||
}); | ||
it('sets event handlers with setEventHandlers method and handles events with installed handlers', function () { | ||
testRenderer1.setEventHandlers({ | ||
testEvent: spyEventHandler | ||
}); | ||
|
||
it('sets a render function with setRender method', function () { | ||
testRenderer1.setRender(spyRenderFn); | ||
expect(typeof testRenderer1.render).to.equal('function'); | ||
testRenderer1.render(); | ||
expect(spyRenderFn.called).to.equal(true); | ||
}); | ||
expect(testRenderer1.handlers).to.deep.equal({ | ||
testEvent: spyEventHandler | ||
}); | ||
|
||
it('sets event handlers with setEventHandlers method and handles events with installed handlers', function () { | ||
testRenderer1.setEventHandlers({ | ||
testEvent: spyEventHandler | ||
testRenderer1.handleVideoEvent({ id: 1, eventName: 'testEvent' }); | ||
expect(spyEventHandler.called).to.equal(true); | ||
}); | ||
|
||
expect(testRenderer1.handlers).to.deep.equal({ | ||
testEvent: spyEventHandler | ||
it('pushes commands to queue if renderer is not loaded', function () { | ||
testRenderer1.loaded = false; | ||
testRenderer1.push(spyRenderFn); | ||
expect(testRenderer1.cmd.length).to.equal(1); | ||
|
||
// clear queue for next tests | ||
testRenderer1.cmd = []; | ||
}); | ||
|
||
testRenderer1.handleVideoEvent({ id: 1, eventName: 'testEvent' }); | ||
expect(spyEventHandler.called).to.equal(true); | ||
}); | ||
it('fires commands immediately if the renderer is loaded', function () { | ||
const func = sinon.spy(); | ||
|
||
it('pushes commands to queue if renderer is not loaded', function () { | ||
testRenderer1.loaded = false; | ||
testRenderer1.push(spyRenderFn); | ||
expect(testRenderer1.cmd.length).to.equal(1); | ||
testRenderer1.loaded = true; | ||
testRenderer1.push(func); | ||
|
||
// clear queue for next tests | ||
testRenderer1.cmd = []; | ||
}); | ||
expect(testRenderer1.cmd.length).to.equal(0); | ||
|
||
it('fires commands immediately if the renderer is loaded', function () { | ||
const func = sinon.spy(); | ||
sinon.assert.calledOnce(func); | ||
}); | ||
|
||
testRenderer1.loaded = true; | ||
testRenderer1.push(func); | ||
it('processes queue by calling each function in queue', function () { | ||
testRenderer1.loaded = false; | ||
const func1 = sinon.spy(); | ||
const func2 = sinon.spy(); | ||
|
||
expect(testRenderer1.cmd.length).to.equal(0); | ||
testRenderer1.push(func1); | ||
testRenderer1.push(func2); | ||
expect(testRenderer1.cmd.length).to.equal(2); | ||
|
||
sinon.assert.calledOnce(func); | ||
}); | ||
testRenderer1.process(); | ||
|
||
it('processes queue by calling each function in queue', function () { | ||
testRenderer1.loaded = false; | ||
const func1 = sinon.spy(); | ||
const func2 = sinon.spy(); | ||
sinon.assert.calledOnce(func1); | ||
sinon.assert.calledOnce(func2); | ||
expect(testRenderer1.cmd.length).to.equal(0); | ||
}); | ||
}); | ||
|
||
testRenderer1.push(func1); | ||
testRenderer1.push(func2); | ||
expect(testRenderer1.cmd.length).to.equal(2); | ||
describe('3rd party renderer', function () { | ||
let adUnitsOld; | ||
let utilsSpy; | ||
before(function () { | ||
adUnitsOld = $$PREBID_GLOBAL$$.adUnits; | ||
utilsSpy = sinon.spy(utils, 'logWarn'); | ||
}); | ||
|
||
testRenderer1.process(); | ||
after(function() { | ||
$$PREBID_GLOBAL$$.adUnits = adUnitsOld; | ||
utilsSpy.restore(); | ||
}); | ||
|
||
sinon.assert.calledOnce(func1); | ||
sinon.assert.calledOnce(func2); | ||
expect(testRenderer1.cmd.length).to.equal(0); | ||
it('should not load renderer and log warn message', function() { | ||
$$PREBID_GLOBAL$$.adUnits = [{ | ||
code: 'video1', | ||
renderer: { | ||
url: 'http://cdn.adnxs.com/renderer/video/ANOutstreamVideo.js', | ||
render: sinon.spy() | ||
} | ||
}] | ||
|
||
let testRenderer = Renderer.install({ | ||
url: 'https://httpbin.org/post', | ||
config: { test: 'config1' }, | ||
id: 1, | ||
adUnitCode: 'video1' | ||
}); | ||
expect(utilsSpy.callCount).to.equal(1); | ||
}); | ||
}); | ||
}); |