Skip to content

Commit

Permalink
remote has its own tests now
Browse files Browse the repository at this point in the history
  • Loading branch information
banderson623 committed Mar 10, 2019
1 parent 95dc4c4 commit 84637bc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/lib/remote.js
Original file line number Diff line number Diff line change
@@ -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<String>} XML Response from the url
*/
static async loadUrl({ url, timeout = 10000, onBandwidthUpdate = () => {} }) {
return new Promise((resolve, reject) => {
this.vastUrl = url;
const request = new XMLHttpRequest();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/vast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
34 changes: 34 additions & 0 deletions test/remote.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 84637bc

Please sign in to comment.