| 1 |
describe('hx-disinherit attribute', function() { |
| 2 |
beforeEach(function() { |
| 3 |
this.server = makeServer() |
| 4 |
clearWorkArea() |
| 5 |
}) |
| 6 |
afterEach(function() { |
| 7 |
this.server.restore() |
| 8 |
clearWorkArea() |
| 9 |
}) |
| 10 |
|
| 11 |
it('basic inheritance sanity-check', function() { |
| 12 |
var response_inner = '<div id="snowflake" class="">Hello world</div>' |
| 13 |
var response = '<div id="unique" class="">' + response_inner + '</div>' |
| 14 |
this.server.respondWith('GET', '/test', response) |
| 15 |
|
| 16 |
var div = make('<div hx-select="#snowflake" hx-target="#cta" hx-swap="outerHTML"><button id="bx1" hx-get="/test"><span id="cta">Click Me!</span></button></div>') |
| 17 |
var btn = byId('bx1') |
| 18 |
btn.click() |
| 19 |
this.server.respond() |
| 20 |
btn.firstChild.id.should.equal('snowflake') |
| 21 |
btn.innerText.should.equal('Hello world') |
| 22 |
}) |
| 23 |
|
| 24 |
it('disinherit exclude single attribute', function() { |
| 25 |
var response_inner = '<div id="snowflake">Hello world</div>' |
| 26 |
var response = '<div id="unique">' + response_inner + '</div>' |
| 27 |
this.server.respondWith('GET', '/test', response) |
| 28 |
|
| 29 |
var div = make('<div hx-select="#snowflake" hx-target="#cta" hx-swap="beforebegin" hx-disinherit="hx-select"><button id="bx1" hx-get="/test"><span id="cta">Click Me!</span></button></div>') |
| 30 |
var btn = byId('bx1') |
| 31 |
btn.click() |
| 32 |
this.server.respond() |
| 33 |
btn.firstChild.id.should.equal('unique') |
| 34 |
btn.firstChild.firstChild.id.should.equal('snowflake') |
| 35 |
btn.childNodes[1].innerText.should.equal('Click Me!') |
| 36 |
}) |
| 37 |
|
| 38 |
it('disinherit exclude multiple attributes', function() { |
| 39 |
var response_inner = '<div id="snowflake">Hello world</div>' |
| 40 |
var response = '<div id="unique">' + response_inner + '</div>' |
| 41 |
this.server.respondWith('GET', '/test', response) |
| 42 |
|
| 43 |
var div = make('<div hx-select="#snowflake" hx-target="#cta" hx-swap="beforebegin" hx-disinherit="hx-select hx-swap">' + |
| 44 |
' <button id="bx1" hx-get="/test"><span id="cta">Click Me!</span></button>' + |
| 45 |
'</div>') |
| 46 |
var btn = byId('bx1') |
| 47 |
btn.click() |
| 48 |
this.server.respond() |
| 49 |
console.log(btn.innerHTML) |
| 50 |
console.log(response) |
| 51 |
btn.firstChild.id.should.equal('cta') |
| 52 |
btn.firstChild.firstChild.id.should.equal('unique') |
| 53 |
btn.firstChild.firstChild.firstChild.id.should.equal('snowflake') |
| 54 |
}) |
| 55 |
|
| 56 |
it('disinherit exclude all attributes', function() { |
| 57 |
var response_inner = '<div id="snowflake">Hello world</div>' |
| 58 |
var response = '<div id="unique">' + response_inner + '</div>' |
| 59 |
this.server.respondWith('GET', '/test', response) |
| 60 |
var div = make('<div hx-select="#snowflake" hx-target="#cta" hx-swap="beforebegin" hx-disinherit="*">' + |
| 61 |
' <button id="bx1" hx-get="/test">' + |
| 62 |
' <span id="cta">Click Me!</span>' + |
| 63 |
' </button>' + |
| 64 |
'</div>') |
| 65 |
var btn = byId('bx1') |
| 66 |
btn.click() |
| 67 |
this.server.respond() |
| 68 |
btn.firstChild.id.should.equal('unique') |
| 69 |
btn.firstChild.firstChild.id.should.equal('snowflake') |
| 70 |
}) |
| 71 |
|
| 72 |
it('same-element inheritance disable', function() { |
| 73 |
var response_inner = '<div id="snowflake" class="">Hello world</div>' |
| 74 |
var response = '<div id="unique">' + response_inner + '</div>' |
| 75 |
this.server.respondWith('GET', '/test', response) |
| 76 |
|
| 77 |
var btn = make('<button hx-select="#snowflake" hx-target="#container" hx-trigger="click" hx-get="/test" hx-swap="outerHTML" hx-disinherit="*"><div id="container"></div></button>') |
| 78 |
btn.click() |
| 79 |
this.server.respond() |
| 80 |
btn.firstChild.id.should.equal('snowflake') |
| 81 |
btn.firstChild.innerText.should.equal('Hello world') |
| 82 |
}) |
| 83 |
|
| 84 |
it('same-element inheritance disable with child nodes', function() { |
| 85 |
var response_inner = '<div id="snowflake" class="">Hello world</div>' |
| 86 |
var response = '<div id="unique">' + response_inner + '</div>' |
| 87 |
this.server.respondWith('GET', '/test', response) |
| 88 |
this.server.respondWith('GET', '/test2', 'unique-snowflake') |
| 89 |
|
| 90 |
var div = make('<div hx-select="#snowflake" hx-target="#container" hx-get="/test" hx-swap="outerHTML" hx-trigger="keyup" hx-disinherit="*"><div id="container"><button id="bx1" hx-get="/test2" hx-trigger="click" hx-target="#target"><div id="target"></div></button></div></div>') |
| 91 |
var btn = byId('bx1') |
| 92 |
btn.click() |
| 93 |
this.server.respond() |
| 94 |
btn.firstChild.id.should.equal('target') |
| 95 |
btn.firstChild.innerText.should.equal('unique-snowflake') |
| 96 |
var count = (div.parentElement.innerHTML.match(/snowflake/g) || []).length |
| 97 |
count.should.equal(2) |
| 98 |
}) |
| 99 |
|
| 100 |
it('boosted element hx-disinherit sanity check', function() { |
| 101 |
try { |
| 102 |
var request |
| 103 |
var handler = htmx.on('htmx:beforeRequest', function(evt) { |
| 104 |
request = evt |
| 105 |
}) |
| 106 |
var div = make('<div hx-boost="true" hx-disinherit="false"><a id="a1" href="/test">Click me</a></div>') |
| 107 |
var link = byId('a1') |
| 108 |
link.click() |
| 109 |
|
| 110 |
should.equal(request.detail.requestConfig.path, '/test') |
| 111 |
should.equal(request.detail.elt['htmx-internal-data'].boosted, true) |
| 112 |
} finally { |
| 113 |
htmx.off('htmx:beforeRequest', handler) |
| 114 |
} |
| 115 |
}) |
| 116 |
|
| 117 |
it('boosted element inheritance manual unset', function() { |
| 118 |
try { |
| 119 |
var request |
| 120 |
var handler = htmx.on('htmx:beforeRequest', function(evt) { |
| 121 |
request = evt |
| 122 |
}) |
| 123 |
var div = make('<div hx-boost="true" hx-get="/test"><div hx-boost="unset"><a id="a1" href="/test">Click me</a></div></div>') |
| 124 |
var link = byId('a1') |
| 125 |
should.equal(link['htmx-internal-data'].boosted, undefined) |
| 126 |
} finally { |
| 127 |
htmx.off('htmx:beforeRequest', handler) |
| 128 |
} |
| 129 |
}) |
| 130 |
|
| 131 |
it('nested htmx-node with boosting parent', function() { |
| 132 |
try { |
| 133 |
var request |
| 134 |
var handler = htmx.on('htmx:beforeRequest', function(evt) { |
| 135 |
request = evt |
| 136 |
}) |
| 137 |
var div = make('<div hx-boost="true" hx-target="#test" hx-disinherit="*"><div id="test"></div><a id="a1" href="/test" hx-get="/test2">Click me</a></div>') |
| 138 |
var link = byId('a1') |
| 139 |
link.click() |
| 140 |
should.equal(request.detail.requestConfig.path, '/test2') |
| 141 |
should.equal(request.detail.elt['htmx-internal-data'].boosted, undefined) |
| 142 |
should.equal(request.detail.target.id, 'a1') |
| 143 |
} finally { |
| 144 |
htmx.off('htmx:beforeRequest', handler) |
| 145 |
} |
| 146 |
}) |
| 147 |
}) |