Skip to content

Commit

Permalink
tests: Add tests for re-entrant mounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Nefsen402 committed Oct 23, 2024
1 parent 1e6d735 commit 97fc7b0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
21 changes: 20 additions & 1 deletion tests/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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',
});
});
68 changes: 66 additions & 2 deletions tests/custom_element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,7 +167,6 @@ test("custom element mounted tree", () => {
assert(mounted);
});


test("custom element children", () => {
const elem = document.createElement("body");

Expand Down Expand Up @@ -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',
});
});

0 comments on commit 97fc7b0

Please sign in to comment.