|
| 1 | +/* global expect*/ |
| 2 | + |
| 3 | +describe(`web worker HTML Language tests`, function () { |
| 4 | + let worker; |
| 5 | + let messageFromWorker = null; |
| 6 | + |
| 7 | + before(async function () { |
| 8 | + worker = new Worker(`html-worker-task.js`); |
| 9 | + console.log(worker); |
| 10 | + worker.onmessage= function (event) { |
| 11 | + console.log(`From Worker:`, event); |
| 12 | + messageFromWorker = event.data; |
| 13 | + }; |
| 14 | + }); |
| 15 | + |
| 16 | + after(async function () { |
| 17 | + }); |
| 18 | + |
| 19 | + beforeEach(async function () { |
| 20 | + }); |
| 21 | + |
| 22 | + async function waitForWorkerMessage(message, timeoutMs) { |
| 23 | + let startTime = Date.now(); |
| 24 | + return new Promise((resolve)=>{ |
| 25 | + let interVal; |
| 26 | + function checkMessage() { |
| 27 | + if(messageFromWorker && messageFromWorker.type === message){ |
| 28 | + resolve(messageFromWorker); |
| 29 | + clearInterval(interVal); |
| 30 | + } |
| 31 | + let elapsedTime = Date.now() - startTime; |
| 32 | + if(elapsedTime > timeoutMs){ |
| 33 | + resolve(null); |
| 34 | + clearInterval(interVal); |
| 35 | + } |
| 36 | + } |
| 37 | + interVal = setInterval(checkMessage, 10); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + it(`Should load HTML Language Service in worker`, async function () { |
| 42 | + messageFromWorker = null; |
| 43 | + worker.postMessage({command: `workerOK`}); |
| 44 | + let output = await waitForWorkerMessage(`worker.ok`, 1000); |
| 45 | + expect(output).to.be.not.null; |
| 46 | + }); |
| 47 | + |
| 48 | + const files = ["a.html", "b.htm", "c.xhtml", "php.php"]; |
| 49 | + const fileModes = ["html", "htm", "xhtml", "php"]; |
| 50 | + for(let i=0;i<files.length; i++){ |
| 51 | + const file = files[i]; |
| 52 | + const fileMode = fileModes[i]; |
| 53 | + it(`Should getAllDocumentLinks in ${file} file`, async function () { |
| 54 | + messageFromWorker = null; |
| 55 | + const text = await (await fetch(`test-files/html-tests/${file}`)).text(); |
| 56 | + worker.postMessage({command: `getAllDocumentLinks`, text, htmlMode: fileMode, filePath: "/a.html"}); |
| 57 | + let output = await waitForWorkerMessage(`getAllDocumentLinks`, 1000); |
| 58 | + const links = output.links; |
| 59 | + expect(links).to.deep.equal(["mystyle.css","http://domain:port/style.css", |
| 60 | + "file:///path/to/style.css","sub/dir/styles.less","https://domain:port/styles.less"]); |
| 61 | + }); |
| 62 | + } |
| 63 | +}); |
0 commit comments