-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathnative_spec.js
105 lines (96 loc) · 3.23 KB
/
native_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
import {fillNativeImp, fillNativeResponse} from '../../../libraries/ortbConverter/processors/native.js';
import {BANNER, NATIVE} from '../../../src/mediaTypes.js';
describe('pbjs -> ortb native requests', () => {
function toNative(bidRequest, context) {
const imp = {};
fillNativeImp(imp, bidRequest, context);
return imp;
}
it('should do nothing if request has no nativeOrtbRequest', () => {
expect(toNative({}, {})).to.eql({});
});
it('should set imp.native according to nativeOrtbRequest', () => {
const nativeOrtbRequest = {ver: 'version', prop: 'value', assets: [{}]};
const imp = toNative({nativeOrtbRequest}, {});
expect(imp.native.ver).to.eql('version');
expect(JSON.parse(imp.native.request)).to.eql(nativeOrtbRequest);
});
it('should do nothing if context.mediaType is set but is not NATIVE', () => {
expect(toNative({nativeOrtbRequest: {ver: 'version'}}, {mediaType: BANNER})).to.eql({})
});
it('should merge context.nativeRequest', () => {
const nativeOrtbRequest = {ver: 'version', eventtrackers: [{tracker: 'req'}], assets: [{}]};
const nativeDefaults = {eventtrackers: [{tracker: 'default'}], other: 'other'};
const imp = toNative({nativeOrtbRequest}, {nativeRequest: nativeDefaults});
expect(imp.native.ver).to.eql('version');
expect(JSON.parse(imp.native.request)).to.eql({
assets: [{}],
ver: 'version',
eventtrackers: [{tracker: 'req'}],
other: 'other'
});
});
it('should keep ortb2Imp.native', () => {
const imp = {
native: {
something: 'orother'
}
}
fillNativeImp(imp, {nativeOrtbRequest: {ver: 'version'}}, {});
expect(imp.native.something).to.eql('orother')
});
it('should keep ortb2Imp.native.battr', () => {
const imp = {
native: {
battr: 'battr'
}
};
fillNativeImp(imp, {mediaTypes: {native: {sizes: [1, 2]}}}, {});
expect(imp.native.battr).to.eql('battr');
});
it('should do nothing if there are no assets', () => {
const imp = {};
fillNativeImp(imp, {nativeOrtbRequest: {assets: []}}, {});
expect(imp).to.eql({});
})
});
describe('ortb -> ortb native response', () => {
const MOCK_NATIVE_RESPONSE = {
property: 'value',
assets: [
{
id: 0
}
]
}
Object.entries({
'serialized': JSON.stringify(MOCK_NATIVE_RESPONSE),
'an object': MOCK_NATIVE_RESPONSE
}).forEach(([t, adm]) => {
describe(`when adm is ${t}`, () => {
let bid;
beforeEach(() => {
bid = {adm};
})
it('should set bidResponse.native', () => {
const bidResponse = {
mediaType: NATIVE
};
fillNativeResponse(bidResponse, bid, {});
expect(bidResponse.native).to.eql({ortb: MOCK_NATIVE_RESPONSE});
});
});
it('should throw if response has no assets', () => {
expect(() => fillNativeResponse({mediaType: NATIVE}, {adm: {...MOCK_NATIVE_RESPONSE, assets: null}}, {})).to.throw;
})
it('should do nothing if bidResponse.mediaType is not NATIVE', () => {
const bidResponse = {
mediaType: BANNER
};
fillNativeResponse(bidResponse, {adm: MOCK_NATIVE_RESPONSE}, {});
expect(bidResponse).to.eql({
mediaType: BANNER
})
})
})
});