From 946710f2e9960b3839613d4bdf730e57ba38a964 Mon Sep 17 00:00:00 2001 From: Alex Pashkov Date: Mon, 9 Sep 2019 16:22:29 +0300 Subject: [PATCH] Add unit test --- modules/viBidAdapter.js | 9 +++++---- test/spec/modules/viBidAdapter_spec.js | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/modules/viBidAdapter.js b/modules/viBidAdapter.js index f3093bc640f6..8d8cb06e47e4 100644 --- a/modules/viBidAdapter.js +++ b/modules/viBidAdapter.js @@ -302,6 +302,10 @@ export function mergeArrays(hashFn, ...args) { return merged; } +export function documentFocus(doc) { + return typeof doc.hasFocus === 'function' ? +doc.hasFocus() : undefined; +} + const spec = { code: 'vi', supportedMediaTypes: [mediaTypes.VIDEO, mediaTypes.BANNER], @@ -373,10 +377,7 @@ const spec = { }; } ), - focus: - typeof document.hasFocus === 'function' - ? +document.hasFocus() - : undefined + focus: documentFocus(document) }, options: { contentType: 'application/json', diff --git a/test/spec/modules/viBidAdapter_spec.js b/test/spec/modules/viBidAdapter_spec.js index b12d534ff024..8f1c07047ec9 100644 --- a/test/spec/modules/viBidAdapter_spec.js +++ b/test/spec/modules/viBidAdapter_spec.js @@ -23,7 +23,8 @@ import { area, get, getViewabilityDescription, - mergeArrays + mergeArrays, + documentFocus } from 'modules/viBidAdapter'; describe('ratioToPercentageCeil', () => { @@ -892,3 +893,19 @@ describe('mergeSizes', () => { ).to.deep.equal([[1, 2], [2, 4], [400, 500], [500, 600]]); }); }); + +describe('documentFocus', () => { + it('calls hasFocus function if it present, converting boolean to an int 0/1 value, returns undefined otherwise', () => { + expect( + documentFocus({ + hasFocus: () => true + }) + ).to.equal(1); + expect( + documentFocus({ + hasFocus: () => false + }) + ).to.equal(0); + expect(documentFocus({})).to.be.undefined; + }); +});