Commit e84f29cd authored by Tom Jorquera's avatar Tom Jorquera
Browse files

client: Add new controller script to load 3rd party scripts

Add `client/external.js` which load third-party scripts passed into the
`externalLibs` array of the client config object.
parent 75ca2134
// Load third-parties libraries given in config
/* global robotController document */
robotController.external = {
load: config => {
for (const url of config.externalLibs) {
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
document.head.appendChild(script);
}
}
};
'use strict';
/* global Object document */
Object.defineProperty(document, 'appendedElements', {
value: []
});
Object.defineProperty(document, 'createElement', {
value: type => {
const res = {
type,
setAttribute: (k, v) => {
res[k] = v;
}
};
return res;
}
});
Object.defineProperty(document, 'head', {
value: {
appendChild: e => {
document.appendedElements.push(e);
}
}
});
describe('client/controller/external', () => {
beforeEach(() => {
global.robotController = {};
/* eslint-disable import/no-unassigned-import */
require('./external.js');
/* eslint-enable */
});
afterEach(() => {
jest.resetModules();
});
test('should add itself to controller', () => {
expect(global.robotController.external).toBeDefined();
});
test('should load external libs in config', () => {
global.robotController.external.load({
externalLibs: ['dep1', 'dep2', 'dep3']
});
console.log(document);
expect(document.appendedElements).toHaveLength(3);
expect(document.appendedElements[0]).toEqual(expect.objectContaining({
type: 'text/javascript',
src: 'dep1'
}));
expect(document.appendedElements[1]).toEqual(expect.objectContaining({
type: 'text/javascript',
src: 'dep2'
}));
expect(document.appendedElements[2]).toEqual(expect.objectContaining({
type: 'text/javascript',
src: 'dep3'
}));
});
});
......@@ -18,6 +18,7 @@
},
"client": {
"name": "hublot",
"gstreamerURL": "ws://localhost:8888"
"gstreamerURL": "ws://localhost:8888",
"externalLibs": []
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment