@@ -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
@@ -67,52 +69,143 @@ describe('renderingManager', function() {
67
69
} ) ;
68
70
69
71
describe ( 'cross domain creative' , function ( ) {
72
+ const ORIGIN = 'http://example.com' ;
70
73
let parseStub ;
71
74
let iframeStub ;
72
75
let triggerPixelSpy ;
76
+ let mockWin ;
77
+ let ucTagData ;
78
+ let mockIframe ;
79
+ let eventSource ;
80
+
73
81
beforeEach ( function ( ) {
82
+ mockIframe = createMockIframe ( ) ;
74
83
parseStub = sinon . stub ( utils , 'parseUrl' ) ;
75
- iframeStub = sinon . stub ( domHelper , 'getEmptyIframe' ) ;
84
+ iframeStub = sinon . stub ( domHelper , 'getEmptyIframe' ) . returns ( mockIframe ) ;
76
85
triggerPixelSpy = sinon . stub ( utils , 'triggerPixel' ) ;
77
- } ) ;
78
-
79
- after ( function ( ) {
80
- parseStub . restore ( ) ;
81
- iframeStub . restore ( ) ;
82
- triggerPixelSpy . restore ( ) ;
83
- } ) ;
84
-
85
- it ( 'should render cross domain creative' , function ( ) {
86
86
parseStub . returns ( {
87
87
protocol : 'http' ,
88
88
host : 'example.com'
89
89
} ) ;
90
- iframeStub . returns ( mockIframe ) ;
91
-
92
- const mockWin = merge ( mocks . createFakeWindow ( 'http://example.com' ) , renderingMocks . getWindowObject ( ) ) ;
93
- let ucTagData = {
90
+ mockWin = merge ( mocks . createFakeWindow ( ORIGIN ) , renderingMocks . getWindowObject ( ) ) ;
91
+ ucTagData = {
94
92
adId : '123' ,
95
93
adServerDomain : 'mypub.com' ,
96
- pubUrl : 'http://example.com'
94
+ pubUrl : ORIGIN ,
97
95
} ;
96
+ eventSource = null ;
97
+
98
98
renderCrossDomain ( mockWin , ucTagData . adId , ucTagData . adServerDomain , ucTagData . pubUrl ) ;
99
99
100
- // dummy implementation of postmessage from prebid.js
101
- let ev = {
102
- origin : 'http://example.com' ,
103
- message : JSON . stringify ( {
104
- message : 'Prebid Response' ,
105
- ad : 'ad' ,
106
- adUrl : 'http://example.com' ,
107
- adId : '123' ,
108
- width : 300 ,
109
- height : 250
110
- } )
100
+ } ) ;
101
+
102
+ afterEach ( function ( ) {
103
+ parseStub . restore ( ) ;
104
+ iframeStub . restore ( ) ;
105
+ triggerPixelSpy . restore ( ) ;
106
+ } ) ;
107
+
108
+ function mockPrebidResponse ( msg ) {
109
+ eventSource = {
110
+ postMessage : sinon . spy ( )
111
111
} ;
112
+ mockWin . postMessage ( {
113
+ source : eventSource ,
114
+ origin : ORIGIN ,
115
+ message : JSON . stringify ( Object . assign ( { message : 'Prebid Response' } , msg ) )
116
+ } ) ;
117
+ }
112
118
113
- mockWin . postMessage ( ev ) ;
119
+ it ( 'should render cross domain creative' , function ( ) {
120
+ mockPrebidResponse ( {
121
+ ad : 'ad' ,
122
+ adUrl : ORIGIN ,
123
+ adId : '123' ,
124
+ width : 300 ,
125
+ height : 250
126
+ } ) ;
114
127
expect ( mockIframe . contentDocument . write . args [ 0 ] [ 0 ] ) . to . equal ( "ad" ) ;
115
128
} ) ;
129
+
130
+ describe ( 'should signal event' , ( ) => {
131
+ const RENDER_FAILED = 'adRenderFailed' ,
132
+ RENDER_SUCCESS = 'adRenderSucceeded' ;
133
+
134
+ function expectEventMessage ( expected ) {
135
+ const actual = JSON . parse ( eventSource . postMessage . args [ 0 ] [ 0 ] ) ;
136
+ sinon . assert . match ( actual , Object . assign ( { message : 'Prebid Event' } , expected ) ) ;
137
+ }
138
+
139
+ describe ( 'AD_RENDER_FAILED' , ( ) => {
140
+ it ( 'on video ads' , ( ) => {
141
+ mockPrebidResponse ( {
142
+ ad : 'ad' ,
143
+ adId : '123' ,
144
+ mediaType : 'video'
145
+ } ) ;
146
+ expectEventMessage ( {
147
+ adId : '123' ,
148
+ event : RENDER_FAILED ,
149
+ info : {
150
+ reason : 'preventWritingOnMainDocument'
151
+ }
152
+ } ) ;
153
+ } ) ;
154
+
155
+ it ( 'on ads that have no markup or adUrl' , ( ) => {
156
+ mockPrebidResponse ( {
157
+ adId : '123' ,
158
+ } ) ;
159
+ expectEventMessage ( {
160
+ adId : '123' ,
161
+ event : RENDER_FAILED ,
162
+ info : {
163
+ reason : 'noAd'
164
+ }
165
+ } ) ;
166
+ } ) ;
167
+
168
+ it ( 'on exceptions' , ( ) => {
169
+ iframeStub . callsFake ( ( ) => {
170
+ throw new Error ( ) ;
171
+ } ) ;
172
+ mockPrebidResponse ( {
173
+ adId : '123' ,
174
+ ad : 'ad' ,
175
+ adUrl : ORIGIN ,
176
+ } ) ;
177
+ expectEventMessage ( {
178
+ adId : '123' ,
179
+ event : RENDER_FAILED ,
180
+ info : {
181
+ reason : 'exception'
182
+ }
183
+ } ) ;
184
+ } ) ;
185
+ } ) ;
186
+ describe ( 'should post AD_RENDER_SUCCEEDED' , ( ) => {
187
+ it ( 'on ad with markup' , ( ) => {
188
+ mockPrebidResponse ( {
189
+ adId : '123' ,
190
+ ad : 'markup'
191
+ } ) ;
192
+ expectEventMessage ( {
193
+ adId : '123' ,
194
+ event : RENDER_SUCCESS
195
+ } ) ;
196
+ } ) ;
197
+ it ( 'on ad with adUrl' , ( ) => {
198
+ mockPrebidResponse ( {
199
+ adId : '123' ,
200
+ adUrl : 'url'
201
+ } ) ;
202
+ expectEventMessage ( {
203
+ adId : '123' ,
204
+ event : RENDER_SUCCESS
205
+ } ) ;
206
+ } )
207
+ } )
208
+ } ) ;
116
209
} ) ;
117
210
118
211
describe ( 'legacy creative' , function ( ) {
0 commit comments