forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_spec.js
More file actions
188 lines (171 loc) · 5.62 KB
/
Copy pathnative_spec.js
File metadata and controls
188 lines (171 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { expect } from 'chai';
import { fireNativeTrackers, getNativeTargeting, nativeBidIsValid } from 'src/native';
import CONSTANTS from 'src/constants.json';
const utils = require('src/utils');
const bid = {
native: {
title: 'Native Creative',
body: 'Cool description great stuff',
cta: 'Do it',
sponsoredBy: 'AppNexus',
clickUrl: 'https://www.link.example',
clickTrackers: ['https://tracker.example'],
impressionTrackers: ['https://impression.example'],
javascriptTrackers: '<script src=\"http://www.foobar.js\"></script>'
}
};
const bidWithUndefinedFields = {
native: {
title: 'Native Creative',
body: undefined,
cta: undefined,
sponsoredBy: 'AppNexus',
clickUrl: 'https://www.link.example',
clickTrackers: ['https://tracker.example'],
impressionTrackers: ['https://impression.example'],
javascriptTrackers: '<script src=\"http://www.foobar.js\"></script>'
}
};
describe('native.js', function () {
let triggerPixelStub;
let insertHtmlIntoIframeStub;
beforeEach(function () {
triggerPixelStub = sinon.stub(utils, 'triggerPixel');
insertHtmlIntoIframeStub = sinon.stub(utils, 'insertHtmlIntoIframe');
});
afterEach(function () {
utils.triggerPixel.restore();
utils.insertHtmlIntoIframe.restore();
});
it('gets native targeting keys', function () {
const targeting = getNativeTargeting(bid);
expect(targeting[CONSTANTS.NATIVE_KEYS.title]).to.equal(bid.native.title);
expect(targeting[CONSTANTS.NATIVE_KEYS.body]).to.equal(bid.native.body);
expect(targeting[CONSTANTS.NATIVE_KEYS.clickUrl]).to.equal(bid.native.clickUrl);
});
it('should only include native targeting keys with values', function () {
const targeting = getNativeTargeting(bidWithUndefinedFields);
expect(Object.keys(targeting)).to.deep.equal([
CONSTANTS.NATIVE_KEYS.title,
CONSTANTS.NATIVE_KEYS.sponsoredBy,
CONSTANTS.NATIVE_KEYS.clickUrl
]);
});
it('fires impression trackers', function () {
fireNativeTrackers({}, bid);
sinon.assert.calledOnce(triggerPixelStub);
sinon.assert.calledWith(triggerPixelStub, bid.native.impressionTrackers[0]);
sinon.assert.calledWith(insertHtmlIntoIframeStub, bid.native.javascriptTrackers);
});
it('fires click trackers', function () {
fireNativeTrackers({ action: 'click' }, bid);
sinon.assert.calledOnce(triggerPixelStub);
sinon.assert.calledWith(triggerPixelStub, bid.native.clickTrackers[0]);
});
});
describe('validate native', function () {
let bidReq = [{
bids: [{
bidderCode: 'test_bidder',
bidId: 'test_bid_id',
mediaTypes: {
native: {
title: {
required: true,
},
body: {
required: true,
},
image: {
required: true,
sizes: [150, 50],
aspect_ratios: [150, 50]
},
icon: {
required: true,
sizes: [50, 50]
},
}
}
}]
}];
let validBid = {
adId: 'test_bid_id',
adUnitCode: '123/prebid_native_adunit',
bidder: 'test_bidder',
native: {
body: 'This is a Prebid Native Creative. There are many like it, but this one is mine.',
clickTrackers: ['http://my.click.tracker/url'],
icon: {
url: 'http://my.image.file/ad_image.jpg',
height: 75,
width: 75
},
image: {
url: 'http://my.icon.file/ad_icon.jpg',
height: 2250,
width: 3000
},
clickUrl: 'http://prebid.org/dev-docs/show-native-ads.html',
impressionTrackers: ['http://my.imp.tracker/url'],
javascriptTrackers: '<script src=\"http://www.foobar.js\"></script>',
title: 'This is an example Prebid Native creative'
}
};
let noIconDimBid = {
adId: 'test_bid_id',
adUnitCode: '123/prebid_native_adunit',
bidder: 'test_bidder',
native: {
body: 'This is a Prebid Native Creative. There are many like it, but this one is mine.',
clickTrackers: ['http://my.click.tracker/url'],
icon: {
url: 'http://my.image.file/ad_image.jpg',
height: 0,
width: 0
},
image: {
url: 'http://my.icon.file/ad_icon.jpg',
height: 2250,
width: 3000
},
clickUrl: 'http://prebid.org/dev-docs/show-native-ads.html',
impressionTrackers: ['http://my.imp.tracker/url'],
javascriptTrackers: '<script src=\"http://www.foobar.js\"></script>',
title: 'This is an example Prebid Native creative'
}
};
let noImgDimBid = {
adId: 'test_bid_id',
adUnitCode: '123/prebid_native_adunit',
bidder: 'test_bidder',
native: {
body: 'This is a Prebid Native Creative. There are many like it, but this one is mine.',
clickTrackers: ['http://my.click.tracker/url'],
icon: {
url: 'http://my.image.file/ad_image.jpg',
height: 75,
width: 75
},
image: {
url: 'http://my.icon.file/ad_icon.jpg',
height: 0,
width: 0
},
clickUrl: 'http://prebid.org/dev-docs/show-native-ads.html',
impressionTrackers: ['http://my.imp.tracker/url'],
javascriptTrackers: '<script src=\"http://www.foobar.js\"></script>',
title: 'This is an example Prebid Native creative'
}
};
beforeEach(function () {});
afterEach(function () {});
it('should reject bid if no image sizes are defined', function () {
let result = nativeBidIsValid(validBid, bidReq);
expect(result).to.be.true;
result = nativeBidIsValid(noIconDimBid, bidReq);
expect(result).to.be.false;
result = nativeBidIsValid(noImgDimBid, bidReq);
expect(result).to.be.false;
});
});