Skip to content

Commit 15010f8

Browse files
committed
Add createElement and JSX tests
1 parent fc4d360 commit 15010f8

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/element.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
export function createElement(type, config, ...args) {}
1+
const TEXT_ELEMENT = "TEXT ELEMENT";
2+
3+
export function createElement(type, config, ...args) {
4+
const props = Object.assign({}, config);
5+
const hasChildren = args.length > 0;
6+
const rawChildren = hasChildren ? [].concat(...args) : [];
7+
props.children = rawChildren
8+
.filter(c => c != null && c !== false)
9+
.map(c => c instanceof Object ? c : createTextElement(c));
10+
return { type, props };
11+
}
12+
13+
function createTextElement(value) {
14+
return createElement(TEXT_ELEMENT, { nodeValue: value });
15+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import test from "ava";
2+
import browserEnv from "browser-env";
3+
/** @jsx createElement */
4+
import { render, createElement } from "../src/didact";
5+
6+
// Create document global var
7+
browserEnv(["document"]);
8+
9+
test.beforeEach(t => {
10+
const root = document.createElement("div");
11+
document.body.appendChild(root);
12+
t.context.root = root;
13+
});
14+
15+
test.afterEach.always(t => {
16+
const root = t.context.root;
17+
root.innerHTML = "";
18+
document.body.removeChild(root);
19+
});
20+
21+
test("render jsx div", t => {
22+
const root = t.context.root;
23+
const element = <div />;
24+
render(element, root);
25+
t.is(root.innerHTML, "<div></div>");
26+
});
27+
28+
test("render div with children", t => {
29+
const root = t.context.root;
30+
const element = <div><b /><a href="foo" /></div>;
31+
render(element, root);
32+
t.is(root.innerHTML, '<div><b></b><a href="foo"></a></div>');
33+
});
34+
35+
test("render div with props", t => {
36+
const root = t.context.root;
37+
const element = <div id="foo" />;
38+
render(element, root);
39+
t.is(root.innerHTML, '<div id="foo"></div>');
40+
});
41+
42+
test("render span with text child", t => {
43+
const root = t.context.root;
44+
const element = <span>Foo</span>;
45+
render(element, root);
46+
t.is(root.innerHTML, "<span>Foo</span>");
47+
});

0 commit comments

Comments
 (0)