UNPKG

video-ad-sdk

Version:

VAST/VPAID SDK that allows video ads to be played on top of any player

299 lines (260 loc) 6.35 kB
import {getFirstAd} from '../../vastSelectors' import { wrapperParsedXML, inlineAd, inlineParsedXML, vastInlineXML, vastPodXML } from '../../../fixtures' import {trackLinearEvent} from '../trackLinearEvent' import { clickThrough, closeLinear, complete, firstQuartile, impression, viewable, notViewable, viewUndetermined, iconClick, iconView, fullscreen, midpoint, mute, pause, playerCollapse, playerExpand, progress, resume, rewind, skip, start, thirdQuartile, unmute, error, creativeView } from '../linearEvents' import type {VastChain} from '../../types' import {pixelTracker} from '../helpers/pixelTracker' import {trackError} from '../helpers/trackError' import {ErrorCode} from '../errorCode' jest.mock('../helpers/trackError', () => ({trackError: jest.fn()})) const vastChain: VastChain = [ { ad: inlineAd, parsedXML: inlineParsedXML, requestTag: 'https://test.example.com/vastadtaguri', XML: vastInlineXML }, { ad: getFirstAd(wrapperParsedXML), parsedXML: wrapperParsedXML, requestTag: 'http://adtag.test.example.com', XML: vastPodXML } ] afterEach(() => { jest.clearAllMocks() jest.resetAllMocks() }) test('trackLinearEvent must track the error linear event with the default pixelTracker', () => { const data = {} const errorCode = ErrorCode.UNKNOWN_ERROR trackLinearEvent(error, vastChain, { data, errorCode }) expect(trackError).toHaveBeenCalledTimes(1) expect(trackError).toHaveBeenCalledWith(vastChain, { data: {errorCode}, errorCode, tracker: pixelTracker }) }) test('trackLinearEvent must be possible to pass a custom tracker to the linear trackers', () => { const data = {} const customTracker: any = (): void => {} trackLinearEvent(error, vastChain, { data, tracker: customTracker }) expect(trackError).toHaveBeenCalledTimes(1) expect(trackError).toHaveBeenCalledWith(vastChain, { data, errorCode: undefined, tracker: customTracker }) }) test("trackLinearEvent must log an error if the the event can't be tracked", () => { const data = {} const logger: any = { error: jest.fn() } trackLinearEvent('wrongEvent' as any, vastChain, { data, logger }) expect(logger.error).toHaveBeenCalledWith( "Event 'wrongEvent' cannot be tracked" ) }) test(`trackLinearEvent must track ${clickThrough} linear event with the default pixelTracker`, () => { const data = {} const tracker = jest.fn() trackLinearEvent(clickThrough, vastChain, { data, tracker }) expect(tracker).toHaveBeenCalledTimes(4) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/customclick', {} ) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/clicktracking', {} ) }) ;[ start, closeLinear, complete, firstQuartile, midpoint, playerCollapse, playerExpand, thirdQuartile, mute, unmute, rewind, pause, resume, fullscreen, skip, creativeView ].forEach((event) => { test(`trackLinearEvent must track ${event} linear event with the default pixelTracker`, () => { const data = {} const tracker = jest.fn() trackLinearEvent(event as any, vastChain, { data, tracker }) expect(tracker).toHaveBeenCalledWith( `https://test.example.com/${event}`, {} ) expect(tracker).toHaveBeenCalledWith( `https://test.example.com/${event}2`, {} ) }) }) test('trackLinearEvent must track impression linear event with the default pixelTracker', () => { const data = {} const tracker = jest.fn() trackLinearEvent(impression, vastChain, { data, tracker }) expect(tracker).toHaveBeenCalledTimes(2) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/impression', {} ) }) test('trackLinearEvent must track viewable linear event with the default pixelTracker', () => { const data = {} const tracker = jest.fn() trackLinearEvent(viewable, vastChain, { data, tracker }) expect(tracker).toHaveBeenCalledTimes(1) expect(tracker).toHaveBeenCalledWith('https://test.example.com/viewable', {}) }) test('trackLinearEvent must track notViewable linear event with the default pixelTracker', () => { const data = {} const tracker = jest.fn() trackLinearEvent(notViewable, vastChain, { data, tracker }) expect(tracker).toHaveBeenCalledTimes(1) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/notViewable', {} ) }) test('trackLinearEvent must track viewUndetermined linear event with the default pixelTracker', () => { const data = {} const tracker = jest.fn() trackLinearEvent(viewUndetermined, vastChain, { data, tracker }) expect(tracker).toHaveBeenCalledTimes(1) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/notDetermined', {} ) }) test('trackLinearEvent must track iconClicks', () => { const data = { iconClickTracking: [ 'https://test.example.com/iconClick', 'https://test.example.com/iconClick2' ] } const tracker = jest.fn() trackLinearEvent(iconClick, vastChain, { data, tracker }) expect(tracker).toHaveBeenCalledTimes(2) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/iconClick', data ) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/iconClick2', data ) }) test('trackLinearEvent must track iconViews', () => { const data = { iconViewTracking: [ 'https://test.example.com/iconView', 'https://test.example.com/iconView2' ] } const tracker = jest.fn() trackLinearEvent(iconView, vastChain, { data, tracker }) expect(tracker).toHaveBeenCalledTimes(2) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/iconView', data ) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/iconView2', data ) }) test('trackLinearEvent must track progress', () => { const data = { progressUri: 'https://test.example.com/progress' } const tracker = jest.fn() trackLinearEvent(progress, vastChain, { data, tracker }) expect(tracker).toHaveBeenCalledTimes(1) expect(tracker).toHaveBeenCalledWith( 'https://test.example.com/progress', data ) })