Skip to content

Commit

Permalink
tests: Add tests for duck typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nefsen402 committed Dec 23, 2024
1 parent 55d0b74 commit d0282c3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
85 changes: 85 additions & 0 deletions tests/duck_typing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {test} from 'node:test';
import assert from 'node:assert';
import './document.js';

import {mount, OArray, Observer} from '../index.js';

const createCustomNode = () => {
let elems = [];
let obj = {
elems,
insertBefore: (elem, before) => {
const i = elems.indexOf(before);
if (i === -1) {
elems.push(elem);
} else {
elems.splice(i, 0, elem);
}
},
replaceChild: (newNode, oldNode) => {
const i = elems.indexOf(oldNode);
elems.splice(i, 1, newNode);
},
removeChild: (node) => {
const i = elems.indexOf(node);
elems.splice(i, 1);
},
set textContent (content) {
elems.splice(0, elems.length);
obj.fastClear = true;
},
get firstChild () {
return elems[0];
},
};

return obj;
};

test("duck type basic", () => {
const elem = createCustomNode();

mount(elem, "hello world");
assert.deepEqual(elem.elems.map(e => e.tree()), ["hello world"]);
});

test("duck type basic remove", () => {
const elem = createCustomNode();

const remove = mount(elem, "hello world");
remove();

assert.deepEqual(elem.elems.map(e => e.tree()), []);
});

test("duck type replace", () => {
const elem = createCustomNode();

const obs = Observer.mutable("hello world");
mount(elem, obs);
obs.set("new");

assert.deepEqual(elem.elems.map(e => e.tree()), ["new"]);
});

test("duck type array clear", () => {
const elem = createCustomNode();

const obs = OArray([1, 2, 3]);
mount(elem, obs);
obs.splice(0, obs.length);

assert.deepEqual(elem.elems.map(e => e.tree()), []);
assert(elem.fastClear === true)
});

test("duck type array nested clear", () => {
const elem = createCustomNode();

const obs = OArray([1, 2, 3]);
mount(elem, OArray([obs]));
obs.splice(0, obs.length);

assert.deepEqual(elem.elems.map(e => e.tree()), []);
assert(elem.fastClear === true)
});
1 change: 1 addition & 0 deletions tests/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ for (const file of files) {
'array.reconciliation.test.js',
'replacement.test.js',
'static.test.js',
'duck_typing.test.js',
].includes(file)) {
describe("iife " + file, transform(file, {
assure_import: /^$/,
Expand Down

0 comments on commit d0282c3

Please sign in to comment.