-
-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
export function createElement(type, config, ...args) {} | ||
const TEXT_ELEMENT = "TEXT ELEMENT"; | ||
|
||
export function createElement(type, config, ...args) { | ||
const props = Object.assign({}, config); | ||
const hasChildren = args.length > 0; | ||
const rawChildren = hasChildren ? [].concat(...args) : []; | ||
props.children = rawChildren | ||
.filter(c => c != null && c !== false) | ||
.map(c => c instanceof Object ? c : createTextElement(c)); | ||
return { type, props }; | ||
} | ||
|
||
function createTextElement(value) { | ||
return createElement(TEXT_ELEMENT, { nodeValue: value }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import test from "ava"; | ||
import browserEnv from "browser-env"; | ||
/** @jsx createElement */ | ||
import { render, createElement } from "../src/didact"; | ||
|
||
// Create document global var | ||
browserEnv(["document"]); | ||
|
||
test.beforeEach(t => { | ||
const root = document.createElement("div"); | ||
document.body.appendChild(root); | ||
t.context.root = root; | ||
}); | ||
|
||
test.afterEach.always(t => { | ||
const root = t.context.root; | ||
root.innerHTML = ""; | ||
document.body.removeChild(root); | ||
}); | ||
|
||
test("render jsx div", t => { | ||
const root = t.context.root; | ||
const element = <div />; | ||
render(element, root); | ||
t.is(root.innerHTML, "<div></div>"); | ||
}); | ||
|
||
test("render div with children", t => { | ||
const root = t.context.root; | ||
const element = <div><b /><a href="foo" /></div>; | ||
render(element, root); | ||
t.is(root.innerHTML, '<div><b></b><a href="foo"></a></div>'); | ||
}); | ||
|
||
test("render div with props", t => { | ||
const root = t.context.root; | ||
const element = <div id="foo" />; | ||
render(element, root); | ||
t.is(root.innerHTML, '<div id="foo"></div>'); | ||
}); | ||
|
||
test("render span with text child", t => { | ||
const root = t.context.root; | ||
const element = <span>Foo</span>; | ||
render(element, root); | ||
t.is(root.innerHTML, "<span>Foo</span>"); | ||
}); |