forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_spec.js
214 lines (172 loc) · 6.17 KB
/
config_spec.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { expect } from 'chai';
import { assert } from 'chai';
import { newConfig } from 'src/config.js';
const utils = require('src/utils');
let getConfig;
let setConfig;
let setDefaults;
describe('config API', function () {
let logErrorSpy;
let logWarnSpy;
beforeEach(function () {
const config = newConfig();
getConfig = config.getConfig;
setConfig = config.setConfig;
setDefaults = config.setDefaults;
logErrorSpy = sinon.spy(utils, 'logError');
logWarnSpy = sinon.spy(utils, 'logWarn');
});
afterEach(function () {
utils.logError.restore();
utils.logWarn.restore();
});
it('setConfig is a function', function () {
expect(setConfig).to.be.a('function');
});
it('getConfig returns an object', function () {
expect(getConfig()).to.be.a('object');
});
it('sets and gets arbitrary configuration properties', function () {
setConfig({ baz: 'qux' });
expect(getConfig('baz')).to.equal('qux');
});
it('only accepts objects', function () {
setConfig('invalid');
expect(getConfig('0')).to.not.equal('i');
});
it('sets multiple config properties', function () {
setConfig({ foo: 'bar' });
setConfig({ biz: 'buz' });
var config = getConfig();
expect(config.foo).to.equal('bar');
expect(config.biz).to.equal('buz');
});
it('overwrites existing config properties', function () {
setConfig({ foo: {biz: 'buz'} });
setConfig({ foo: {baz: 'qux'} });
expect(getConfig('foo')).to.eql({baz: 'qux'});
});
it('sets debugging', function () {
setConfig({ debug: true });
expect(getConfig('debug')).to.be.true;
});
it('sets bidderTimeout', function () {
setConfig({ bidderTimeout: 1000 });
expect(getConfig('bidderTimeout')).to.be.equal(1000);
});
it('gets user-defined publisherDomain', function () {
setConfig({ publisherDomain: 'fc.kahuna' });
expect(getConfig('publisherDomain')).to.equal('fc.kahuna');
});
it('gets default userSync config', function () {
const DEFAULT_USERSYNC = {
syncEnabled: true,
pixelEnabled: true,
syncsPerBidder: 5,
syncDelay: 3000,
auctionDelay: 0
};
setDefaults({'userSync': DEFAULT_USERSYNC});
expect(getConfig('userSync')).to.eql(DEFAULT_USERSYNC);
});
it('has subscribe functionality for adding listeners to config updates', function () {
const listener = sinon.spy();
getConfig(listener);
setConfig({ foo: 'bar' });
sinon.assert.calledOnce(listener);
sinon.assert.calledWith(listener, { foo: 'bar' });
});
it('subscribers can unsubscribe', function () {
const listener = sinon.spy();
const unsubscribe = getConfig(listener);
unsubscribe();
setConfig({ logging: true });
sinon.assert.notCalled(listener);
});
it('subscribers can subscribe to topics', function () {
const listener = sinon.spy();
getConfig('logging', listener);
setConfig({ logging: true, foo: 'bar' });
sinon.assert.calledOnce(listener);
sinon.assert.calledWithExactly(listener, { logging: true });
});
it('topic subscribers are only called when that topic is changed', function () {
const listener = sinon.spy();
const wildcard = sinon.spy();
getConfig('subject', listener);
getConfig(wildcard);
setConfig({ foo: 'bar' });
sinon.assert.notCalled(listener);
sinon.assert.calledOnce(wildcard);
});
it('sets priceGranularity', function () {
setConfig({ priceGranularity: 'low' });
expect(getConfig('priceGranularity')).to.be.equal('low');
});
it('set mediaTypePriceGranularity', function () {
const customPriceGranularityVideo = {
'buckets': [{
'max': 3,
'increment': 0.01,
'cap': true
}]
};
const customPriceGranularityInstream = utils.deepClone(customPriceGranularityVideo);
const customPriceGranularityOutstream = utils.deepClone(customPriceGranularityVideo);
setConfig({
'mediaTypePriceGranularity': {
'banner': 'medium',
'video': customPriceGranularityVideo,
'video-instream': customPriceGranularityInstream,
'video-outstream': customPriceGranularityOutstream,
'native': 'high'
}
});
const configResult = getConfig('mediaTypePriceGranularity');
expect(configResult.banner).to.be.equal('medium');
expect(configResult.video).to.be.equal(customPriceGranularityVideo);
expect(configResult['video-instream']).to.be.equal(customPriceGranularityInstream);
expect(configResult['video-outstream']).to.be.equal(customPriceGranularityOutstream);
expect(configResult.native).to.be.equal('high');
});
it('sets priceGranularity and customPriceBucket', function () {
const goodConfig = {
'buckets': [{
'max': 3,
'increment': 0.01,
'cap': true
}]
};
setConfig({ priceGranularity: goodConfig });
expect(getConfig('priceGranularity')).to.be.equal('custom');
expect(getConfig('customPriceBucket')).to.equal(goodConfig);
});
it('sets deviceAccess', function () {
// When the deviceAccess flag config option is not set, cookies may be read and set
expect(getConfig('deviceAccess')).to.be.equal(true);
// When the deviceAccess flag config option is set to false, no cookies are read or set
setConfig({
'deviceAccess': false
});
expect(getConfig('deviceAccess')).to.be.equal(false);
// When the deviceAccess flag config option is set to true, cookies may be read and set
setConfig({
'deviceAccess': true
});
expect(getConfig('deviceAccess')).to.be.equal(true);
});
it('should log error for invalid priceGranularity', function () {
setConfig({ priceGranularity: '' });
const error = 'Prebid Error: no value passed to `setPriceGranularity()`';
assert.ok(logErrorSpy.calledWith(error), 'expected error was logged');
});
it('should log a warning on invalid values', function () {
setConfig({ bidderSequence: 'unrecognized sequence' });
expect(logWarnSpy.calledOnce).to.equal(true);
});
it('should not log warnings when given recognized values', function () {
setConfig({ bidderSequence: 'fixed' });
setConfig({ bidderSequence: 'random' });
expect(logWarnSpy.called).to.equal(false);
});
});