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