1 module ut.dom; 2 3 version (unittest): 4 5 import unit_threaded; 6 import spasm.dom; 7 import spasm.spa; 8 import spasm.types; 9 10 @safe: 11 12 struct Div { 13 mixin Node!"div"; 14 } 15 16 unittest { 17 renderToString!Div.should == "<div></div>"; 18 } 19 20 unittest { 21 struct Styled { 22 @style!"class" mixin Node!"div"; 23 } 24 renderToString!Styled.should == `<div class="class"></div>`; 25 } 26 27 unittest { 28 struct App { 29 mixin Node!"section"; 30 @child Div div; 31 } 32 renderToString!App.should == "<section><div></div></section>"; 33 } 34 35 unittest { 36 struct Toggle { 37 mixin Node!"li"; 38 @style!"active" bool active; 39 } 40 Toggle toggle; 41 auto node = toggle.renderToNode; 42 node.renderToString().should == "<li></li>"; 43 toggle.update.active = true; 44 node.renderToString().should == `<li class="active"></li>`; 45 } 46 47 unittest { 48 struct ChildStyle { 49 mixin Node!"li"; 50 @style!"inner" @child Div div; 51 } 52 renderToString!ChildStyle.should == `<li><div class="inner"></div></li>`; 53 } 54 55 unittest { 56 struct ChildVisibility { 57 mixin Node!"li"; 58 @child Div div; 59 @visible!"div" bool show; 60 } 61 ChildVisibility app; 62 auto node = app.renderToNode; 63 node.renderToString().should == `<li></li>`; 64 app.update.show = true; 65 node.renderToString().should == `<li><div></div></li>`; 66 } 67 68 unittest { 69 struct Inner { 70 mixin Node!"span"; 71 @attr int* key; 72 } 73 struct App { 74 mixin Node!"div"; 75 int key = 0; 76 @child Inner inner; 77 } 78 App app; 79 auto node = app.renderToNode; 80 node.renderToString().should == `<div><span key=0></span></div>`; 81 app.update.key = 5; 82 node.renderToString().should == `<div><span key=5></span></div>`; 83 } 84 85 unittest { 86 static struct Appy { 87 nothrow: 88 mixin Node!"section"; 89 bool hidden; 90 @style!"active" bool isActive(bool hidden) { 91 return !hidden; 92 } 93 } 94 Appy app; 95 auto node = app.renderToNode; 96 node.renderToString().should == `<section class="active"></section>`; 97 app.update.hidden = true; 98 node.renderToString().should == `<section></section>`; 99 } 100 101 unittest { 102 static struct Inner { 103 mixin Node!"div"; 104 @attr int* count; 105 } 106 static struct App { 107 mixin Node!"section"; 108 int number = 6; 109 @(param.count!(number)) 110 @child Inner inner; 111 } 112 App app; 113 auto node = app.renderToNode; 114 node.renderToString().should == `<section><div count=6></div></section>`; 115 app.update.number = 5; 116 node.renderToString().should == `<section><div count=5></div></section>`; 117 } 118 119 unittest { 120 alias ChildNode = NamedNode!"root"*; 121 static struct Parent { 122 mixin Node!"section"; 123 @child ChildNode left; 124 @child ChildNode right; 125 } 126 static struct Div { 127 mixin Node!"div"; 128 @prop string innerHTML; 129 } 130 static struct App { 131 @(param.left!left.right!right) 132 @child Parent parent; 133 Div left = { innerHTML: "l" }; 134 @(param.innerHTML!"r") 135 Div right; 136 } 137 App app; 138 auto node = app.renderToNode; 139 node.renderToString().should == `<section><div innerHTML="l"></div><div innerHTML="r"></div></section>`; 140 } 141 142 unittest { 143 alias ChildNode = NamedNode!"root"*; 144 static struct Parent { 145 mixin Node!"section"; 146 @child ChildNode left; 147 } 148 static struct Left { 149 mixin Node!"div"; 150 @child ChildNode top; 151 } 152 static struct Top { 153 mixin Node!"header"; 154 @prop string innerHTML = "top"; 155 } 156 static struct App { 157 @(param.left!left) 158 @child Parent parent; 159 @(param.top!top) 160 Left left; 161 Top top; 162 } 163 App app; 164 auto node = app.renderToNode; 165 node.renderToString().should == `<section><div><header innerHTML="top"></header></div></section>`; 166 }