From 4721e95936fb78a8a07ae739f1f71236e7b0f2d4 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Mon, 16 Dec 2024 16:10:45 -0600 Subject: [PATCH 1/3] basic test coverage for hooks. --- test/hooks.js | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 test/hooks.js diff --git a/test/hooks.js b/test/hooks.js new file mode 100644 index 0000000..2b11489 --- /dev/null +++ b/test/hooks.js @@ -0,0 +1,178 @@ +describe("lifecycle hooks", function(){ + beforeEach(function() { + clearWorkArea(); + }); + + it('calls beforeNodeAdded before a new node is added to the DOM', function(){ + let calls = []; + let initial = make(""); + Idiomorph.morph(initial, "", { callbacks: { + beforeNodeAdded: node => { + calls.push(node.outerHTML); + } + } }); + initial.outerHTML.should.equal(""); + calls.should.eql(["
  • B
  • "]); + }); + + it('returning false to beforeNodeAdded prevents adding the node', function(){ + let initial = make(""); + Idiomorph.morph(initial, "", { callbacks: { + beforeNodeAdded: node => false, + } }); + initial.outerHTML.should.equal(""); + }); + + it('calls afterNodeAdded after a new node is added to the DOM', function(){ + let calls = []; + let initial = make(""); + Idiomorph.morph(initial, "", { callbacks: { + afterNodeAdded: node => { + calls.push(node.outerHTML); + } + } }); + initial.outerHTML.should.equal(""); + calls.should.eql(["
  • B
  • "]); + }); + + it('calls beforeNodeMorphed before a node is morphed', function(){ + let calls = []; + let initial = make(``); + Idiomorph.morph(initial, ``, { callbacks: { + beforeNodeMorphed: (oldNode, newNode) => { + calls.push([ + oldNode.outerHTML || oldNode.textContent, + newNode.outerHTML || newNode.textContent, + ]); + } + } }); + initial.outerHTML.should.equal(``); + calls.should.eql([ + [``, ``], + [`
  • A
  • `, `
  • B
  • `], + [`A`, `B`], + ]); + }); + + it('returning false to beforeNodeMorphed prevents morphing the node', function(){ + let initial = make(``); + Idiomorph.morph(initial, ``, { callbacks: { + beforeNodeMorphed: node => { + if(node.nodeType === Node.TEXT_NODE) return false + } + } }) + initial.outerHTML.should.equal(``); + }); + + it.skip('calls afterNodeMorphed before a node is morphed', function(){ + let calls = []; + let initial = make(``); + Idiomorph.morph(initial, ``, { callbacks: { + afterNodeMorphed: (oldNode, newNode) => { + calls.push([ + oldNode.outerHTML || oldNode.textContent, + newNode.outerHTML || newNode.textContent, + ]); + } + } }); + initial.outerHTML.should.equal(``); + calls.should.eql([ + [`A`, `B`], + [`
  • A
  • `, `
  • B
  • `], + [``, ``], + ]); + }); + + it('calls beforeNodeRemoved before a node is removed from the DOM', function(){ + let calls = []; + let initial = make(""); + Idiomorph.morph(initial, "", { callbacks: { + beforeNodeRemoved: node => { + calls.push(node.outerHTML); + } + } }); + initial.outerHTML.should.equal(""); + calls.should.eql(["
  • B
  • "]); + }); + + it('returning false to beforeNodeRemoved prevents removing the node', function(){ + let initial = make(""); + Idiomorph.morph(initial, "", { callbacks: { + beforeNodeRemoved: node => false, + } }); + initial.outerHTML.should.equal(""); + }); + + it('calls afterNodeRemoved after a node is removed from the DOM', function(){ + let calls = []; + let initial = make(""); + Idiomorph.morph(initial, "", { callbacks: { + afterNodeRemoved: node => { + calls.push(node.outerHTML); + } + } }); + initial.outerHTML.should.equal(""); + calls.should.eql(["
  • B
  • "]); + }); + + it.skip('calls beforeAttributeUpdated when an attribute is added', function(){ + let calls = []; + let initial = make(""); + Idiomorph.morph(initial, ``, { callbacks: { + beforeAttributeUpdated: (attributeName, node, mutationType) => { + calls.push([attributeName, node.outerHTML, mutationType]); + } + } }); + initial.outerHTML.should.equal(``); + calls.should.eql([["href", ``, "update"]]); + }); + + it.skip('calls beforeAttributeUpdated when an attribute is updated', function(){ + let calls = []; + let initial = make(``); + Idiomorph.morph(initial, ``, { callbacks: { + beforeAttributeUpdated: (attributeName, node, mutationType) => { + calls.push([attributeName, node.outerHTML, mutationType]); + } + } }); + initial.outerHTML.should.equal(``); + calls.should.eql([["href", ``, "update"]]); + }); + + it('calls beforeAttributeUpdated when an attribute is removed', function(){ + let calls = []; + let initial = make(``); + Idiomorph.morph(initial, ``, { callbacks: { + beforeAttributeUpdated: (attributeName, node, mutationType) => { + calls.push([attributeName, node.outerHTML, mutationType]); + } + } }); + initial.outerHTML.should.equal(``); + calls.should.eql([["href", ``, "remove"]]); + }); + + it('returning false to beforeAttributeUpdated prevents the attribute addition', function(){ + let initial = make(""); + Idiomorph.morph(initial, ``, { callbacks: { + beforeAttributeUpdated: () => false, + } }); + initial.outerHTML.should.equal(``); + }); + + it('returning false to beforeAttributeUpdated prevents the attribute update', function(){ + let initial = make(``); + Idiomorph.morph(initial, ``, { callbacks: { + beforeAttributeUpdated: () => false, + } }); + initial.outerHTML.should.equal(``); + }); + + it('returning false to beforeAttributeUpdated prevents the attribute removal', function(){ + let initial = make(``); + Idiomorph.morph(initial, ``, { callbacks: { + beforeAttributeUpdated: () => false, + } }); + initial.outerHTML.should.equal(``); + }); +}); + From da7ade6733ad7ef4aa81e5b7e239aebede16b100 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Tue, 17 Dec 2024 15:34:02 -0600 Subject: [PATCH 2/3] fix extraneous 'remove' attribute hook call. --- src/idiomorph.js | 6 +++--- test/hooks.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/idiomorph.js b/src/idiomorph.js index a454dcc..df55f3a 100644 --- a/src/idiomorph.js +++ b/src/idiomorph.js @@ -439,10 +439,10 @@ var Idiomorph = (function () { // iterate backwards to avoid skipping over items when a delete occurs for (let i = toAttributes.length - 1; 0 <= i; i--) { const toAttribute = toAttributes[i]; - if (ignoreAttribute(toAttribute.name, toEl, 'remove', ctx)) { - continue; - } if (!fromEl.hasAttribute(toAttribute.name)) { + if (ignoreAttribute(toAttribute.name, toEl, 'remove', ctx)) { + continue; + } toEl.removeAttribute(toAttribute.name); } } diff --git a/test/hooks.js b/test/hooks.js index 2b11489..ce6f5b7 100644 --- a/test/hooks.js +++ b/test/hooks.js @@ -115,7 +115,7 @@ describe("lifecycle hooks", function(){ calls.should.eql(["
  • B
  • "]); }); - it.skip('calls beforeAttributeUpdated when an attribute is added', function(){ + it('calls beforeAttributeUpdated when an attribute is added', function(){ let calls = []; let initial = make(""); Idiomorph.morph(initial, ``, { callbacks: { @@ -127,7 +127,7 @@ describe("lifecycle hooks", function(){ calls.should.eql([["href", ``, "update"]]); }); - it.skip('calls beforeAttributeUpdated when an attribute is updated', function(){ + it('calls beforeAttributeUpdated when an attribute is updated', function(){ let calls = []; let initial = make(``); Idiomorph.morph(initial, ``, { callbacks: { From 350bda2650fdf453103f558a3d12c129e09be8b3 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Tue, 17 Dec 2024 15:34:15 -0600 Subject: [PATCH 3/3] is this correct, or a bug? --- test/hooks.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/hooks.js b/test/hooks.js index ce6f5b7..ceae148 100644 --- a/test/hooks.js +++ b/test/hooks.js @@ -64,7 +64,7 @@ describe("lifecycle hooks", function(){ initial.outerHTML.should.equal(`
    • A
    `); }); - it.skip('calls afterNodeMorphed before a node is morphed', function(){ + it('calls afterNodeMorphed before a node is morphed', function(){ let calls = []; let initial = make(`
    • A
    `); Idiomorph.morph(initial, `
    • B
    `, { callbacks: { @@ -77,9 +77,9 @@ describe("lifecycle hooks", function(){ } }); initial.outerHTML.should.equal(`
    • B
    `); calls.should.eql([ - [`A`, `B`], - [`
  • A
  • `, `
  • B
  • `], - [`
    • A
    `, `
    • B
    `], + [`B`, `B`], + [`
  • B
  • `, `
  • B
  • `], + [`
    • B
    `, `
    • B
    `], ]); });