| 1 |
describe('default extensions behavior', function() { |
| 2 |
var loadCalls, afterSwapCalls, afterSettleCalls |
| 3 |
|
| 4 |
beforeEach(function() { |
| 5 |
loadCalls = [] |
| 6 |
this.server = makeServer() |
| 7 |
clearWorkArea() |
| 8 |
|
| 9 |
htmx.defineExtension('ext-testswap', { |
| 10 |
onEvent: function(name, evt) { |
| 11 |
if (name === 'htmx:load') { |
| 12 |
loadCalls.push(evt.detail.elt) |
| 13 |
} |
| 14 |
}, |
| 15 |
handleSwap: function(swapStyle, target, fragment, settleInfo) { |
| 16 |
|
| 17 |
var parentEl = target.parentElement |
| 18 |
parentEl.removeChild(target) |
| 19 |
const arr = [] |
| 20 |
for (const child of fragment.childNodes) { |
| 21 |
arr.push(parentEl.appendChild(child)) |
| 22 |
} |
| 23 |
return arr |
| 24 |
} |
| 25 |
}) |
| 26 |
}) |
| 27 |
|
| 28 |
afterEach(function() { |
| 29 |
this.server.restore() |
| 30 |
clearWorkArea() |
| 31 |
htmx.removeExtension('ext-testswap') |
| 32 |
}) |
| 33 |
|
| 34 |
it('handleSwap: afterSwap and afterSettle triggered if extension defined on parent', function() { |
| 35 |
this.server.respondWith('GET', '/test', '<button>Clicked!</button>') |
| 36 |
var div = make('<div hx-ext="ext-testswap"><button hx-get="/test" hx-swap="testswap">Click Me!</button></div>') |
| 37 |
var btn = div.firstChild |
| 38 |
btn.click() |
| 39 |
this.server.respond() |
| 40 |
loadCalls.length.should.equal(1) |
| 41 |
loadCalls[0].textContent.should.equal('Clicked!') |
| 42 |
}) |
| 43 |
|
| 44 |
it('handleSwap: new content is handled by htmx', function() { |
| 45 |
this.server.respondWith('GET', '/test', '<button id="test-ext-testswap">Clicked!<span hx-get="/test-inner" hx-trigger="load"></span></button>') |
| 46 |
this.server.respondWith('GET', '/test-inner', 'Loaded!') |
| 47 |
make('<div hx-ext="ext-testswap"><button hx-get="/test" hx-swap="testswap">Click Me!</button></div>').querySelector('button').click() |
| 48 |
|
| 49 |
this.server.respond() |
| 50 |
var btn = byId('test-ext-testswap') |
| 51 |
btn.textContent.should.equal('Clicked!') |
| 52 |
loadCalls.length.should.equal(1) |
| 53 |
loadCalls[0].textContent.should.equal('Clicked!') |
| 54 |
|
| 55 |
this.server.respond() |
| 56 |
btn.textContent.should.equal('Clicked!Loaded!') |
| 57 |
loadCalls.length.should.equal(1) |
| 58 |
}) |
| 59 |
}) |