Skip to content

Commit 3ea28d4

Browse files
authored
Merge pull request #80 from botandrose/test-coverage
Add coverage for untested code paths and features; rm test noise
2 parents 917587a + 15643c3 commit 3ea28d4

File tree

8 files changed

+113
-90
lines changed

8 files changed

+113
-90
lines changed

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"chai-dom": "^1.11.0",
4343
"fs-extra": "^9.1.0",
4444
"htmx.org": "1.9.9",
45+
"p-wait-for": "^5.0.2",
4546
"sinon": "^9.2.4",
4647
"typescript": "^5.3.3",
4748
"uglify-js": "^3.15.0"

test/bootstrap.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ describe("Bootstrap test", function(){
5858
d2.innerHTML.should.equal("E");
5959
d3.innerHTML.should.equal("F");
6060

61-
console.log(morphTo);
62-
console.log(div1.outerHTML);
6361
div1.outerHTML.should.equal(morphTo)
6462

65-
setTimeout(()=> {
66-
console.log("idiomorph mutations : ", div1.mutations);
67-
done();
68-
}, 0)
63+
// // debugging output
64+
// console.log(morphTo);
65+
// console.log(div1.outerHTML);
66+
67+
// setTimeout(()=> {
68+
// console.log("idiomorph mutations : ", div1.mutations);
69+
done();
70+
// }, 0)
6971
});
7072

7173
it('deep morphdom does not work ideally', function(done)
@@ -78,11 +80,12 @@ describe("Bootstrap test", function(){
7880

7981
morphdom(div1, '<div id="root2"><div><div id="d2">E</div></div><div><div id="d3">F</div></div><div><div id="d1">D</div></div></div>', {});
8082

81-
setTimeout(()=> {
82-
console.log("morphdom mutations : ", div1.mutations);
83-
done();
84-
}, 0)
85-
print(div1);
83+
// // debugging output
84+
// setTimeout(()=> {
85+
// console.log("morphdom mutations : ", div1.mutations);
86+
done();
87+
// }, 0)
88+
// print(div1);
8689
});
8790

8891
})

test/core.js

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ describe("Core morphing tests", function(){
1414
let finalSrc = "<button>Bar</button>";
1515
let final = make(finalSrc);
1616
Idiomorph.morph(initial, final, {morphStyle:'outerHTML'});
17-
if (initial.outerHTML !== "<button>Bar</button>") {
18-
console.log("HTML after morph: " + initial.outerHTML);
19-
console.log("Expected: " + finalSrc);
20-
}
2117
initial.outerHTML.should.equal("<button>Bar</button>");
2218
});
2319

@@ -26,10 +22,6 @@ describe("Core morphing tests", function(){
2622
let initial = make("<button>Foo</button>");
2723
let finalSrc = "<button>Bar</button>";
2824
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
29-
if (initial.outerHTML !== "<button>Bar</button>") {
30-
console.log("HTML after morph: " + initial.outerHTML);
31-
console.log("Expected: " + finalSrc);
32-
}
3325
initial.outerHTML.should.equal("<button>Bar</button>");
3426
});
3527

@@ -39,10 +31,6 @@ describe("Core morphing tests", function(){
3931
let finalSrc = "<div><button>Bar</button></div>";
4032
let final = make(finalSrc).children;
4133
Idiomorph.morph(initial, final, {morphStyle:'outerHTML'});
42-
if (initial.outerHTML !== "<button>Bar</button>") {
43-
console.log("HTML after morph: " + initial.outerHTML);
44-
console.log("Expected: " + finalSrc);
45-
}
4634
initial.outerHTML.should.equal("<button>Bar</button>");
4735
});
4836

@@ -52,10 +40,6 @@ describe("Core morphing tests", function(){
5240
let finalSrc = "<div><button>Bar</button></div>";
5341
let final = [...make(finalSrc).children];
5442
Idiomorph.morph(initial, final, {morphStyle:'outerHTML'});
55-
if (initial.outerHTML !== "<button>Bar</button>") {
56-
console.log("HTML after morph: " + initial.outerHTML);
57-
console.log("Expected: " + finalSrc);
58-
}
5943
initial.outerHTML.should.equal("<button>Bar</button>");
6044
});
6145

@@ -66,10 +50,6 @@ describe("Core morphing tests", function(){
6650
let finalSrc = "<p>Foo</p><button>Bar</button><p>Bar</p>";
6751
let final = makeElements(finalSrc);
6852
Idiomorph.morph(initial, final, {morphStyle:'outerHTML'});
69-
if (initial.outerHTML !== "<button>Bar</button>") {
70-
console.log("HTML after morph: " + initial.outerHTML);
71-
console.log("Expected: " + finalSrc);
72-
}
7353
initial.outerHTML.should.equal("<button>Bar</button>");
7454
initial.parentElement.innerHTML.should.equal("<p>Foo</p><button>Bar</button><p>Bar</p>");
7555
});
@@ -81,10 +61,6 @@ describe("Core morphing tests", function(){
8161
let finalSrc = "<p>Foo</p><button>Bar</button><p>Bar</p>";
8262
let final = [...makeElements(finalSrc)];
8363
Idiomorph.morph(initial, final, {morphStyle:'outerHTML'});
84-
if (initial.outerHTML !== "<button>Bar</button>") {
85-
console.log("HTML after morph: " + initial.outerHTML);
86-
console.log("Expected: " + finalSrc);
87-
}
8864
initial.outerHTML.should.equal("<button>Bar</button>");
8965
initial.parentElement.innerHTML.should.equal("<p>Foo</p><button>Bar</button><p>Bar</p>");
9066
});
@@ -95,10 +71,6 @@ describe("Core morphing tests", function(){
9571
let initial = parent.querySelector("button");
9672
let finalSrc = "<p>Foo</p><button>Bar</button><p>Bar</p>";
9773
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
98-
if (initial.outerHTML !== "<button>Bar</button>") {
99-
console.log("HTML after morph: " + initial.outerHTML);
100-
console.log("Expected: " + finalSrc);
101-
}
10274
initial.outerHTML.should.equal("<button>Bar</button>");
10375
initial.parentElement.innerHTML.should.equal("<p>Foo</p><button>Bar</button><p>Bar</p>");
10476
});
@@ -109,10 +81,6 @@ describe("Core morphing tests", function(){
10981
let initial = parent.querySelector("button");
11082
let finalSrc = "<p>Doh</p><p>Foo</p><button>Bar</button><p>Bar</p><p>Ray</p>";
11183
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
112-
if (initial.outerHTML !== "<button>Bar</button>") {
113-
console.log("HTML after morph: " + initial.outerHTML);
114-
console.log("Expected: " + finalSrc);
115-
}
11684
initial.outerHTML.should.equal("<button>Bar</button>");
11785
initial.parentElement.innerHTML.should.equal("<p>Doh</p><p>Foo</p><button>Bar</button><p>Bar</p><p>Ray</p>");
11886
});
@@ -130,10 +98,6 @@ describe("Core morphing tests", function(){
13098
let finalSrc = "<button>Bar</button>";
13199
let final = make(finalSrc);
132100
Idiomorph.morph(initial, final, {morphStyle:'innerHTML'});
133-
if (initial.outerHTML !== "<button>Bar</button>") {
134-
console.log("HTML after morph: " + initial.outerHTML);
135-
console.log("Expected: " + finalSrc);
136-
}
137101
initial.outerHTML.should.equal("<div><button>Bar</button></div>");
138102
});
139103

@@ -142,10 +106,6 @@ describe("Core morphing tests", function(){
142106
let initial = make("<button>Foo</button>");
143107
let finalSrc = "<button>Bar</button>";
144108
Idiomorph.morph(initial, finalSrc, {morphStyle:'innerHTML'});
145-
if (initial.outerHTML !== "<button>Bar</button>") {
146-
console.log("HTML after morph: " + initial.outerHTML);
147-
console.log("Expected: " + finalSrc);
148-
}
149109
initial.outerHTML.should.equal("<button><button>Bar</button></button>");
150110
});
151111

@@ -155,10 +115,6 @@ describe("Core morphing tests", function(){
155115
let finalSrc = "<div><button>Bar</button></div>";
156116
let final = make(finalSrc).children;
157117
Idiomorph.morph(initial, final, {morphStyle:'innerHTML'});
158-
if (initial.outerHTML !== "<button>Bar</button>") {
159-
console.log("HTML after morph: " + initial.outerHTML);
160-
console.log("Expected: " + finalSrc);
161-
}
162118
initial.outerHTML.should.equal("<button><button>Bar</button></button>");
163119
});
164120

@@ -168,10 +124,6 @@ describe("Core morphing tests", function(){
168124
let finalSrc = "<div><button>Bar</button></div>";
169125
let final = [...make(finalSrc).children];
170126
Idiomorph.morph(initial, final, {morphStyle:'innerHTML'});
171-
if (initial.outerHTML !== "<button>Bar</button>") {
172-
console.log("HTML after morph: " + initial.outerHTML);
173-
console.log("Expected: " + finalSrc);
174-
}
175127
initial.outerHTML.should.equal("<button><button>Bar</button></button>");
176128
});
177129

@@ -182,6 +134,13 @@ describe("Core morphing tests", function(){
182134
initial.outerHTML.should.equal("<div></div>");
183135
});
184136

137+
it('errors on bad morphStyle', function()
138+
{
139+
(() => {
140+
Idiomorph.morph(make("<p>"), [], {morphStyle:'magic'});
141+
}).should.throw("Do not understand how to morph style magic");
142+
});
143+
185144
it('can morph a template tag properly', function()
186145
{
187146
let initial = make("<template data-old>Foo</template>");
@@ -345,10 +304,6 @@ describe("Core morphing tests", function(){
345304

346305
let finalSrc = '<textarea>bar</textarea>';
347306
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
348-
if (initial.outerHTML !== '<input value="bar">') {
349-
console.log("HTML after morph: " + initial.outerHTML);
350-
console.log("Expected: " + finalSrc);
351-
}
352307
initial.outerHTML.should.equal('<textarea>bar</textarea>');
353308

354309
initial.focus();
@@ -362,6 +317,35 @@ describe("Core morphing tests", function(){
362317
document.body.removeChild(parent);
363318
});
364319

320+
it('can morph input value properly because value property is special and doesnt reflect', function()
321+
{
322+
let initial = make('<div><input value="foo"></div>');
323+
let final = make('<input value="foo">');
324+
final.value = "bar";
325+
Idiomorph.morph(initial, final, {morphStyle:'innerHTML'});
326+
initial.innerHTML.should.equal('<input value="bar">');
327+
});
328+
329+
it('can morph textarea value properly because value property is special and doesnt reflect', function()
330+
{
331+
let initial = make('<textarea>foo</textarea>');
332+
let final = make('<textarea>foo</textarea>');
333+
final.value = "bar";
334+
Idiomorph.morph(initial, final, {morphStyle:'outerHTML'});
335+
initial.value.should.equal('bar');
336+
});
337+
338+
it('specially considers textarea value property in beforeAttributeUpdated hook because value property is special and doesnt reflect', function()
339+
{
340+
let initial = make('<div><textarea>foo</textarea></div>');
341+
let final = make('<textarea>foo</textarea>');
342+
final.value = "bar";
343+
Idiomorph.morph(initial, final, {morphStyle:'innerHTML',callbacks:{
344+
beforeAttributeUpdated: (attr, to, updatetype) => false,
345+
}});
346+
initial.innerHTML.should.equal('<textarea>foo</textarea>');
347+
});
348+
365349
it('can morph input checked properly, remove checked', function()
366350
{
367351
let parent = make('<div><input type="checkbox" checked></div>');
@@ -370,10 +354,6 @@ describe("Core morphing tests", function(){
370354

371355
let finalSrc = '<input type="checkbox">';
372356
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
373-
if (initial.outerHTML !== '<input type="checkbox">') {
374-
console.log("HTML after morph: " + initial.outerHTML);
375-
console.log("Expected: " + finalSrc);
376-
}
377357
initial.outerHTML.should.equal('<input type="checkbox">');
378358
initial.checked.should.equal(false);
379359
document.body.removeChild(parent);
@@ -387,10 +367,6 @@ describe("Core morphing tests", function(){
387367

388368
let finalSrc = '<input type="checkbox" checked>';
389369
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
390-
if (initial.outerHTML !== '<input type="checkbox" checked="">') {
391-
console.log("HTML after morph: " + initial.outerHTML);
392-
console.log("Expected: " + finalSrc);
393-
}
394370
initial.outerHTML.should.equal('<input type="checkbox" checked="">');
395371
initial.checked.should.equal(true);
396372
document.body.removeChild(parent);
@@ -405,10 +381,6 @@ describe("Core morphing tests", function(){
405381

406382
let finalSrc = '<input type="checkbox" checked>';
407383
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
408-
if (initial.outerHTML !== '<input type="checkbox" checked="true">') {
409-
console.log("HTML after morph: " + initial.outerHTML);
410-
console.log("Expected: " + finalSrc);
411-
}
412384
initial.outerHTML.should.equal('<input type="checkbox" checked="true">');
413385
initial.checked.should.equal(true);
414386
document.body.removeChild(parent);
@@ -423,10 +395,6 @@ describe("Core morphing tests", function(){
423395

424396
let finalSrc = '<input type="checkbox">';
425397
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
426-
if (initial.outerHTML !== '<input type="checkbox">') {
427-
console.log("HTML after morph: " + initial.outerHTML);
428-
console.log("Expected: " + finalSrc);
429-
}
430398
initial.outerHTML.should.equal('<input type="checkbox">');
431399
initial.checked.should.equal(false);
432400
document.body.removeChild(parent);
@@ -443,10 +411,6 @@ describe("Core morphing tests", function(){
443411
// should more inner HTML despite no config
444412
Idiomorph.morph(initial, finalSrc);
445413

446-
if (initial.outerHTML !== "<button>Bar</button>") {
447-
console.log("HTML after morph: " + initial.outerHTML);
448-
console.log("Expected: " + finalSrc);
449-
}
450414
initial.outerHTML.should.equal("<button><button>Bar</button></button>");
451415
} finally {
452416
Idiomorph.defaults.morphStyle = 'outerHTML';
@@ -464,10 +428,6 @@ describe("Core morphing tests", function(){
464428
// should morph outer HTML despite default setting
465429
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
466430

467-
if (initial.outerHTML !== "<button>Bar</button>") {
468-
console.log("HTML after morph: " + initial.outerHTML);
469-
console.log("Expected: " + finalSrc);
470-
}
471431
initial.outerHTML.should.equal("<button>Bar</button>");
472432
} finally {
473433
Idiomorph.defaults.morphStyle = 'outerHTML';

test/head.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ describe("Tests to ensure that the head tag merging works correctly", function()
7070
originalHead.childNodes[1].outerHTML.should.equal("<meta name=\"foo\" content=\"bar\">");
7171
});
7272

73+
it('ignore style ignores head', function () {
74+
let parser = new DOMParser();
75+
let document = parser.parseFromString("<html><head><title>Foo</title></head></html>", "text/html");
76+
let originalHead = document.head;
77+
Idiomorph.morph(document, "<html><head><meta name='foo' content='bar'></head></html>", {head:{ignore:true}});
78+
79+
originalHead.outerHTML.should.equal("<head><title>Foo</title></head>");
80+
});
81+
7382
it('im-preserve preserves', function () {
7483
let parser = new DOMParser();
7584
let document = parser.parseFromString("<html><head><title im-preserve='true'>Foo</title></head></html>", "text/html");
@@ -96,6 +105,21 @@ describe("Tests to ensure that the head tag merging works correctly", function()
96105
originalHead.childNodes[1].outerHTML.should.equal("<meta name=\"foo\" content=\"bar\">");
97106
});
98107

108+
it('im-re-append re-appends with append style', function () {
109+
let parser = new DOMParser();
110+
let document = parser.parseFromString("<html><head><meta name='foo' content='bar' im-re-append='true'></head></html>", "text/html");
111+
let originalHead = document.head;
112+
let originalTitle = originalHead.children[0];
113+
Idiomorph.morph(document, "<html><head><meta name='bar' content='baz' im-re-append='true'></head></html>", {head:{style:'append'}});
99114

115+
originalHead.should.equal(document.head);
116+
originalHead.childNodes.length.should.equal(2);
117+
originalHead.innerHTML.should.equal('<meta name="foo" content="bar" im-re-append="true"><meta name="bar" content="baz" im-re-append="true">');
118+
});
100119

120+
it('can handle scripts with block mode', async function(){
121+
Idiomorph.morph(window.document, `<head><script src='/test/lib/fixture.js'></script></head>`,{head:{block:true,style:'append'}});
122+
await waitFor(() => window.hasOwnProperty("fixture"))
123+
window.fixture.should.equal("FIXTURE")
124+
});
101125
});

test/lib/fixture.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
window.fixture = "FIXTURE";

0 commit comments

Comments
 (0)