From 3fd0abe57ff85c5e8b5e4fc59217024ecf9f4241 Mon Sep 17 00:00:00 2001 From: Jesse Ditson Date: Thu, 12 Sep 2024 16:43:42 -0700 Subject: [PATCH] update symlink, rename unit tests --- .../fixtures/todo-mvc-app/node_modules | 2 +- .../{view-setup.spec.ts => tests.spec.ts} | 60 ++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) rename tests/unit/{view-setup.spec.ts => tests.spec.ts} (68%) diff --git a/tests/integration/fixtures/todo-mvc-app/node_modules b/tests/integration/fixtures/todo-mvc-app/node_modules index 6c57164..561d794 120000 --- a/tests/integration/fixtures/todo-mvc-app/node_modules +++ b/tests/integration/fixtures/todo-mvc-app/node_modules @@ -1 +1 @@ -../../../node_modules \ No newline at end of file +../../../../node_modules \ No newline at end of file diff --git a/tests/unit/view-setup.spec.ts b/tests/unit/tests.spec.ts similarity index 68% rename from tests/unit/view-setup.spec.ts rename to tests/unit/tests.spec.ts index c9bb7cc..e96fd0d 100644 --- a/tests/unit/view-setup.spec.ts +++ b/tests/unit/tests.spec.ts @@ -1,7 +1,8 @@ -import test, { before, describe } from "node:test"; +import test, { after, afterEach, before, describe } from "node:test"; import assert from "node:assert/strict"; import { JSDOM } from "jsdom"; import { cre8, Upd8View } from "../../src/upd8"; +import { rejects, throws } from "node:assert"; class TestMultiIDView extends Upd8View<{}, {}> { static get id() { @@ -19,7 +20,12 @@ class TestMultiIDView extends Upd8View<{}, {}> { } } -test("it chooses the outer-most of duplicate IDs when showing views", () => { +afterEach(async () => { + // Pause for gc - otherwise old instances will continue to respond to event listeners. + await new Promise((resolve) => setTimeout(resolve, 0)); +}); + +test("it chooses the outer-most of duplicate IDs when showing views", async () => { let dom = new JSDOM( `
` ); @@ -46,6 +52,8 @@ test("it chooses the outer-most of duplicate IDs when showing views", () => { (v) => v.checkView() ); assert.equal(correct, "yes"); + initUI.teardown(); + dom.window.close(); }); describe("setContent", () => { @@ -85,6 +93,10 @@ describe("setContent", () => { }); upd8 = initUI({}, (event) => {}); }); + after(() => { + initUI.teardown(); + dom.window.close(); + }); test("setContent without a selector returns one element", () => { const val = initUI.imperative( SetContentTest.id, @@ -106,3 +118,47 @@ describe("setContent", () => { }); }); }); + +describe("mount", () => { + class InitError extends Upd8View<{}, {}> { + static get id() { + return "ie"; + } + + get id() { + return InitError.id; + } + + mount(): Function[] { + return [this.eventListener("not-exist", "click", () => {})]; + } + } + type RTC = ReturnType>; + let dom: JSDOM, initUI: RTC, upd8: ReturnType; + before(() => { + dom = new JSDOM( + ` +
+ ` + ); + initUI = cre8([InitError], { + document: dom.window.document, + }); + upd8 = initUI({}, (event) => {}); + }); + after(() => { + initUI.teardown(); + dom.window.close(); + }); + test("it emits a helpful error message when a listener fails to mount", async () => { + rejects( + async () => { + await upd8({}); + }, + { + message: + '[ie] eventListener failed: couldn\'t find element with id "not-exist" for click in
', + } + ); + }); +});