From 97fc7b03a1517a3093a00d2c40f46b102ca7fe2f Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Wed, 23 Oct 2024 16:28:35 -0400 Subject: [PATCH] tests: Add tests for re-entrant mounts --- tests/array.test.js | 21 ++++++++++- tests/custom_element.test.js | 68 ++++++++++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/tests/array.test.js b/tests/array.test.js index d293301..019188c 100644 --- a/tests/array.test.js +++ b/tests/array.test.js @@ -256,7 +256,7 @@ test("array modify from mount", () => { name: 'body', children: ['1', '2', '3'] }); - assert(count === 2) + assert(count === 2); }); test("replace empty oarray", () => { @@ -310,3 +310,22 @@ test("each double up", () => { children: ['content', 'content'] }); }); + +test("unmount from custom component", () => { + const elem = document.createElement("body"); + const arr = OArray(); + + mount(elem, arr); + + const Comp = () => { + arr.splice(0); + + return "hello world"; + }; + + arr.push(h(Comp)); + + assert.deepEqual(elem.tree(), { + name: 'body', + }); +}); diff --git a/tests/custom_element.test.js b/tests/custom_element.test.js index 6457eae..2ddf6bf 100644 --- a/tests/custom_element.test.js +++ b/tests/custom_element.test.js @@ -2,7 +2,7 @@ import {test} from 'node:test'; import assert from 'node:assert'; import './document.js'; -import {mount, h} from '../index.js'; +import {mount, h, Observer} from '../index.js'; const silence = (cb) => () => { const orig = console.error; @@ -167,7 +167,6 @@ test("custom element mounted tree", () => { assert(mounted); }); - test("custom element children", () => { const elem = document.createElement("body"); @@ -262,3 +261,68 @@ test("Pass null children for auto closed component", () => { assert.deepEqual(passed, []); }); + +test("Remove custom component while mounting", () => { + const Component = () => { + comp.set(null); + + return "hello world"; + }; + + const comp = Observer.mutable(h(Component)); + + const elem = document.createElement("body"); + mount(elem, comp); + + assert.deepEqual(elem.tree(), { + name: 'body', + }); +}); + +test("Remove custom component while mounting recursive", () => { + const Component2 = () => { + comp2.set(null); + + return "hello world"; + }; + const comp2 = Observer.mutable(h(Component2)); + + const Component = () => { + comp.set(null); + + return comp2; + }; + + const comp = Observer.mutable(h(Component)); + + const elem = document.createElement("body"); + mount(elem, comp); + + assert.deepEqual(elem.tree(), { + name: 'body', + }); +}); + +test("Remove custom component while mounting recursive invert", () => { + const Component2 = () => { + comp.set(null); + + return "hello world"; + }; + const comp2 = Observer.mutable(h(Component2)); + + const Component = () => { + comp2.set(null); + + return comp2; + }; + + const comp = Observer.mutable(h(Component)); + + const elem = document.createElement("body"); + mount(elem, comp); + + assert.deepEqual(elem.tree(), { + name: 'body', + }); +});