@@ -4,7 +4,6 @@ import * as domHelper from 'src/domHelper';
4
4
import { expect } from 'chai' ;
5
5
import { mocks } from 'test/helpers/mocks' ;
6
6
import { merge } from 'lodash' ;
7
- import * as postscribe from "postscribe" ;
8
7
9
8
const renderingMocks = {
10
9
messages : [ ] ,
@@ -41,11 +40,14 @@ const renderingMocks = {
41
40
}
42
41
}
43
42
44
- let mockIframe = {
45
- contentDocument : {
46
- open : sinon . spy ( ) ,
47
- write : sinon . spy ( ) ,
48
- close : sinon . spy ( )
43
+ function createMockIframe ( ) {
44
+ return {
45
+ contentDocument : {
46
+ open : sinon . spy ( ) ,
47
+ write : sinon . spy ( ) ,
48
+ close : sinon . spy ( )
49
+ } ,
50
+ style : { } ,
49
51
}
50
52
}
51
53
@@ -304,59 +306,151 @@ describe('renderingManager', function() {
304
306
} ) ;
305
307
306
308
describe ( 'cross domain creative' , function ( ) {
309
+ const ORIGIN = 'http://example.com' ;
307
310
let parseStub ;
308
311
let iframeStub ;
309
312
let triggerPixelSpy ;
313
+ let mockWin ;
314
+ let env ;
315
+ let renderObject ;
316
+ let ucTagData ;
317
+ let mockIframe ;
318
+ let eventSource ;
319
+
310
320
beforeEach ( function ( ) {
321
+ mockIframe = createMockIframe ( ) ;
311
322
parseStub = sinon . stub ( utils , 'parseUrl' ) ;
312
- iframeStub = sinon . stub ( domHelper , 'getEmptyIframe' ) ;
323
+ iframeStub = sinon . stub ( domHelper , 'getEmptyIframe' ) . returns ( mockIframe ) ;
313
324
triggerPixelSpy = sinon . stub ( utils , 'triggerPixel' ) ;
314
- } ) ;
315
-
316
- after ( function ( ) {
317
- parseStub . restore ( ) ;
318
- iframeStub . restore ( ) ;
319
- triggerPixelSpy . restore ( ) ;
320
- } ) ;
321
-
322
- it ( 'should render cross domain creative' , function ( ) {
323
325
parseStub . returns ( {
324
326
protocol : 'http' ,
325
327
host : 'example.com'
326
328
} ) ;
327
- iframeStub . returns ( mockIframe ) ;
328
-
329
- const mockWin = merge ( mocks . createFakeWindow ( 'http://example.com' ) , renderingMocks . getWindowObject ( ) ) ;
330
- const env = {
329
+ mockWin = merge ( mocks . createFakeWindow ( ORIGIN ) , renderingMocks . getWindowObject ( ) ) ;
330
+ env = {
331
331
isMobileApp : ( ) => false ,
332
332
isAmp : ( ) => false ,
333
333
canLocatePrebid : ( ) => false
334
334
} ;
335
- const renderObject = newRenderingManager ( mockWin , env ) ;
336
- let ucTagData = {
335
+ renderObject = newRenderingManager ( mockWin , env ) ;
336
+ ucTagData = {
337
337
adId : '123' ,
338
338
adServerDomain : 'mypub.com' ,
339
- pubUrl : 'http://example.com'
339
+ pubUrl : ORIGIN ,
340
340
} ;
341
+ eventSource = null ;
341
342
342
343
renderObject . renderAd ( mockWin . document , ucTagData ) ;
343
344
344
- // dummy implementation of postmessage from prebid.js
345
- let ev = {
346
- origin : 'http://example.com' ,
347
- message : JSON . stringify ( {
348
- message : 'Prebid Response' ,
349
- ad : 'ad' ,
350
- adUrl : 'http://example.com' ,
351
- adId : '123' ,
352
- width : 300 ,
353
- height : 250
354
- } )
345
+ } ) ;
346
+
347
+ afterEach ( function ( ) {
348
+ parseStub . restore ( ) ;
349
+ iframeStub . restore ( ) ;
350
+ triggerPixelSpy . restore ( ) ;
351
+ } ) ;
352
+
353
+ function mockPrebidResponse ( msg ) {
354
+ eventSource = {
355
+ postMessage : sinon . spy ( )
355
356
} ;
357
+ mockWin . postMessage ( {
358
+ source : eventSource ,
359
+ origin : ORIGIN ,
360
+ message : JSON . stringify ( Object . assign ( { message : 'Prebid Response' } , msg ) )
361
+ } ) ;
362
+ }
356
363
357
- mockWin . postMessage ( ev ) ;
364
+ it ( 'should render cross domain creative' , function ( ) {
365
+ mockPrebidResponse ( {
366
+ ad : 'ad' ,
367
+ adUrl : ORIGIN ,
368
+ adId : '123' ,
369
+ width : 300 ,
370
+ height : 250
371
+ } ) ;
358
372
expect ( mockIframe . contentDocument . write . args [ 0 ] [ 0 ] ) . to . equal ( "ad" ) ;
359
373
} ) ;
374
+
375
+ describe ( 'should signal event' , ( ) => {
376
+ const RENDER_FAILED = 'adRenderFailed' ,
377
+ RENDER_SUCCESS = 'adRenderSucceeded' ;
378
+
379
+ function expectEventMessage ( expected ) {
380
+ const actual = JSON . parse ( eventSource . postMessage . args [ 0 ] [ 0 ] ) ;
381
+ sinon . assert . match ( actual , Object . assign ( { message : 'Prebid Event' } , expected ) ) ;
382
+ }
383
+
384
+ describe ( 'AD_RENDER_FAILED' , ( ) => {
385
+ it ( 'on video ads' , ( ) => {
386
+ mockPrebidResponse ( {
387
+ ad : 'ad' ,
388
+ adId : '123' ,
389
+ mediaType : 'video'
390
+ } ) ;
391
+ expectEventMessage ( {
392
+ adId : '123' ,
393
+ event : RENDER_FAILED ,
394
+ info : {
395
+ reason : 'preventWritingOnMainDocument'
396
+ }
397
+ } )
398
+ } ) ;
399
+
400
+ it ( 'on ads that have no markup or adUrl' , ( ) => {
401
+ mockPrebidResponse ( {
402
+ adId : '123' ,
403
+ } )
404
+ expectEventMessage ( {
405
+ adId : '123' ,
406
+ event : RENDER_FAILED ,
407
+ info : {
408
+ reason : 'noAd'
409
+ }
410
+ } ) ;
411
+ } ) ;
412
+
413
+ it ( 'on exceptions' , ( ) => {
414
+ iframeStub . callsFake ( ( ) => {
415
+ throw new Error ( )
416
+ } ) ;
417
+ mockPrebidResponse ( {
418
+ adId : '123' ,
419
+ ad : 'ad' ,
420
+ adUrl : ORIGIN ,
421
+ } ) ;
422
+ expectEventMessage ( {
423
+ adId : '123' ,
424
+ event : RENDER_FAILED ,
425
+ info : {
426
+ reason : 'exception'
427
+ }
428
+ } ) ;
429
+ } )
430
+ } ) ;
431
+ describe ( 'should post AD_RENDER_SUCCEEDED' , ( ) => {
432
+ it ( 'on ad with markup' , ( ) => {
433
+ mockPrebidResponse ( {
434
+ adId : '123' ,
435
+ ad : 'markup'
436
+ } ) ;
437
+ expectEventMessage ( {
438
+ adId : '123' ,
439
+ event : RENDER_SUCCESS
440
+ } ) ;
441
+ } ) ;
442
+ it ( 'on ad with adUrl' , ( ) => {
443
+ mockPrebidResponse ( {
444
+ adId : '123' ,
445
+ adUrl : 'url'
446
+ } ) ;
447
+ expectEventMessage ( {
448
+ adId : '123' ,
449
+ event : RENDER_SUCCESS
450
+ } ) ;
451
+ } )
452
+ } )
453
+ } ) ;
360
454
} ) ;
361
455
362
456
describe ( 'legacy creative' , function ( ) {
0 commit comments