Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit fe2a259

Browse files
committed
Complete test coverage for message handlers.
1 parent 697ddaa commit fe2a259

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

SpecRunner.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<p id="testParaID">This is a test paragraph, with an id.</p>
2424
<p class="testParaClass">This is a test paragrapch, with a class.</p>
2525
<button id="testButton">Click Me</button>
26+
<button id="testButton2">Click Me Again</button>
2627
<div id="testMutate"></div>
2728
</div>
2829
</body>

polyplug.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ const polyplug = function() {
472472
}
473473
*/
474474
const polyplugStdout = new CustomEvent("polyplugStdout", {detail: msg.content});
475+
document.dispatchEvent(polyplugStdout);
475476
}
476477

477478
function onStderr(msg) {
@@ -489,6 +490,7 @@ const polyplug = function() {
489490
}
490491
*/
491492
const polyplugStderr = new CustomEvent("polyplugStderr", {detail: msg.content});
493+
document.dispatchEvent(polyplugStderr);
492494
}
493495

494496
function onError(msg) {
@@ -508,7 +510,8 @@ const polyplug = function() {
508510
}
509511
}
510512
*/
511-
const polyplugError = new CustomEvent("polyplugError", {detail: msg});
513+
const polyplugError = new CustomEvent("polyplugError", {detail: msg.context});
514+
document.dispatchEvent(polyplugError);
512515
}
513516

514517
return {

spec/polyplugSpec.js

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ describe("When working with PolyPlug,", function() {
281281
});
282282
describe("when registering events in the DOM,", function() {
283283
it("raises the expected polyplugSend event with the event context", function(done) {
284-
document.addEventListener("polyplugSend", function(e) {
284+
function eventListener(e) {
285285
const detail = JSON.parse(e.detail);
286286
expect(detail.type).toEqual("click");
287287
expect(detail.listener).toEqual("myTestClicker");
@@ -300,12 +300,14 @@ describe("When working with PolyPlug,", function() {
300300
}
301301
]
302302
});
303-
done()
304-
})
303+
done();
304+
}
305+
document.addEventListener("polyplugSend", eventListener);
305306
plug.registerEvent({id: "testButton"}, "click", "myTestClicker");
306307
const button = plug.getElements({id: "testButton"})[0];
307308
const clickEvent = new Event("click");
308309
button.dispatchEvent(clickEvent);
310+
document.removeEventListener("polyplugSend", eventListener);
309311
});
310312
});
311313
describe("when handling incoming messages,", function() {
@@ -349,5 +351,87 @@ describe("When working with PolyPlug,", function() {
349351
const expected = JSON.stringify(target);
350352
expect(actual).toEqual(expected);
351353
});
354+
it("the registerEvent message registers an event", function(done) {
355+
const msg = JSON.stringify({
356+
type: "registerEvent",
357+
query: {
358+
id: "testButton2"
359+
},
360+
eventType: "click",
361+
listener: "my_on_click_function"
362+
});
363+
plug.receiveMessage(msg);
364+
function eventListener(e) {
365+
const detail = JSON.parse(e.detail);
366+
expect(detail.type).toEqual("click");
367+
expect(detail.listener).toEqual("my_on_click_function");
368+
expect(detail.target).toEqual({
369+
"nodeType": 1,
370+
"tagName": "button",
371+
"attributes": {
372+
"id": "testButton2"
373+
},
374+
"childNodes": [
375+
{
376+
"nodeType": 3,
377+
"nodeName": "#text",
378+
"nodeValue": "Click Me Again",
379+
"childNodes": []
380+
}
381+
]
382+
});
383+
done();
384+
}
385+
document.addEventListener("polyplugSend", eventListener);
386+
const button = plug.getElements({id: "testButton2"})[0];
387+
const clickEvent = new Event("click");
388+
button.dispatchEvent(clickEvent);
389+
document.removeEventListener("polyplugSend", eventListener);
390+
});
391+
it("the stdout message dispatches a polyplugStdout event", function(done) {
392+
function eventListener(e) {
393+
expect(e.detail).toEqual("Hello, world");
394+
done();
395+
}
396+
document.addEventListener("polyplugStdout", eventListener);
397+
const msg = JSON.stringify({
398+
type: "stdout",
399+
content: "Hello, world"
400+
});
401+
plug.receiveMessage(msg);
402+
document.removeEventListener("polyplugStdout", eventListener);
403+
});
404+
it("the stderr message dispatches a polyplugStdout event", function(done) {
405+
function eventListener(e) {
406+
expect(e.detail).toEqual("Hello, stderr");
407+
done();
408+
}
409+
document.addEventListener("polyplugStderr", eventListener);
410+
const msg = JSON.stringify({
411+
type: "stderr",
412+
content: "Hello, stderr"
413+
});
414+
plug.receiveMessage(msg);
415+
document.removeEventListener("polyplugStderr", eventListener);
416+
});
417+
it("the error message dispatches a polyplugErrorevent", function(done) {
418+
// Arbitrary error context from the remote interpreter.
419+
const errorContext = {
420+
exception: "ValueError",
421+
message: "The thing went bang!",
422+
stackTrace: ["frame1", "frame2", "frame3" ]
423+
}
424+
function eventListener(e) {
425+
expect(e.detail).toEqual(errorContext);
426+
done();
427+
}
428+
document.addEventListener("polyplugError", eventListener);
429+
const msg = JSON.stringify({
430+
type: "error",
431+
context: errorContext
432+
});
433+
plug.receiveMessage(msg);
434+
document.removeEventListener("polyplugError", eventListener);
435+
});
352436
});
353437
});

0 commit comments

Comments
 (0)