Hacky way to test LMLayerWorker as if it's inside a Worker.

This commit is contained in:
Eddie Antonio Santos 2018-11-15 15:12:14 -07:00
parent 538393930d
commit 5200b665a1
No known key found for this signature in database
GPG key ID: DD411EA4D9509D87

View file

@ -18,10 +18,31 @@ describe('LMLayerWorker', function() {
});
describe('#constructor()', function() {
it.skip('should construct with zero arguments', function() {
// TODO: This is broken, because the LMLayerWorker needs
// to believe it's in a DedicatedWorkerGlobalScope.
assert.isOk(new LMLayerWorker);
it('should construct with zero arguments', function() {
// Create a new "sandboxed" LMLayer. It's only "sandboxed", in that it believes it's in a WebWorker.
var fs = require('fs');
var sourceCode = fs.readFileSync(require.resolve('../../worker'));
var sandbox = `(function (self) {
var exports = {}; // TypeScript CommonJS code generation will attempt to write to this.
var postMessage = self.postMessage;
${sourceCode}
return LMLayerWorker;
})`;
var createSandbox = eval(sandbox);
var fakeSelf = {
postMessage: sinon.fake()
};
var WorkerInSandbox = createSandbox(fakeSelf);
let worker = new WorkerInSandbox();
assert.isOk(worker);
console.log(sandbox);
// Now try it out.
worker.onMessage({
message: 'initialize',
model: 'en-x-dummy'
});
assert(fakeSelf.postMessage.calledOnce);
});
});
});