@@ -2,6 +2,7 @@ import { expect } from 'chai';
22import { spec , converter } from 'modules/nativeryBidAdapter' ;
33import { newBidder } from 'src/adapters/bidderFactory.js' ;
44import * as utils from 'src/utils.js' ;
5+ import * as ajax from 'src/ajax.js' ;
56
67const ENDPOINT = 'https://hb.nativery.com/openrtb2/auction' ;
78const MAX_IMPS_PER_REQUEST = 10 ;
@@ -192,4 +193,102 @@ describe('NativeryAdapter', function () {
192193 logErrorSpy . restore ( ) ;
193194 } ) ;
194195 } ) ;
196+
197+ describe ( 'onBidWon callback' , ( ) => {
198+ it ( 'should exists and be a function' , ( ) => {
199+ expect ( spec . onBidWon ) . to . exist . and . to . be . a ( 'function' ) ;
200+ } ) ;
201+ it ( 'should NOT call ajax when invalid or empty data is provided' , ( ) => {
202+ const ajaxStub = sandBox . stub ( ajax , 'ajax' ) ;
203+ spec . onBidWon ( null ) ;
204+ spec . onBidWon ( { } ) ;
205+ spec . onBidWon ( undefined ) ;
206+ expect ( ajaxStub . called ) . to . be . false ;
207+ } ) ;
208+ it ( 'should call ajax with correct payload when valid data is provided' , ( ) => {
209+ const ajaxStub = sandBox . stub ( ajax , 'ajax' ) ;
210+ const validData = { bidder : 'nativery' , adUnitCode : 'div-1' } ;
211+ spec . onBidWon ( validData ) ;
212+ assertTrackEvent ( ajaxStub , 'NAT_BID_WON' , validData )
213+ } ) ;
214+ } ) ;
215+
216+ describe ( 'onAdRenderSucceeded callback' , ( ) => {
217+ it ( 'should exists and be a function' , ( ) => {
218+ expect ( spec . onAdRenderSucceeded ) . to . exist . and . to . be . a ( 'function' ) ;
219+ } ) ;
220+ it ( 'should NOT call ajax when invalid or empty data is provided' , ( ) => {
221+ const ajaxStub = sandBox . stub ( ajax , 'ajax' ) ;
222+ spec . onAdRenderSucceeded ( null ) ;
223+ spec . onAdRenderSucceeded ( { } ) ;
224+ spec . onAdRenderSucceeded ( undefined ) ;
225+ expect ( ajaxStub . called ) . to . be . false ;
226+ } ) ;
227+ it ( 'should call ajax with correct payload when valid data is provided' , ( ) => {
228+ const ajaxStub = sandBox . stub ( ajax , 'ajax' ) ;
229+ const validData = { bidder : 'nativery' , adUnitCode : 'div-1' } ;
230+ spec . onAdRenderSucceeded ( validData ) ;
231+ assertTrackEvent ( ajaxStub , 'NAT_AD_RENDERED' , validData )
232+ } ) ;
233+ } ) ;
234+
235+ describe ( 'onTimeout callback' , ( ) => {
236+ it ( 'should exists and be a function' , ( ) => {
237+ expect ( spec . onTimeout ) . to . exist . and . to . be . a ( 'function' ) ;
238+ } ) ;
239+ it ( 'should NOT call ajax when invalid or empty data is provided' , ( ) => {
240+ const ajaxStub = sandBox . stub ( ajax , 'ajax' ) ;
241+ spec . onTimeout ( null ) ;
242+ spec . onTimeout ( { } ) ;
243+ spec . onTimeout ( [ ] ) ;
244+ spec . onTimeout ( undefined ) ;
245+ expect ( ajaxStub . called ) . to . be . false ;
246+ } ) ;
247+ it ( 'should call ajax with correct payload when valid data is provided' , ( ) => {
248+ const ajaxStub = sandBox . stub ( ajax , 'ajax' ) ;
249+ const validData = [ { bidder : 'nativery' , adUnitCode : 'div-1' } ] ;
250+ spec . onTimeout ( validData ) ;
251+ assertTrackEvent ( ajaxStub , 'NAT_TIMEOUT' , validData )
252+ } ) ;
253+ } ) ;
254+
255+ describe ( 'onBidderError callback' , ( ) => {
256+ it ( 'should exists and be a function' , ( ) => {
257+ expect ( spec . onBidderError ) . to . exist . and . to . be . a ( 'function' ) ;
258+ } ) ;
259+ it ( 'should NOT call ajax when invalid or empty data is provided' , ( ) => {
260+ const ajaxStub = sandBox . stub ( ajax , 'ajax' ) ;
261+ spec . onBidderError ( null ) ;
262+ spec . onBidderError ( { } ) ;
263+ spec . onBidderError ( undefined ) ;
264+ expect ( ajaxStub . called ) . to . be . false ;
265+ } ) ;
266+ it ( 'should call ajax with correct payload when valid data is provided' , ( ) => {
267+ const ajaxStub = sandBox . stub ( ajax , 'ajax' ) ;
268+ const validData = {
269+ error : 'error' ,
270+ bidderRequest : {
271+ bidder : 'nativery' ,
272+ }
273+ } ;
274+ spec . onBidderError ( validData ) ;
275+ assertTrackEvent ( ajaxStub , 'NAT_BIDDER_ERROR' , validData )
276+ } ) ;
277+ } ) ;
195278} ) ;
279+
280+ const assertTrackEvent = ( ajaxStub , event , data ) => {
281+ expect ( ajaxStub . calledOnce ) . to . be . true ;
282+
283+ const [ url , callback , body , options ] = ajaxStub . firstCall . args ;
284+
285+ expect ( url ) . to . equal ( 'https://hb.nativery.com/openrtb2/track-event' ) ;
286+ expect ( callback ) . to . be . undefined ;
287+ expect ( body ) . to . be . a ( 'string' ) ;
288+ expect ( options ) . to . deep . equal ( { method : 'POST' , withCredentials : true , keepalive : true } ) ;
289+
290+ const payload = JSON . parse ( body ) ;
291+ expect ( payload . event ) . to . equal ( event ) ;
292+ expect ( payload . prebidVersion ) . to . exist . and . to . be . a ( 'string' )
293+ expect ( payload . data ) . to . deep . equal ( data ) ;
294+ }
0 commit comments