Precise DOM morphing
morphing typescript dom
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Reorganize test files

+725 -717
+78
test/alpine-morph.test.js
··· 1 + import { fixture, html, expect } from "@open-wc/testing"; 2 + import { morph } from "../"; 3 + 4 + // adapted from: https://github.com/alpinejs/alpine/blob/891d68503960a39826e89f2f666d9b1e7ce3f0c9/tests/jest/morph/external.spec.js 5 + describe("alpine-morph", () => { 6 + it("updates text content", async () => { 7 + const a = await fixture(html`<div>foo</div>`); 8 + const b = await fixture(html`<div>bar</div>`); 9 + 10 + morph(a, b); 11 + 12 + expect(a.outerHTML).to.equal(b.outerHTML); 13 + }); 14 + 15 + it("changes inner tag", async () => { 16 + const a = await fixture(html`<div><div>foo</div></div>`); 17 + const b = await fixture(html`<div><span>foo</span></div>`); 18 + 19 + morph(a, b); 20 + 21 + expect(a.outerHTML).to.equal(b.outerHTML); 22 + }); 23 + 24 + it("adds child", async () => { 25 + const a = await fixture(html`<div>foo</div>`); 26 + const b = await fixture( 27 + html`<div> 28 + foo 29 + <h1>baz</h1> 30 + </div>`, 31 + ); 32 + 33 + morph(a, b); 34 + 35 + expect(a.outerHTML).to.equal(b.outerHTML); 36 + }); 37 + 38 + it("removes child", async () => { 39 + const a = await fixture( 40 + html`<div> 41 + foo 42 + <h1>baz</h1> 43 + </div>`, 44 + ); 45 + const b = await fixture(html`<div>foo</div>`); 46 + 47 + morph(a, b); 48 + 49 + expect(a.outerHTML).to.equal(b.outerHTML); 50 + }); 51 + 52 + it("adds attribute", async () => { 53 + const a = await fixture(html`<div>foo</div>`); 54 + const b = await fixture(html`<div foo="bar">foo</div>`); 55 + 56 + morph(a, b); 57 + 58 + expect(a.outerHTML).to.equal(b.outerHTML); 59 + }); 60 + 61 + it("removes attribute", async () => { 62 + const a = await fixture(html`<div foo="bar">foo</div>`); 63 + const b = await fixture(html`<div>foo</div>`); 64 + 65 + morph(a, b); 66 + 67 + expect(a.outerHTML).to.equal(b.outerHTML); 68 + }); 69 + 70 + it("changes attribute", async () => { 71 + const a = await fixture(html`<div foo="bar">foo</div>`); 72 + const b = await fixture(html`<div foo="baz">foo</div>`); 73 + 74 + morph(a, b); 75 + 76 + expect(a.outerHTML).to.equal(b.outerHTML); 77 + }); 78 + });
+647
test/nanomorph.test.js
··· 1 + import { fixture, html, expect } from "@open-wc/testing"; 2 + import { morph } from "../"; 3 + 4 + // adapted from: https://github.com/choojs/nanomorph/blob/b8088d03b1113bddabff8aa0e44bd8db88d023c7/test/diff.js 5 + describe("nanomorph", () => { 6 + describe("root level", () => { 7 + it("should replace a node", async () => { 8 + const a = await fixture(html`<p>hello world</p>`); 9 + const b = await fixture(html`<div>hello world</div>`); 10 + 11 + morph(a, b); 12 + 13 + expect(a.outerHTML).to.equal(b.outerHTML); 14 + }); 15 + 16 + it("should replace a component", async () => { 17 + const a = await fixture(html`<div data-nanomorph-component-id="a">hello world</div>`); 18 + const b = await fixture(html`<div data-nanomorph-component-id="b">bye moon</div>`); 19 + 20 + morph(a, b); 21 + 22 + expect(a.outerHTML).to.equal(b.outerHTML); 23 + }); 24 + 25 + it("should morph a node", async () => { 26 + const a = await fixture(html`<p>hello world</p>`); 27 + const b = await fixture(html`<p>hello you</p>`); 28 + 29 + morph(a, b); 30 + 31 + expect(a.outerHTML).to.equal(b.outerHTML); 32 + }); 33 + 34 + it("should morph a node with namespaced attribute", async () => { 35 + const a = await fixture(html`<svg><use xlink:href="#heybooboo"></use></svg>`); 36 + const b = await fixture(html`<svg><use xlink:href="#boobear"></use></svg>`); 37 + 38 + morph(a, b); 39 + 40 + expect(a.outerHTML).to.equal(b.outerHTML); 41 + }); 42 + 43 + it("should ignore if node is same", async () => { 44 + const a = await fixture(html`<p>hello world</p>`); 45 + 46 + morph(a, a); 47 + 48 + expect(a.outerHTML).to.equal(a.outerHTML); 49 + }); 50 + }); 51 + 52 + describe("nested", () => { 53 + it("should replace a node", async () => { 54 + const a = await fixture(html`<main><p>hello world</p></main>`); 55 + const b = await fixture(html`<main><div>hello world</div></main>`); 56 + 57 + morph(a, b); 58 + 59 + expect(a.outerHTML).to.equal(b.outerHTML); 60 + }); 61 + 62 + it("should replace a node", async () => { 63 + const a = await fixture(html`<main><p>hello world</p></main>`); 64 + const b = await fixture(html`<main><p>hello you</p></main>`); 65 + 66 + morph(a, b); 67 + 68 + expect(a.outerHTML).to.equal(b.outerHTML); 69 + }); 70 + 71 + it("should replace a node", async () => { 72 + const a = await fixture(html`<main><p>hello world</p></main>`); 73 + 74 + morph(a, a); 75 + 76 + expect(a.outerHTML).to.equal(a.outerHTML); 77 + }); 78 + 79 + it("should append a node", async () => { 80 + const a = await fixture(html`<main></main>`); 81 + const b = await fixture(html`<main><p>hello you</p></main>`); 82 + 83 + morph(a, b); 84 + 85 + expect(a.outerHTML).to.equal(b.outerHTML); 86 + }); 87 + 88 + it("should remove a node", async () => { 89 + const a = await fixture(html`<main><p>hello you</p></main>`); 90 + const b = await fixture(html`<main></main>`); 91 + 92 + morph(a, b); 93 + 94 + expect(a.outerHTML).to.equal(b.outerHTML); 95 + }); 96 + 97 + it.skip("should update child nodes", async () => { 98 + const a = await fixture(html`<main><p>hello world</p></main>`); 99 + const b = await fixture(html`<section><p>hello you</p></section>`); 100 + 101 + morph(a, b, { childrenOnly: true }); 102 + 103 + expect(a.outerHTML).to.equal("<main><p>hello you</p></main>"); 104 + }); 105 + }); 106 + 107 + describe("values", () => { 108 + it("if new tree has no value and old tree does, remove value", async () => { 109 + const a = await fixture(html`<input type="text" value="howdy" />`); 110 + const b = await fixture(html`<input type="text" />`); 111 + 112 + morph(a, b); 113 + 114 + expect(a.outerHTML).to.equal(b.outerHTML); 115 + expect(a.getAttribute("value")).to.equal(null); 116 + expect(a.value).to.equal(""); 117 + }); 118 + 119 + it("if new tree has null value and old tree does, remove value", async () => { 120 + const a = await fixture(html`<input type="text" value="howdy" />`); 121 + const b = await fixture(html`<input type="text" value=${null} />`); 122 + 123 + morph(a, b); 124 + 125 + expect(a.outerHTML).to.equal(b.outerHTML); 126 + expect(a.getAttribute("value")).to.equal(null); 127 + expect(a.value).to.equal(""); 128 + }); 129 + 130 + it("if new tree has value in HTML and old tree does too, set value from new tree", async () => { 131 + const a = await fixture(html`<input type="text" value="howdy" />`); 132 + const b = await fixture(html`<input type="text" value="hi" />`); 133 + 134 + morph(a, b); 135 + 136 + expect(a.outerHTML).to.equal(b.outerHTML); 137 + expect(a.value).to.equal("hi"); 138 + }); 139 + 140 + it("if new tree has value from mutation and old tree does too, set value from new tree", async () => { 141 + const a = await fixture(html`<input type="text" />`); 142 + a.value = "howdy"; 143 + const b = await fixture(html`<input type="text" />`); 144 + b.value = "hi"; 145 + 146 + morph(a, b); 147 + 148 + expect(a.outerHTML).to.equal(b.outerHTML); 149 + expect(a.value).to.equal("hi"); 150 + }); 151 + 152 + it("if new tree has value in HTML and old tree does from mutation, set value from new tree", async () => { 153 + const a = await fixture(html`<input type="text" value="howdy" />`); 154 + const b = await fixture(html`<input type="text" />`); 155 + b.value = "hi"; 156 + 157 + morph(a, b); 158 + 159 + expect(a.outerHTML).to.equal(b.outerHTML); 160 + expect(a.value).to.equal("hi"); 161 + }); 162 + 163 + it("if new tree has value from mutation and old tree does in HTML, set value from new tree", async () => { 164 + const a = await fixture(html`<input type="text" value="howdy" />`); 165 + a.value = "howdy"; 166 + const b = await fixture(html`<input type="text" value="hi" />`); 167 + 168 + morph(a, b); 169 + 170 + expect(a.outerHTML).to.equal(b.outerHTML); 171 + expect(a.value).to.equal("hi"); 172 + }); 173 + }); 174 + 175 + describe("boolean properties", () => { 176 + describe("checked", () => { 177 + it("if new tree has no checked and old tree does, remove value", async () => { 178 + const a = await fixture(html`<input type="checkbox" checked=${true} />`); 179 + const b = await fixture(html`<input type="checkbox" />`); 180 + 181 + morph(a, b); 182 + 183 + expect(a.outerHTML).to.equal(b.outerHTML); 184 + expect(a.checked).to.equal(false); 185 + }); 186 + 187 + it("if new tree has checked and old tree does not, add value", async () => { 188 + const a = await fixture(html`<input type="checkbox" />`); 189 + const b = await fixture(html`<input type="checkbox" checked=${true} />`); 190 + 191 + morph(a, b); 192 + 193 + expect(a.outerHTML).to.equal(b.outerHTML); 194 + expect(a.checked).to.equal(true); 195 + }); 196 + 197 + it("if new tree has checked=false and old tree has checked=true, set value from new tree", async () => { 198 + const a = await fixture(html`<input type="checkbox" checked=${false} />`); 199 + const b = await fixture(html`<input type="checkbox" checked=${true} />`); 200 + 201 + morph(a, b); 202 + 203 + expect(a.outerHTML).to.equal(b.outerHTML); 204 + expect(a.checked).to.equal(true); 205 + }); 206 + 207 + it("if new tree has checked=true and old tree has checked=false, set value from new tree", async () => { 208 + const a = await fixture(html`<input type="checkbox" checked=${true} />`); 209 + const b = await fixture(html`<input type="checkbox" checked=${false} />`); 210 + 211 + morph(a, b); 212 + 213 + expect(a.outerHTML).to.equal(b.outerHTML); 214 + expect(a.checked).to.equal(false); 215 + }); 216 + 217 + it("if new tree has no checked and old tree has checked mutated to true, set value from new tree", async () => { 218 + const a = await fixture(html`<input type="checkbox" />`); 219 + const b = await fixture(html`<input type="checkbox" />`); 220 + b.checked = true; 221 + 222 + morph(a, b); 223 + 224 + expect(a.outerHTML).to.equal(b.outerHTML); 225 + expect(a.checked).to.equal(true); 226 + }); 227 + 228 + it("if new tree has checked=false and old tree has checked mutated to true, set value from new tree", async () => { 229 + const a = await fixture(html`<input type="checkbox" checked=${false} />`); 230 + const b = await fixture(html`<input type="checkbox" />`); 231 + b.checked = true; 232 + 233 + morph(a, b); 234 + 235 + expect(a.outerHTML).to.equal(b.outerHTML); 236 + expect(a.checked).to.equal(true); 237 + }); 238 + 239 + it("if new tree has checked=true and old tree has checked mutated to false, set value from new tree", async () => { 240 + const a = await fixture(html`<input type="checkbox" checked=${true} />`); 241 + const b = await fixture(html`<input type="checkbox" />`); 242 + b.checked = false; 243 + 244 + morph(a, b); 245 + 246 + expect(a.outerHTML).to.equal(b.outerHTML); 247 + expect(a.checked).to.equal(false); 248 + }); 249 + 250 + it("if new tree has no checked and old tree has checked=true, set value from new tree", async () => { 251 + const a = await fixture(html`<input type="checkbox" />`); 252 + const b = await fixture(html`<input type="checkbox" checked=${true} />`); 253 + 254 + morph(a, b); 255 + 256 + expect(a.outerHTML).to.equal(b.outerHTML); 257 + expect(a.checked).to.equal(true); 258 + }); 259 + }); 260 + 261 + describe("disabled", () => { 262 + it("if new tree has no disabled and old tree does, remove value", async () => { 263 + const a = await fixture(html`<input type="checkbox" disabled=${true} />`); 264 + const b = await fixture(html`<input type="checkbox" />`); 265 + 266 + morph(a, b); 267 + 268 + expect(a.outerHTML).to.equal(b.outerHTML); 269 + expect(a.disabled).to.equal(false); 270 + }); 271 + 272 + it("if new tree has disabled and old tree does not, add value", async () => { 273 + const a = await fixture(html`<input type="checkbox" />`); 274 + const b = await fixture(html`<input type="checkbox" disabled=${true} />`); 275 + 276 + morph(a, b); 277 + 278 + expect(a.outerHTML).to.equal(b.outerHTML); 279 + expect(a.disabled).to.equal(true); 280 + }); 281 + 282 + it("if new tree has disabled=false and old tree has disabled=true, set value from new tree", async () => { 283 + const a = await fixture(html`<input type="checkbox" disabled=${false} />`); 284 + const b = await fixture(html`<input type="checkbox" disabled=${true} />`); 285 + 286 + morph(a, b); 287 + 288 + expect(a.outerHTML).to.equal(b.outerHTML); 289 + expect(a.disabled).to.equal(true); 290 + }); 291 + 292 + it("if new tree has disabled=true and old tree has disabled=false, set value from new tree", async () => { 293 + const a = await fixture(html`<input type="checkbox" disabled=${true} />`); 294 + const b = await fixture(html`<input type="checkbox" disabled=${false} />`); 295 + 296 + morph(a, b); 297 + 298 + expect(a.outerHTML).to.equal(b.outerHTML); 299 + expect(a.disabled).to.equal(false); 300 + }); 301 + 302 + it("if new tree has no disabled and old tree has disabled mutated to true, set value from new tree", async () => { 303 + const a = await fixture(html`<input type="checkbox" />`); 304 + const b = await fixture(html`<input type="checkbox" />`); 305 + b.disabled = true; 306 + 307 + morph(a, b); 308 + 309 + expect(a.outerHTML).to.equal(b.outerHTML); 310 + expect(a.disabled).to.equal(true); 311 + }); 312 + 313 + it("if new tree has disabled=false and old tree has disabled mutated to true, set value from new tree", async () => { 314 + const a = await fixture(html`<input type="checkbox" disabled=${false} />`); 315 + const b = await fixture(html`<input type="checkbox" />`); 316 + b.disabled = true; 317 + 318 + morph(a, b); 319 + 320 + expect(a.outerHTML).to.equal(b.outerHTML); 321 + expect(a.disabled).to.equal(true); 322 + }); 323 + 324 + it("if new tree has disabled=true and old tree has disabled mutated to false, set value from new tree", async () => { 325 + const a = await fixture(html`<input type="checkbox" disabled=${true} />`); 326 + const b = await fixture(html`<input type="checkbox" />`); 327 + b.disabled = false; 328 + 329 + morph(a, b); 330 + 331 + expect(a.outerHTML).to.equal(b.outerHTML); 332 + expect(a.disabled).to.equal(false); 333 + }); 334 + 335 + it("if new tree has no disabled and old tree has disabled=true, set value from new tree", async () => { 336 + const a = await fixture(html`<input type="checkbox" />`); 337 + const b = await fixture(html`<input type="checkbox" disabled=${true} />`); 338 + 339 + morph(a, b); 340 + 341 + expect(a.outerHTML).to.equal(b.outerHTML); 342 + expect(a.disabled).to.equal(true); 343 + }); 344 + }); 345 + 346 + describe("indeterminate", () => { 347 + it("if new tree has no indeterminate and old tree has indeterminate mutated to true, set value from new tree", async () => { 348 + const a = await fixture(html`<input type="checkbox" />`); 349 + const b = await fixture(html`<input type="checkbox" />`); 350 + b.indeterminate = true; 351 + 352 + morph(a, b); 353 + 354 + expect(a.outerHTML).to.equal(b.outerHTML); 355 + expect(a.indeterminate).to.equal(true); 356 + }); 357 + 358 + it("if new tree has no indeterminate and old tree has indeterminate mutated to false, set value from new tree", async () => { 359 + const a = await fixture(html`<input type="checkbox" />`); 360 + const b = await fixture(html`<input type="checkbox" />`); 361 + b.indeterminate = false; 362 + 363 + morph(a, b); 364 + 365 + expect(a.outerHTML).to.equal(b.outerHTML); 366 + expect(a.indeterminate).to.equal(false); 367 + }); 368 + }); 369 + }); 370 + 371 + describe("lists", () => { 372 + it("should append nodes", async () => { 373 + const a = await fixture(html`<ul></ul>`); 374 + const b = await fixture( 375 + html`<ul> 376 + <li>1</li> 377 + <li>2</li> 378 + <li>3</li> 379 + <li>4</li> 380 + <li>5</li> 381 + </ul>`, 382 + ); 383 + 384 + morph(a, b); 385 + 386 + expect(a.outerHTML).to.equal(b.outerHTML); 387 + }); 388 + 389 + it("should remove nodes", async () => { 390 + const a = await fixture( 391 + html`<ul> 392 + <li>1</li> 393 + <li>2</li> 394 + <li>3</li> 395 + <li>4</li> 396 + <li>5</li> 397 + </ul>`, 398 + ); 399 + const b = await fixture(html`<ul></ul>`); 400 + 401 + morph(a, b); 402 + 403 + expect(a.outerHTML).to.equal(b.outerHTML); 404 + }); 405 + }); 406 + 407 + describe("selectables", () => { 408 + it('should append nodes', async () => { 409 + const a = await fixture(html`<select></select>`); 410 + const b = await fixture(html`<select><option>1</option><option>2</option><option>3</option><option>4</option></select>`); 411 + 412 + morph(a, b); 413 + 414 + expect(a.outerHTML).to.equal(b.outerHTML); 415 + }); 416 + 417 + it('should append nodes (including optgroups)', async () => { 418 + const a = await fixture(html`<select></select>`); 419 + const b = await fixture(html`<select><optgroup><option>1</option><option>2</option></optgroup><option>3</option><option>4</option></select>`); 420 + 421 + morph(a, b); 422 + 423 + expect(a.outerHTML).to.equal(b.outerHTML); 424 + }); 425 + 426 + it('should remove nodes', async () => { 427 + const a = await fixture(html`<select><option>1</option><option>2</option><option>3</option><option>4</option></select>`); 428 + const b = await fixture(html`<select></select>`); 429 + 430 + morph(a, b); 431 + 432 + expect(a.outerHTML).to.equal(b.outerHTML); 433 + }); 434 + 435 + it('should remove nodes (including optgroups)', async () => { 436 + const a = await fixture(html`<select><optgroup><option>1</option><option>2</option></optgroup><option>3</option><option>4</option></select>`); 437 + const b = await fixture(html`<select></select>`); 438 + 439 + morph(a, b); 440 + 441 + expect(a.outerHTML).to.equal(b.outerHTML); 442 + }); 443 + 444 + it('should add selected', async () => { 445 + const a = await fixture(html`<select><option>1</option><option>2</option></select>`); 446 + const b = await fixture(html`<select><option>1</option><option selected>2</option></select>`); 447 + 448 + morph(a, b); 449 + 450 + expect(a.outerHTML).to.equal(b.outerHTML); 451 + }); 452 + 453 + it('should add selected (xhtml)', async () => { 454 + const a = await fixture(html`<select><option>1</option><option>2</option></select>`); 455 + const b = await fixture(html`<select><option>1</option><option selected="selected">2</option></select>`); 456 + 457 + morph(a, b); 458 + 459 + expect(a.outerHTML).to.equal(b.outerHTML); 460 + }); 461 + 462 + it('should switch selected', async () => { 463 + const a = await fixture(html`<select><option selected="selected">1</option><option>2</option></select>`); 464 + const b = await fixture(html`<select><option>1</option><option selected="selected">2</option></select>`); 465 + 466 + morph(a, b); 467 + 468 + expect(a.outerHTML).to.equal(b.outerHTML); 469 + }); 470 + }); 471 + 472 + it("should replace nodes", async () => { 473 + const a = await fixture(html`<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>`); 474 + const b = await fixture(html`<ul><div>1</div><li>2</li><p>3</p><li>4</li><li>5</li></ul>`); 475 + 476 + morph(a, b); 477 + 478 + expect(a.outerHTML).to.equal(b.outerHTML); 479 + }); 480 + 481 + it("should replace nodes after multiple iterations", async () => { 482 + const a = await fixture(html`<ul></ul>`); 483 + const b = await fixture(html`<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>`); 484 + 485 + morph(a, b); 486 + 487 + expect(a.outerHTML).to.equal(b.outerHTML); 488 + 489 + const c = await fixture(html`<ul><div>1</div><li>2</li><p>3</p><li>4</li><li>5</li></ul>`); 490 + 491 + morph(a, c); 492 + 493 + expect(a.outerHTML).to.equal(c.outerHTML); 494 + }); 495 + 496 + describe('use id as a key hint', () => { 497 + it('appends an element', async () => { 498 + const a = await fixture(html`<ul> 499 + <li id="a"></li> 500 + <li id="b"></li> 501 + <li id="c"></li> 502 + </ul>`); 503 + const b = await fixture(html`<ul> 504 + <li id="a"></li> 505 + <li id="new"></li> 506 + <li id="b"></li> 507 + <li id="c"></li> 508 + </ul>`); 509 + 510 + const oldFirst = a.children[0]; 511 + const oldSecond = a.children[1]; 512 + const oldThird = a.children[2]; 513 + 514 + morph(a, b); 515 + 516 + expect(a.outerHTML).to.equal(b.outerHTML); 517 + expect(a.children[0]).to.equal(oldFirst); 518 + expect(a.children[1]).to.equal(oldSecond); 519 + expect(a.children[2]).to.equal(oldThird); 520 + }); 521 + 522 + it('handles non-id elements', async () => { 523 + const a = await fixture(html`<ul> 524 + <li></li> 525 + <li id="a"></li> 526 + <li id="b"></li> 527 + <li id="c"></li> 528 + <li></li> 529 + </ul>`); 530 + const b = await fixture(html`<ul> 531 + <li></li> 532 + <li id="a"></li> 533 + <li id="new"></li> 534 + <li id="b"></li> 535 + <li id="c"></li> 536 + <li></li> 537 + </ul>`); 538 + 539 + const oldSecond = a.children[1]; 540 + const oldThird = a.children[2]; 541 + const oldFourth = a.children[3]; 542 + 543 + morph(a, b); 544 + 545 + expect(a.outerHTML).to.equal(b.outerHTML); 546 + expect(a.children[1]).to.equal(oldSecond); 547 + expect(a.children[3]).to.equal(oldThird); 548 + expect(a.children[4]).to.equal(oldFourth); 549 + }); 550 + 551 + it('copies over children', async () => { 552 + const a = await fixture(html`<section>'hello'<section>`); 553 + const b = await fixture(html`<section><div></div><section>`); 554 + 555 + morph(a, b); 556 + 557 + expect(a.outerHTML).to.equal(b.outerHTML); 558 + }) 559 + 560 + it('removes an element', async () => { 561 + const a = await fixture(html`<ul><li id="a"></li><li id="b"></li><li id="c"></li></ul>`); 562 + const b = await fixture(html`<ul><li id="a"></li><li id="c"></li></ul>`); 563 + 564 + const oldFirst = a.children[0]; 565 + const oldThird = a.children[2]; 566 + 567 + morph(a, b); 568 + 569 + expect(a.outerHTML).to.equal(b.outerHTML); 570 + expect(a.children[0]).to.equal(oldFirst); 571 + expect(a.children[1]).to.equal(oldThird); 572 + }) 573 + 574 + it('id match still morphs', async () => { 575 + const a = await fixture(html`<li id="12">FOO</li>`); 576 + const b = await fixture(html`<li id="12">BAR</li>`); 577 + 578 + morph(a, b); 579 + 580 + expect(a.outerHTML).to.equal(b.outerHTML); 581 + }) 582 + 583 + it('removes orphaned keyed nodes', async () => { 584 + const a = await fixture(html` 585 + <div> 586 + <div>1</div> 587 + <li id="a">a</li> 588 + </div> 589 + `); 590 + const b = await fixture(html` 591 + <div> 592 + <div>2</div> 593 + <li id="b">b</li> 594 + </div> 595 + `); 596 + 597 + morph(a, b); 598 + 599 + expect(a.outerHTML).to.equal(b.outerHTML); 600 + }) 601 + 602 + it('whitespace', async () => { 603 + const a = await fixture(html`<ul> </ul>`); 604 + const b = await fixture(html`<ul><li></li><li></li> </ul>`); 605 + 606 + morph(a, b); 607 + 608 + expect(a.outerHTML).to.equal(b.outerHTML); 609 + }) 610 + }); 611 + 612 + it('allows morphing from Node to NodeList', async () => { 613 + const a = await fixture(html`<div><div>a</div></div>`); 614 + const b = await fixture(html`<div>a</div><div>b</div>`); 615 + 616 + morph(a, b); 617 + 618 + expect(a.outerHTML).to.equal(b.outerHTML); 619 + }) 620 + 621 + it('allows morphing from NodeList to Node', async () => { 622 + const a = await fixture(html`<div>a</div><div>b</div>`); 623 + const b = await fixture(html`<div><div>a</div></div>`); 624 + 625 + morph(a, b); 626 + 627 + expect(a.outerHTML).to.equal(b.outerHTML); 628 + }) 629 + 630 + it('allows morphing from NodeList to NodeList', async () => { 631 + const a = await fixture(html`<div>a</div><div>b</div>`); 632 + const b = await fixture(html`<div>z</div><div>y</div>`); 633 + 634 + morph(a, b); 635 + 636 + expect(a.outerHTML).to.equal(b.outerHTML); 637 + }) 638 + 639 + it('allows morphing from Node to Node', async () => { 640 + const a = await fixture(html`<div><div>a</div></div>`); 641 + const b = await fixture(html`<div><div>b</div></div>`); 642 + 643 + morph(a, b); 644 + 645 + expect(a.outerHTML).to.equal(b.outerHTML); 646 + }) 647 + });
-717
test/something.test.js
··· 37 37 38 38 expect(a.outerHTML).to.equal(b.outerHTML); 39 39 }); 40 - 41 - // adapted from: https://github.com/alpinejs/alpine/blob/891d68503960a39826e89f2f666d9b1e7ce3f0c9/tests/jest/morph/external.spec.js 42 - it("updates text content", async () => { 43 - const a = await fixture(html`<div>foo</div>`); 44 - const b = await fixture(html`<div>bar</div>`); 45 - 46 - morph(a, b); 47 - 48 - expect(a.outerHTML).to.equal(b.outerHTML); 49 - }); 50 - 51 - it("changes inner tag", async () => { 52 - const a = await fixture(html`<div><div>foo</div></div>`); 53 - const b = await fixture(html`<div><span>foo</span></div>`); 54 - 55 - morph(a, b); 56 - 57 - expect(a.outerHTML).to.equal(b.outerHTML); 58 - }); 59 - 60 - it("adds child", async () => { 61 - const a = await fixture(html`<div>foo</div>`); 62 - const b = await fixture( 63 - html`<div> 64 - foo 65 - <h1>baz</h1> 66 - </div>`, 67 - ); 68 - 69 - morph(a, b); 70 - 71 - expect(a.outerHTML).to.equal(b.outerHTML); 72 - }); 73 - 74 - it("removes child", async () => { 75 - const a = await fixture( 76 - html`<div> 77 - foo 78 - <h1>baz</h1> 79 - </div>`, 80 - ); 81 - const b = await fixture(html`<div>foo</div>`); 82 - 83 - morph(a, b); 84 - 85 - expect(a.outerHTML).to.equal(b.outerHTML); 86 - }); 87 - 88 - it("adds attribute", async () => { 89 - const a = await fixture(html`<div>foo</div>`); 90 - const b = await fixture(html`<div foo="bar">foo</div>`); 91 - 92 - morph(a, b); 93 - 94 - expect(a.outerHTML).to.equal(b.outerHTML); 95 - }); 96 - 97 - it("removes attribute", async () => { 98 - const a = await fixture(html`<div foo="bar">foo</div>`); 99 - const b = await fixture(html`<div>foo</div>`); 100 - 101 - morph(a, b); 102 - 103 - expect(a.outerHTML).to.equal(b.outerHTML); 104 - }); 105 - 106 - it("changes attribute", async () => { 107 - const a = await fixture(html`<div foo="bar">foo</div>`); 108 - const b = await fixture(html`<div foo="baz">foo</div>`); 109 - 110 - morph(a, b); 111 - 112 - expect(a.outerHTML).to.equal(b.outerHTML); 113 - }); 114 - 115 - // adapted from: https://github.com/choojs/nanomorph/blob/b8088d03b1113bddabff8aa0e44bd8db88d023c7/test/diff.js 116 - describe("root level", () => { 117 - it("should replace a node", async () => { 118 - const a = await fixture(html`<p>hello world</p>`); 119 - const b = await fixture(html`<div>hello world</div>`); 120 - 121 - morph(a, b); 122 - 123 - expect(a.outerHTML).to.equal(b.outerHTML); 124 - }); 125 - 126 - it("should replace a component", async () => { 127 - const a = await fixture(html`<div data-nanomorph-component-id="a">hello world</div>`); 128 - const b = await fixture(html`<div data-nanomorph-component-id="b">bye moon</div>`); 129 - 130 - morph(a, b); 131 - 132 - expect(a.outerHTML).to.equal(b.outerHTML); 133 - }); 134 - 135 - it("should morph a node", async () => { 136 - const a = await fixture(html`<p>hello world</p>`); 137 - const b = await fixture(html`<p>hello you</p>`); 138 - 139 - morph(a, b); 140 - 141 - expect(a.outerHTML).to.equal(b.outerHTML); 142 - }); 143 - 144 - it("should morph a node with namespaced attribute", async () => { 145 - const a = await fixture(html`<svg><use xlink:href="#heybooboo"></use></svg>`); 146 - const b = await fixture(html`<svg><use xlink:href="#boobear"></use></svg>`); 147 - 148 - morph(a, b); 149 - 150 - expect(a.outerHTML).to.equal(b.outerHTML); 151 - }); 152 - 153 - it("should ignore if node is same", async () => { 154 - const a = await fixture(html`<p>hello world</p>`); 155 - 156 - morph(a, a); 157 - 158 - expect(a.outerHTML).to.equal(a.outerHTML); 159 - }); 160 - }); 161 - 162 - describe("nested", () => { 163 - it("should replace a node", async () => { 164 - const a = await fixture(html`<main><p>hello world</p></main>`); 165 - const b = await fixture(html`<main><div>hello world</div></main>`); 166 - 167 - morph(a, b); 168 - 169 - expect(a.outerHTML).to.equal(b.outerHTML); 170 - }); 171 - 172 - it("should replace a node", async () => { 173 - const a = await fixture(html`<main><p>hello world</p></main>`); 174 - const b = await fixture(html`<main><p>hello you</p></main>`); 175 - 176 - morph(a, b); 177 - 178 - expect(a.outerHTML).to.equal(b.outerHTML); 179 - }); 180 - 181 - it("should replace a node", async () => { 182 - const a = await fixture(html`<main><p>hello world</p></main>`); 183 - 184 - morph(a, a); 185 - 186 - expect(a.outerHTML).to.equal(a.outerHTML); 187 - }); 188 - 189 - it("should append a node", async () => { 190 - const a = await fixture(html`<main></main>`); 191 - const b = await fixture(html`<main><p>hello you</p></main>`); 192 - 193 - morph(a, b); 194 - 195 - expect(a.outerHTML).to.equal(b.outerHTML); 196 - }); 197 - 198 - it("should remove a node", async () => { 199 - const a = await fixture(html`<main><p>hello you</p></main>`); 200 - const b = await fixture(html`<main></main>`); 201 - 202 - morph(a, b); 203 - 204 - expect(a.outerHTML).to.equal(b.outerHTML); 205 - }); 206 - 207 - it.skip("should update child nodes", async () => { 208 - const a = await fixture(html`<main><p>hello world</p></main>`); 209 - const b = await fixture(html`<section><p>hello you</p></section>`); 210 - 211 - morph(a, b, { childrenOnly: true }); 212 - 213 - expect(a.outerHTML).to.equal("<main><p>hello you</p></main>"); 214 - }); 215 - }); 216 - 217 - describe("values", () => { 218 - it("if new tree has no value and old tree does, remove value", async () => { 219 - const a = await fixture(html`<input type="text" value="howdy" />`); 220 - const b = await fixture(html`<input type="text" />`); 221 - 222 - morph(a, b); 223 - 224 - expect(a.outerHTML).to.equal(b.outerHTML); 225 - expect(a.getAttribute("value")).to.equal(null); 226 - expect(a.value).to.equal(""); 227 - }); 228 - 229 - it("if new tree has null value and old tree does, remove value", async () => { 230 - const a = await fixture(html`<input type="text" value="howdy" />`); 231 - const b = await fixture(html`<input type="text" value=${null} />`); 232 - 233 - morph(a, b); 234 - 235 - expect(a.outerHTML).to.equal(b.outerHTML); 236 - expect(a.getAttribute("value")).to.equal(null); 237 - expect(a.value).to.equal(""); 238 - }); 239 - 240 - it("if new tree has value in HTML and old tree does too, set value from new tree", async () => { 241 - const a = await fixture(html`<input type="text" value="howdy" />`); 242 - const b = await fixture(html`<input type="text" value="hi" />`); 243 - 244 - morph(a, b); 245 - 246 - expect(a.outerHTML).to.equal(b.outerHTML); 247 - expect(a.value).to.equal("hi"); 248 - }); 249 - 250 - it("if new tree has value from mutation and old tree does too, set value from new tree", async () => { 251 - const a = await fixture(html`<input type="text" />`); 252 - a.value = "howdy"; 253 - const b = await fixture(html`<input type="text" />`); 254 - b.value = "hi"; 255 - 256 - morph(a, b); 257 - 258 - expect(a.outerHTML).to.equal(b.outerHTML); 259 - expect(a.value).to.equal("hi"); 260 - }); 261 - 262 - it("if new tree has value in HTML and old tree does from mutation, set value from new tree", async () => { 263 - const a = await fixture(html`<input type="text" value="howdy" />`); 264 - const b = await fixture(html`<input type="text" />`); 265 - b.value = "hi"; 266 - 267 - morph(a, b); 268 - 269 - expect(a.outerHTML).to.equal(b.outerHTML); 270 - expect(a.value).to.equal("hi"); 271 - }); 272 - 273 - it("if new tree has value from mutation and old tree does in HTML, set value from new tree", async () => { 274 - const a = await fixture(html`<input type="text" value="howdy" />`); 275 - a.value = "howdy"; 276 - const b = await fixture(html`<input type="text" value="hi" />`); 277 - 278 - morph(a, b); 279 - 280 - expect(a.outerHTML).to.equal(b.outerHTML); 281 - expect(a.value).to.equal("hi"); 282 - }); 283 - }); 284 - 285 - describe("boolean properties", () => { 286 - describe("checked", () => { 287 - it("if new tree has no checked and old tree does, remove value", async () => { 288 - const a = await fixture(html`<input type="checkbox" checked=${true} />`); 289 - const b = await fixture(html`<input type="checkbox" />`); 290 - 291 - morph(a, b); 292 - 293 - expect(a.outerHTML).to.equal(b.outerHTML); 294 - expect(a.checked).to.equal(false); 295 - }); 296 - 297 - it("if new tree has checked and old tree does not, add value", async () => { 298 - const a = await fixture(html`<input type="checkbox" />`); 299 - const b = await fixture(html`<input type="checkbox" checked=${true} />`); 300 - 301 - morph(a, b); 302 - 303 - expect(a.outerHTML).to.equal(b.outerHTML); 304 - expect(a.checked).to.equal(true); 305 - }); 306 - 307 - it("if new tree has checked=false and old tree has checked=true, set value from new tree", async () => { 308 - const a = await fixture(html`<input type="checkbox" checked=${false} />`); 309 - const b = await fixture(html`<input type="checkbox" checked=${true} />`); 310 - 311 - morph(a, b); 312 - 313 - expect(a.outerHTML).to.equal(b.outerHTML); 314 - expect(a.checked).to.equal(true); 315 - }); 316 - 317 - it("if new tree has checked=true and old tree has checked=false, set value from new tree", async () => { 318 - const a = await fixture(html`<input type="checkbox" checked=${true} />`); 319 - const b = await fixture(html`<input type="checkbox" checked=${false} />`); 320 - 321 - morph(a, b); 322 - 323 - expect(a.outerHTML).to.equal(b.outerHTML); 324 - expect(a.checked).to.equal(false); 325 - }); 326 - 327 - it("if new tree has no checked and old tree has checked mutated to true, set value from new tree", async () => { 328 - const a = await fixture(html`<input type="checkbox" />`); 329 - const b = await fixture(html`<input type="checkbox" />`); 330 - b.checked = true; 331 - 332 - morph(a, b); 333 - 334 - expect(a.outerHTML).to.equal(b.outerHTML); 335 - expect(a.checked).to.equal(true); 336 - }); 337 - 338 - it("if new tree has checked=false and old tree has checked mutated to true, set value from new tree", async () => { 339 - const a = await fixture(html`<input type="checkbox" checked=${false} />`); 340 - const b = await fixture(html`<input type="checkbox" />`); 341 - b.checked = true; 342 - 343 - morph(a, b); 344 - 345 - expect(a.outerHTML).to.equal(b.outerHTML); 346 - expect(a.checked).to.equal(true); 347 - }); 348 - 349 - it("if new tree has checked=true and old tree has checked mutated to false, set value from new tree", async () => { 350 - const a = await fixture(html`<input type="checkbox" checked=${true} />`); 351 - const b = await fixture(html`<input type="checkbox" />`); 352 - b.checked = false; 353 - 354 - morph(a, b); 355 - 356 - expect(a.outerHTML).to.equal(b.outerHTML); 357 - expect(a.checked).to.equal(false); 358 - }); 359 - 360 - it("if new tree has no checked and old tree has checked=true, set value from new tree", async () => { 361 - const a = await fixture(html`<input type="checkbox" />`); 362 - const b = await fixture(html`<input type="checkbox" checked=${true} />`); 363 - 364 - morph(a, b); 365 - 366 - expect(a.outerHTML).to.equal(b.outerHTML); 367 - expect(a.checked).to.equal(true); 368 - }); 369 - }); 370 - 371 - describe("disabled", () => { 372 - it("if new tree has no disabled and old tree does, remove value", async () => { 373 - const a = await fixture(html`<input type="checkbox" disabled=${true} />`); 374 - const b = await fixture(html`<input type="checkbox" />`); 375 - 376 - morph(a, b); 377 - 378 - expect(a.outerHTML).to.equal(b.outerHTML); 379 - expect(a.disabled).to.equal(false); 380 - }); 381 - 382 - it("if new tree has disabled and old tree does not, add value", async () => { 383 - const a = await fixture(html`<input type="checkbox" />`); 384 - const b = await fixture(html`<input type="checkbox" disabled=${true} />`); 385 - 386 - morph(a, b); 387 - 388 - expect(a.outerHTML).to.equal(b.outerHTML); 389 - expect(a.disabled).to.equal(true); 390 - }); 391 - 392 - it("if new tree has disabled=false and old tree has disabled=true, set value from new tree", async () => { 393 - const a = await fixture(html`<input type="checkbox" disabled=${false} />`); 394 - const b = await fixture(html`<input type="checkbox" disabled=${true} />`); 395 - 396 - morph(a, b); 397 - 398 - expect(a.outerHTML).to.equal(b.outerHTML); 399 - expect(a.disabled).to.equal(true); 400 - }); 401 - 402 - it("if new tree has disabled=true and old tree has disabled=false, set value from new tree", async () => { 403 - const a = await fixture(html`<input type="checkbox" disabled=${true} />`); 404 - const b = await fixture(html`<input type="checkbox" disabled=${false} />`); 405 - 406 - morph(a, b); 407 - 408 - expect(a.outerHTML).to.equal(b.outerHTML); 409 - expect(a.disabled).to.equal(false); 410 - }); 411 - 412 - it("if new tree has no disabled and old tree has disabled mutated to true, set value from new tree", async () => { 413 - const a = await fixture(html`<input type="checkbox" />`); 414 - const b = await fixture(html`<input type="checkbox" />`); 415 - b.disabled = true; 416 - 417 - morph(a, b); 418 - 419 - expect(a.outerHTML).to.equal(b.outerHTML); 420 - expect(a.disabled).to.equal(true); 421 - }); 422 - 423 - it("if new tree has disabled=false and old tree has disabled mutated to true, set value from new tree", async () => { 424 - const a = await fixture(html`<input type="checkbox" disabled=${false} />`); 425 - const b = await fixture(html`<input type="checkbox" />`); 426 - b.disabled = true; 427 - 428 - morph(a, b); 429 - 430 - expect(a.outerHTML).to.equal(b.outerHTML); 431 - expect(a.disabled).to.equal(true); 432 - }); 433 - 434 - it("if new tree has disabled=true and old tree has disabled mutated to false, set value from new tree", async () => { 435 - const a = await fixture(html`<input type="checkbox" disabled=${true} />`); 436 - const b = await fixture(html`<input type="checkbox" />`); 437 - b.disabled = false; 438 - 439 - morph(a, b); 440 - 441 - expect(a.outerHTML).to.equal(b.outerHTML); 442 - expect(a.disabled).to.equal(false); 443 - }); 444 - 445 - it("if new tree has no disabled and old tree has disabled=true, set value from new tree", async () => { 446 - const a = await fixture(html`<input type="checkbox" />`); 447 - const b = await fixture(html`<input type="checkbox" disabled=${true} />`); 448 - 449 - morph(a, b); 450 - 451 - expect(a.outerHTML).to.equal(b.outerHTML); 452 - expect(a.disabled).to.equal(true); 453 - }); 454 - }); 455 - 456 - describe("indeterminate", () => { 457 - it("if new tree has no indeterminate and old tree has indeterminate mutated to true, set value from new tree", async () => { 458 - const a = await fixture(html`<input type="checkbox" />`); 459 - const b = await fixture(html`<input type="checkbox" />`); 460 - b.indeterminate = true; 461 - 462 - morph(a, b); 463 - 464 - expect(a.outerHTML).to.equal(b.outerHTML); 465 - expect(a.indeterminate).to.equal(true); 466 - }); 467 - 468 - it("if new tree has no indeterminate and old tree has indeterminate mutated to false, set value from new tree", async () => { 469 - const a = await fixture(html`<input type="checkbox" />`); 470 - const b = await fixture(html`<input type="checkbox" />`); 471 - b.indeterminate = false; 472 - 473 - morph(a, b); 474 - 475 - expect(a.outerHTML).to.equal(b.outerHTML); 476 - expect(a.indeterminate).to.equal(false); 477 - }); 478 - }); 479 - }); 480 - 481 - describe("lists", () => { 482 - it("should append nodes", async () => { 483 - const a = await fixture(html`<ul></ul>`); 484 - const b = await fixture( 485 - html`<ul> 486 - <li>1</li> 487 - <li>2</li> 488 - <li>3</li> 489 - <li>4</li> 490 - <li>5</li> 491 - </ul>`, 492 - ); 493 - 494 - morph(a, b); 495 - 496 - expect(a.outerHTML).to.equal(b.outerHTML); 497 - }); 498 - 499 - it("should remove nodes", async () => { 500 - const a = await fixture( 501 - html`<ul> 502 - <li>1</li> 503 - <li>2</li> 504 - <li>3</li> 505 - <li>4</li> 506 - <li>5</li> 507 - </ul>`, 508 - ); 509 - const b = await fixture(html`<ul></ul>`); 510 - 511 - morph(a, b); 512 - 513 - expect(a.outerHTML).to.equal(b.outerHTML); 514 - }); 515 - }); 516 - 517 - describe("selectables", () => { 518 - it('should append nodes', async () => { 519 - const a = await fixture(html`<select></select>`); 520 - const b = await fixture(html`<select><option>1</option><option>2</option><option>3</option><option>4</option></select>`); 521 - 522 - morph(a, b); 523 - 524 - expect(a.outerHTML).to.equal(b.outerHTML); 525 - }); 526 - 527 - it('should append nodes (including optgroups)', async () => { 528 - const a = await fixture(html`<select></select>`); 529 - const b = await fixture(html`<select><optgroup><option>1</option><option>2</option></optgroup><option>3</option><option>4</option></select>`); 530 - 531 - morph(a, b); 532 - 533 - expect(a.outerHTML).to.equal(b.outerHTML); 534 - }); 535 - 536 - it('should remove nodes', async () => { 537 - const a = await fixture(html`<select><option>1</option><option>2</option><option>3</option><option>4</option></select>`); 538 - const b = await fixture(html`<select></select>`); 539 - 540 - morph(a, b); 541 - 542 - expect(a.outerHTML).to.equal(b.outerHTML); 543 - }); 544 - 545 - it('should remove nodes (including optgroups)', async () => { 546 - const a = await fixture(html`<select><optgroup><option>1</option><option>2</option></optgroup><option>3</option><option>4</option></select>`); 547 - const b = await fixture(html`<select></select>`); 548 - 549 - morph(a, b); 550 - 551 - expect(a.outerHTML).to.equal(b.outerHTML); 552 - }); 553 - 554 - it('should add selected', async () => { 555 - const a = await fixture(html`<select><option>1</option><option>2</option></select>`); 556 - const b = await fixture(html`<select><option>1</option><option selected>2</option></select>`); 557 - 558 - morph(a, b); 559 - 560 - expect(a.outerHTML).to.equal(b.outerHTML); 561 - }); 562 - 563 - it('should add selected (xhtml)', async () => { 564 - const a = await fixture(html`<select><option>1</option><option>2</option></select>`); 565 - const b = await fixture(html`<select><option>1</option><option selected="selected">2</option></select>`); 566 - 567 - morph(a, b); 568 - 569 - expect(a.outerHTML).to.equal(b.outerHTML); 570 - }); 571 - 572 - it('should switch selected', async () => { 573 - const a = await fixture(html`<select><option selected="selected">1</option><option>2</option></select>`); 574 - const b = await fixture(html`<select><option>1</option><option selected="selected">2</option></select>`); 575 - 576 - morph(a, b); 577 - 578 - expect(a.outerHTML).to.equal(b.outerHTML); 579 - }); 580 - }); 581 - 582 - it("should replace nodes", async () => { 583 - const a = await fixture(html`<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>`); 584 - const b = await fixture(html`<ul><div>1</div><li>2</li><p>3</p><li>4</li><li>5</li></ul>`); 585 - 586 - morph(a, b); 587 - 588 - expect(a.outerHTML).to.equal(b.outerHTML); 589 - }); 590 - 591 - it("should replace nodes after multiple iterations", async () => { 592 - const a = await fixture(html`<ul></ul>`); 593 - const b = await fixture(html`<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>`); 594 - 595 - morph(a, b); 596 - 597 - expect(a.outerHTML).to.equal(b.outerHTML); 598 - 599 - const c = await fixture(html`<ul><div>1</div><li>2</li><p>3</p><li>4</li><li>5</li></ul>`); 600 - 601 - morph(a, c); 602 - 603 - expect(a.outerHTML).to.equal(c.outerHTML); 604 - }); 605 - 606 - describe('use id as a key hint', () => { 607 - it('appends an element', async () => { 608 - const a = await fixture(html`<ul> 609 - <li id="a"></li> 610 - <li id="b"></li> 611 - <li id="c"></li> 612 - </ul>`); 613 - const b = await fixture(html`<ul> 614 - <li id="a"></li> 615 - <li id="new"></li> 616 - <li id="b"></li> 617 - <li id="c"></li> 618 - </ul>`); 619 - 620 - const oldFirst = a.children[0]; 621 - const oldSecond = a.children[1]; 622 - const oldThird = a.children[2]; 623 - 624 - morph(a, b); 625 - 626 - expect(a.outerHTML).to.equal(b.outerHTML); 627 - expect(a.children[0]).to.equal(oldFirst); 628 - expect(a.children[1]).to.equal(oldSecond); 629 - expect(a.children[2]).to.equal(oldThird); 630 - }); 631 - 632 - it('handles non-id elements', async () => { 633 - const a = await fixture(html`<ul> 634 - <li></li> 635 - <li id="a"></li> 636 - <li id="b"></li> 637 - <li id="c"></li> 638 - <li></li> 639 - </ul>`); 640 - const b = await fixture(html`<ul> 641 - <li></li> 642 - <li id="a"></li> 643 - <li id="new"></li> 644 - <li id="b"></li> 645 - <li id="c"></li> 646 - <li></li> 647 - </ul>`); 648 - 649 - const oldSecond = a.children[1]; 650 - const oldThird = a.children[2]; 651 - const oldFourth = a.children[3]; 652 - 653 - morph(a, b); 654 - 655 - expect(a.outerHTML).to.equal(b.outerHTML); 656 - expect(a.children[1]).to.equal(oldSecond); 657 - expect(a.children[3]).to.equal(oldThird); 658 - expect(a.children[4]).to.equal(oldFourth); 659 - }); 660 - 661 - it('copies over children', async () => { 662 - const a = await fixture(html`<section>'hello'<section>`); 663 - const b = await fixture(html`<section><div></div><section>`); 664 - 665 - morph(a, b); 666 - 667 - expect(a.outerHTML).to.equal(b.outerHTML); 668 - }) 669 - 670 - it('removes an element', async () => { 671 - const a = await fixture(html`<ul><li id="a"></li><li id="b"></li><li id="c"></li></ul>`); 672 - const b = await fixture(html`<ul><li id="a"></li><li id="c"></li></ul>`); 673 - 674 - const oldFirst = a.children[0]; 675 - const oldThird = a.children[2]; 676 - 677 - morph(a, b); 678 - 679 - expect(a.outerHTML).to.equal(b.outerHTML); 680 - expect(a.children[0]).to.equal(oldFirst); 681 - expect(a.children[1]).to.equal(oldThird); 682 - }) 683 - 684 - it('id match still morphs', async () => { 685 - const a = await fixture(html`<li id="12">FOO</li>`); 686 - const b = await fixture(html`<li id="12">BAR</li>`); 687 - 688 - morph(a, b); 689 - 690 - expect(a.outerHTML).to.equal(b.outerHTML); 691 - }) 692 - 693 - it('removes orphaned keyed nodes', async () => { 694 - const a = await fixture(html` 695 - <div> 696 - <div>1</div> 697 - <li id="a">a</li> 698 - </div> 699 - `); 700 - const b = await fixture(html` 701 - <div> 702 - <div>2</div> 703 - <li id="b">b</li> 704 - </div> 705 - `); 706 - 707 - morph(a, b); 708 - 709 - expect(a.outerHTML).to.equal(b.outerHTML); 710 - }) 711 - 712 - it('whitespace', async () => { 713 - const a = await fixture(html`<ul> </ul>`); 714 - const b = await fixture(html`<ul><li></li><li></li> </ul>`); 715 - 716 - morph(a, b); 717 - 718 - expect(a.outerHTML).to.equal(b.outerHTML); 719 - }) 720 - }); 721 - 722 - it('allows morphing from Node to NodeList', async () => { 723 - const a = await fixture(html`<div><div>a</div></div>`); 724 - const b = await fixture(html`<div>a</div><div>b</div>`); 725 - 726 - morph(a, b); 727 - 728 - expect(a.outerHTML).to.equal(b.outerHTML); 729 - }) 730 - 731 - it('allows morphing from NodeList to Node', async () => { 732 - const a = await fixture(html`<div>a</div><div>b</div>`); 733 - const b = await fixture(html`<div><div>a</div></div>`); 734 - 735 - morph(a, b); 736 - 737 - expect(a.outerHTML).to.equal(b.outerHTML); 738 - }) 739 - 740 - it('allows morphing from NodeList to NodeList', async () => { 741 - const a = await fixture(html`<div>a</div><div>b</div>`); 742 - const b = await fixture(html`<div>z</div><div>y</div>`); 743 - 744 - morph(a, b); 745 - 746 - expect(a.outerHTML).to.equal(b.outerHTML); 747 - }) 748 - 749 - it('allows morphing from Node to Node', async () => { 750 - const a = await fixture(html`<div><div>a</div></div>`); 751 - const b = await fixture(html`<div><div>b</div></div>`); 752 - 753 - morph(a, b); 754 - 755 - expect(a.outerHTML).to.equal(b.outerHTML); 756 - }) 757 40 });