prebid-universal-creative 1.14.1 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,308 @@
1
+ import { renderAmpOrMobileAd } from 'src/mobileAndAmpRender';
2
+ import * as postscribeRender from 'src/postscribeRender'
3
+ import * as utils from 'src/utils';
4
+ import { expect } from 'chai';
5
+ import { mocks } from 'test/helpers/mocks';
6
+ import { merge } from 'lodash';
7
+
8
+
9
+ function renderingMocks() {
10
+ return {
11
+ messages: [],
12
+ getWindowObject: function () {
13
+ return {
14
+ document: {
15
+ body: {
16
+ appendChild: sinon.spy(),
17
+ },
18
+ createComment: () => true,
19
+ },
20
+ parent: {
21
+ postMessage: sinon.spy(),
22
+ $$PREBID_GLOBAL$$: {
23
+ renderAd: sinon.spy(),
24
+ },
25
+ },
26
+ postMessage: (message, domain) => {
27
+ this.messages[0](message);
28
+ },
29
+ top: null,
30
+ $sf: {
31
+ ext: {
32
+ register: sinon.spy(),
33
+ expand: sinon.spy(),
34
+ },
35
+ },
36
+ addEventListener: (type, listener, capture) => {
37
+ this.messages.push(listener);
38
+ },
39
+ innerWidth: 300,
40
+ innerHeight: 250,
41
+ };
42
+ },
43
+ };
44
+ }
45
+
46
+ describe("renderingManager", function () {
47
+ let xhr;
48
+ let requests;
49
+
50
+ before(function () {
51
+ xhr = sinon.useFakeXMLHttpRequest();
52
+ xhr.onCreate = (request) => requests.push(request);
53
+ });
54
+
55
+ beforeEach(function () {
56
+ requests = [];
57
+ });
58
+
59
+ after(function () {
60
+ xhr.restore();
61
+ });
62
+
63
+ describe("mobile creative", function () {
64
+ let writeHtmlSpy;
65
+ let sendRequestSpy;
66
+ let triggerPixelSpy;
67
+ let mockWin;
68
+
69
+ before(function () {
70
+ writeHtmlSpy = sinon.spy(postscribeRender, "writeAdHtml");
71
+ sendRequestSpy = sinon.spy(utils, "sendRequest");
72
+ triggerPixelSpy = sinon.spy(utils, "triggerPixel");
73
+ mockWin = merge(
74
+ mocks.createFakeWindow("http://example.com"),
75
+ renderingMocks().getWindowObject()
76
+ );
77
+ });
78
+
79
+ afterEach(function () {
80
+ writeHtmlSpy.resetHistory();
81
+ sendRequestSpy.resetHistory();
82
+ triggerPixelSpy.resetHistory();
83
+ });
84
+
85
+ after(function () {
86
+ writeHtmlSpy.restore();
87
+ sendRequestSpy.restore();
88
+ triggerPixelSpy.restore();
89
+ });
90
+
91
+ it("should render mobile app creative", function () {
92
+ let ucTagData = {
93
+ cacheHost: "example.com",
94
+ cachePath: "/path",
95
+ uuid: "123",
96
+ size: "300x250",
97
+ };
98
+
99
+ renderAmpOrMobileAd(ucTagData, true);
100
+
101
+ let response = {
102
+ width: 300,
103
+ height: 250,
104
+ crid: 123,
105
+ adm: "ad-markup",
106
+ wurl: "https://test.prebidcache.wurl",
107
+ };
108
+ requests[0].respond(200, {}, JSON.stringify(response));
109
+ expect(writeHtmlSpy.callCount).to.equal(1);
110
+ expect(sendRequestSpy.args[0][0]).to.equal(
111
+ "https://example.com/path?uuid=123"
112
+ );
113
+ });
114
+
115
+ it("should render mobile app creative with missing cache wurl", function () {
116
+ let ucTagData = {
117
+ cacheHost: "example.com",
118
+ cachePath: "/path",
119
+ uuid: "123",
120
+ size: "300x250",
121
+ };
122
+
123
+ renderAmpOrMobileAd(ucTagData, true);
124
+
125
+ let response = {
126
+ width: 300,
127
+ height: 250,
128
+ crid: 123,
129
+ adm: "ad-markup",
130
+ };
131
+ requests[0].respond(200, {}, JSON.stringify(response));
132
+ expect(writeHtmlSpy.callCount).to.equal(1);
133
+ expect(sendRequestSpy.args[0][0]).to.equal(
134
+ "https://example.com/path?uuid=123"
135
+ );
136
+ });
137
+
138
+ it("should render mobile app creative using default cacheHost and cachePath", function () {
139
+ let ucTagData = {
140
+ uuid: "123",
141
+ size: "300x250",
142
+ };
143
+ renderAmpOrMobileAd(ucTagData, true);
144
+
145
+ let response = {
146
+ width: 300,
147
+ height: 250,
148
+ crid: 123,
149
+ adm: "ad-markup",
150
+ };
151
+ requests[0].respond(200, {}, JSON.stringify(response));
152
+ expect(writeHtmlSpy.callCount).to.equal(1);
153
+ expect(sendRequestSpy.args[0][0]).to.equal(
154
+ "https://prebid.adnxs.com/pbc/v1/cache?uuid=123"
155
+ );
156
+ });
157
+
158
+ // it('should catch errors from creative', function (done) {
159
+ // window.addEventListener('error', e => {
160
+ // done(e.error);
161
+ // });
162
+
163
+ // const consoleErrorSpy = sinon.spy(console, 'error');
164
+
165
+ // let ucTagData = {
166
+ // cacheHost: 'example.com',
167
+ // cachePath: '/path',
168
+ // uuid: '123',
169
+ // size: '300x250'
170
+ // };
171
+
172
+ // renderAmpOrMobileAd(ucTagData, true);
173
+
174
+ // let response = {
175
+ // width: 300,
176
+ // height: 250,
177
+ // crid: 123,
178
+ // adm: '<script src="notExistingScript.js"></script>'
179
+ // };
180
+ // requests[0].respond(200, {}, JSON.stringify(response));
181
+
182
+ // setTimeout(() => {
183
+ // expect(consoleErrorSpy.callCount).to.equal(1);
184
+ // done();
185
+ // }, 10);
186
+ // });
187
+ });
188
+
189
+ describe("amp creative", function () {
190
+ let writeHtmlSpy;
191
+ let sendRequestSpy;
192
+ let triggerPixelSpy;
193
+ let mockWin;
194
+
195
+ before(function () {
196
+ writeHtmlSpy = sinon.spy(postscribeRender, "writeAdHtml");
197
+ sendRequestSpy = sinon.spy(utils, "sendRequest");
198
+ triggerPixelSpy = sinon.spy(utils, "triggerPixel");
199
+ mockWin = merge(
200
+ mocks.createFakeWindow("http://example.com"),
201
+ renderingMocks().getWindowObject()
202
+ );
203
+ });
204
+
205
+ afterEach(function () {
206
+ writeHtmlSpy.resetHistory();
207
+ sendRequestSpy.resetHistory();
208
+ triggerPixelSpy.resetHistory();
209
+ });
210
+
211
+ after(function () {
212
+ writeHtmlSpy.restore();
213
+ sendRequestSpy.restore();
214
+ triggerPixelSpy.restore();
215
+ });
216
+
217
+ it("should render amp creative", function () {
218
+ let ucTagData = {
219
+ cacheHost: "example.com",
220
+ cachePath: "/path",
221
+ uuid: "123",
222
+ size: "300x250",
223
+ hbPb: "10.00",
224
+ };
225
+
226
+ renderAmpOrMobileAd(ucTagData);
227
+
228
+ let response = {
229
+ width: 300,
230
+ height: 250,
231
+ crid: 123,
232
+ adm: "ad-markup${AUCTION_PRICE}",
233
+ wurl: "https://test.prebidcache.wurl",
234
+ };
235
+ requests[0].respond(200, {}, JSON.stringify(response));
236
+ expect(writeHtmlSpy.args[0][0]).to.equal(
237
+ "<!--Creative 123 served by Prebid.js Header Bidding-->ad-markup10.00"
238
+ );
239
+ expect(sendRequestSpy.args[0][0]).to.equal(
240
+ "https://example.com/path?uuid=123"
241
+ );
242
+ expect(triggerPixelSpy.args[0][0]).to.equal(
243
+ "https://test.prebidcache.wurl"
244
+ );
245
+ });
246
+
247
+ it("should replace AUCTION_PRICE with response.price over hbPb", function () {
248
+ let ucTagData = {
249
+ cacheHost: "example.com",
250
+ cachePath: "/path",
251
+ uuid: "123",
252
+ size: "300x250",
253
+ hbPb: "10.00",
254
+ };
255
+
256
+ renderAmpOrMobileAd(ucTagData);
257
+
258
+ let response = {
259
+ width: 300,
260
+ height: 250,
261
+ crid: 123,
262
+ price: 12.5,
263
+ adm: "ad-markup${AUCTION_PRICE}",
264
+ wurl: "https://test.prebidcache.wurl",
265
+ };
266
+ requests[0].respond(200, {}, JSON.stringify(response));
267
+ expect(writeHtmlSpy.args[0][0]).to.equal(
268
+ "<!--Creative 123 served by Prebid.js Header Bidding-->ad-markup12.5"
269
+ );
270
+ expect(sendRequestSpy.args[0][0]).to.equal(
271
+ "https://example.com/path?uuid=123"
272
+ );
273
+ expect(triggerPixelSpy.args[0][0]).to.equal(
274
+ "https://test.prebidcache.wurl"
275
+ );
276
+ });
277
+
278
+ it("should replace AUCTION_PRICE with with empty value when neither price nor hbPb exist", function () {
279
+ let ucTagData = {
280
+ cacheHost: "example.com",
281
+ cachePath: "/path",
282
+ uuid: "123",
283
+ size: "300x250",
284
+ };
285
+
286
+ renderAmpOrMobileAd(ucTagData);
287
+
288
+ let response = {
289
+ width: 300,
290
+ height: 250,
291
+ crid: 123,
292
+ adm: "ad-markup${AUCTION_PRICE}",
293
+ wurl: "https://test.prebidcache.wurl",
294
+ };
295
+ requests[0].respond(200, {}, JSON.stringify(response));
296
+ expect(writeHtmlSpy.args[0][0]).to.equal(
297
+ "<!--Creative 123 served by Prebid.js Header Bidding-->ad-markup"
298
+ );
299
+ expect(sendRequestSpy.args[0][0]).to.equal(
300
+ "https://example.com/path?uuid=123"
301
+ );
302
+ expect(triggerPixelSpy.args[0][0]).to.equal(
303
+ "https://test.prebidcache.wurl"
304
+ );
305
+ });
306
+ });
307
+ });
308
+
@@ -12,6 +12,7 @@ const NATIVE_KEYS = {
12
12
  body: 'hb_native_body',
13
13
  body2: 'hb_native_body2',
14
14
  privacyLink: 'hb_native_privacy',
15
+ privacyIcon: 'hb_native_privicon',
15
16
  sponsoredBy: 'hb_native_brand',
16
17
  image: 'hb_native_image',
17
18
  icon: 'hb_native_icon',
@@ -55,18 +55,19 @@ describe('nativeRenderManager', function () {
55
55
  assetManagerStub.restore();
56
56
  });
57
57
 
58
- it('should verify the postMessage for impression trackers was executed', function() {
59
- mockWin.document.getElementsByClassName = () => [{
60
- attributes: {
61
- pbAdId: {
62
- value: 'ad123'
63
- }
64
- },
65
- addEventListener: (type, listener, capture) => {
58
+ it("should verify the postMessage for impression trackers was executed", function () {
59
+ mockWin.document.getElementsByClassName = () => [
60
+ {
61
+ attributes: {
62
+ pbAdId: {
63
+ value: "ad123",
64
+ },
65
+ },
66
+ addEventListener: (type, listener, capture) => {},
66
67
  },
67
- }];
68
+ ];
68
69
  let nativeTracker = new newNativeRenderManager(mockWin);
69
- nativeTracker.renderNativeAd(tagData);
70
+ nativeTracker.renderNativeAd(mockWin.document, tagData);
70
71
 
71
72
  expect(mockWin.parent.postMessage.callCount).to.equal(1);
72
73
  let postMessageTargetDomain = mockWin.parent.postMessage.args[0][1];
@@ -93,7 +94,7 @@ describe('nativeRenderManager', function () {
93
94
  }];
94
95
 
95
96
  let nativeTracker = new newNativeRenderManager(mockWin);
96
- nativeTracker.renderNativeAd(tagData);
97
+ nativeTracker.renderNativeAd(mockWin.document, tagData);
97
98
 
98
99
  expect(mockWin.parent.postMessage.callCount).to.equal(2);
99
100
 
@@ -0,0 +1,23 @@
1
+ import '../../src/nativeRender';
2
+
3
+ describe('nativeRender', () => {
4
+
5
+ after(() => {
6
+ delete window.ucTag;
7
+ })
8
+
9
+ it('should accept 2 arguments', () => {
10
+ expect(window.ucTag.renderAd).to.exist;
11
+ //expect exactly two arguments by this function
12
+ expect(window.ucTag.renderAd.length).to.equal(2);
13
+
14
+ // this function with two arguments and see it NOT throwing
15
+ const renderAd = window.ucTag.renderAd.bind(this, document, {
16
+ pubUrl: 'http://prebidjs.com',
17
+ adId: 'abc123',
18
+ replaceAllAssets: true
19
+ })
20
+ expect(renderAd).to.not.throw();
21
+
22
+ })
23
+ })