|
| 1 | +import test from "ava"; |
| 2 | +import browserEnv from "browser-env"; |
| 3 | +import { render } from "../src/didact"; |
| 4 | + |
| 5 | +// Create document global var |
| 6 | +browserEnv(["document"]); |
| 7 | + |
| 8 | +test.beforeEach(t => { |
| 9 | + const root = document.createElement("div"); |
| 10 | + document.body.appendChild(root); |
| 11 | + t.context.root = root; |
| 12 | +}); |
| 13 | + |
| 14 | +test.afterEach.always(t => { |
| 15 | + const root = t.context.root; |
| 16 | + root.innerHTML = ""; |
| 17 | + document.body.removeChild(root); |
| 18 | +}); |
| 19 | + |
| 20 | +test("render div", t => { |
| 21 | + const root = t.context.root; |
| 22 | + const element = { |
| 23 | + type: "div", |
| 24 | + props: {} |
| 25 | + }; |
| 26 | + render(element, root); |
| 27 | + t.is(root.innerHTML, "<div></div>"); |
| 28 | +}); |
| 29 | + |
| 30 | +test("render div with children", t => { |
| 31 | + const root = t.context.root; |
| 32 | + const element = { |
| 33 | + type: "div", |
| 34 | + props: { |
| 35 | + children: [ |
| 36 | + { type: "b", props: {} }, |
| 37 | + { type: "a", props: { href: "foo" } } |
| 38 | + ] |
| 39 | + } |
| 40 | + }; |
| 41 | + render(element, root); |
| 42 | + t.is(root.innerHTML, '<div><b></b><a href="foo"></a></div>'); |
| 43 | +}); |
| 44 | + |
| 45 | +test("render div with props", t => { |
| 46 | + const root = t.context.root; |
| 47 | + const element = { |
| 48 | + type: "div", |
| 49 | + props: { id: "foo" } |
| 50 | + }; |
| 51 | + render(element, root); |
| 52 | + t.is(root.innerHTML, '<div id="foo"></div>'); |
| 53 | +}); |
| 54 | + |
| 55 | +test("render span with text child", t => { |
| 56 | + const root = t.context.root; |
| 57 | + const element = { |
| 58 | + type: "span", |
| 59 | + props: { |
| 60 | + children: [ |
| 61 | + { |
| 62 | + type: "TEXT ELEMENT", |
| 63 | + props: { nodeValue: "Foo" } |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + }; |
| 68 | + render(element, root); |
| 69 | + t.is(root.innerHTML, "<span>Foo</span>"); |
| 70 | +}); |
0 commit comments