diff --git a/src/lib/remote.js b/src/lib/remote.js index 3611558..db4b790 100644 --- a/src/lib/remote.js +++ b/src/lib/remote.js @@ -1,7 +1,17 @@ export class VastNetworkError extends Error {} export default class Remote { - static async loadVast({ url, timeout, onBandwidthUpdate } = { timeout: 10000, onBandwidthUpdate: () => {} }) { + /** + * Fetches a remote XML Vast url. It has no knowledge of XML or the Vast structure + * + * @async + * @param {String} url - Where to download the XML + * @param {Integer} timeout - time in milleseconds to wait until for remote load + * @param {Function} onBandwidthUpdate - Callback when there is a new bandwidth estimate available, + * will be be passed a number representing KB/s + * @returns {Promise} XML Response from the url + */ + static async loadUrl({ url, timeout = 10000, onBandwidthUpdate = () => {} }) { return new Promise((resolve, reject) => { this.vastUrl = url; const request = new XMLHttpRequest(); diff --git a/src/lib/vast.js b/src/lib/vast.js index 939f76a..3fa84d1 100644 --- a/src/lib/vast.js +++ b/src/lib/vast.js @@ -146,7 +146,7 @@ export default class Vast { async loadRemoteVast(url, { timeout } = { timeout: 10000 }) { this.vastUrl = url; - const remoteVastXml = await Remote.loadVast({ + const remoteVastXml = await Remote.loadUrl({ url: url, timeout: timeout, onBandwidthUpdate: bw => { diff --git a/test/remote.spec.js b/test/remote.spec.js new file mode 100644 index 0000000..d0f4c81 --- /dev/null +++ b/test/remote.spec.js @@ -0,0 +1,34 @@ +import Remote from '../src/lib/remote'; +import mock from 'xhr-mock'; + +describe('Basic Remote Functionality', () => { + const fakeUrl = 'http://not-real.com/vast.xml'; + + beforeEach(() => { + mock.setup(); + mock.get(fakeUrl, { body: 'nope' }); + }); + afterEach(() => { + mock.teardown(); + }); + + it('should have a single static function', () => { + expect(Remote.loadUrl).toBeDefined(); + expect(typeof Remote.loadUrl).toBe('function'); + }); + + it('should be able to be used without a bandwith function', async () => { + await Remote.loadUrl({ + url: fakeUrl, + }); + }); + + it('should callback the bandwidth function after load', async () => { + const bandwidthTest = jest.fn(); + await Remote.loadUrl({ + url: fakeUrl, + onBandwidthUpdate: bandwidthTest, + }); + expect(bandwidthTest).toHaveBeenCalled(); + }); +});