From 5200b665a14a8a08afea6a993f10eecc5f44e73d Mon Sep 17 00:00:00 2001 From: Eddie Antonio Santos Date: Thu, 15 Nov 2018 15:12:14 -0700 Subject: [PATCH] Hacky way to test LMLayerWorker as if it's inside a Worker. --- .../headless/worker-intialization.js | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/common/predictive-text/unit_tests/headless/worker-intialization.js b/common/predictive-text/unit_tests/headless/worker-intialization.js index 1ccf4ae798..e766a85c57 100644 --- a/common/predictive-text/unit_tests/headless/worker-intialization.js +++ b/common/predictive-text/unit_tests/headless/worker-intialization.js @@ -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); }); }); });