describe('swap() unit tests', function() {
// TODO move to __parseSwapSpec unit test
it('prepend normalizes to afterbegin', function () {
assert.equal(htmx.__parseSwapSpec('prepend').style, 'afterbegin')
})
it('append normalizes to beforeend', function () {
assert.equal(htmx.__parseSwapSpec('append').style, 'beforeend')
})
it('before normalizes to beforebegin', function () {
assert.equal(htmx.__parseSwapSpec('before').style, 'beforebegin')
})
it('after normalizes to afterend', function () {
assert.equal(htmx.__parseSwapSpec('after').style, 'afterend')
})
it('new terminology works with modifiers', function () {
assert.equal(htmx.__parseSwapSpec('prepend swap:10').style, 'afterbegin')
assert.equal(htmx.__parseSwapSpec('prepend swap:10').swap, "10")
assert.equal(htmx.__parseSwapSpec('append swap:20').style, 'beforeend')
assert.equal(htmx.__parseSwapSpec('append swap:20').swap, "20")
})
// end TODO move to __parseSwapSpec unit test
it('swaps in plain content properly', async function () {
await htmx.swap({"target":"#test-playground", "text":"Hello Swap"})
playground().innerText.should.equal("Hello Swap")
})
it('swaps in html content properly', async function () {
await htmx.swap({"target":"#test-playground", "text":"Hello Swap"})
let child = playground().children[0];
child.tagName.should.equal("A");
child.innerText.should.equal("Hello Swap")
})
it('initializes htmx content properly', async function () {
await htmx.swap({"target":"#test-playground", "text":"Hello Swap"})
let child = playground().children[0];
child.tagName.should.equal("A");
child.innerText.should.equal("Hello Swap")
assert.isNotNull(child._htmx);
})
it('swaps in plain content properly w/outerHTML', async function () {
createProcessedHTML("
")
await htmx.swap({"target":"#d1", "text":"Hello Swap", "swap" : "outerHTML"})
playground().innerText.should.equal("Hello Swap")
})
it('swaps in html content properly w/outerHTML', async function () {
createProcessedHTML("")
await htmx.swap({"target":"#d1", "text":"Hello Swap", "swap" : "outerHTML"})
let child = playground().children[0];
child.tagName.should.equal("SPAN");
child.innerText.should.equal("Hello Swap")
})
it('replaces target element w/outerHTML', async function () {
createProcessedHTML("")
let original = find('#d1');
await htmx.swap({"target":"#d1", "text":"Replaced", "swap" : "outerHTML"})
let replaced = find('#d1');
replaced.should.not.equal(original);
replaced.tagName.should.equal("SPAN");
})
it('inserts before target w/beforebegin', async function () {
createProcessedHTML("
Target
")
await htmx.swap({"target":"#d1", "text":"Before", "swap" : "beforebegin"})
let children = playground().children;
children[0].tagName.should.equal("SPAN");
children[0].innerText.should.equal("Before");
children[1].innerText.should.equal("Target");
})
it('inserts plain text before target w/beforebegin', async function () {
createProcessedHTML("
"})
window.testVar.should.equal(7);
delete window.testVar;
})
it('executes script in partial', async function () {
window.testVar = 0;
await htmx.swap({"target":"#test-playground", "text":""})
window.testVar.should.equal(8);
delete window.testVar;
})
it('executes script when wrapped in html tag', async function () {
window.testVar = 0;
await htmx.swap({"target":"#test-playground", "text":"
Content
"})
window.testVar.should.equal(9);
delete window.testVar;
})
it('executes script when wrapped in body tag', async function () {
window.testVar = 0;
await htmx.swap({"target":"#test-playground", "text":"
Content
"})
window.testVar.should.equal(10);
delete window.testVar;
})
it('replaces attributes when swapping element with same id', async function () {
createProcessedHTML("")
await htmx.swap({"target":"#d1", "text":"
Content
", "swap":"outerHTML"})
let replaced = find('#d1');
replaced.getAttribute('class').should.equal('new');
replaced.getAttribute('data-value').should.equal('2');
})
it('triggers CSS transitions during swap', async function () {
this.skip(); //fails on firefox for some reason
createProcessedHTML("
"})
triggered.should.be.true;
})
it('bubbles settle events up from replacement element after outerHTML swap', async function () {
createProcessedHTML("
Original
")
let beforeSettleTarget, afterSettleTarget;
htmx.on('htmx:before:settle', (e) => { beforeSettleTarget = e.target; });
htmx.on('htmx:after:settle', (e) => { afterSettleTarget = e.target; });
await htmx.swap({"target":"#d1", "text":"Replaced", "swap":"outerHTML"})
let replacement = find('#replaced');
assert.isNotNull(replacement);
beforeSettleTarget.should.equal(replacement);
afterSettleTarget.should.equal(replacement);
document.body.contains(beforeSettleTarget).should.be.true;
document.body.contains(afterSettleTarget).should.be.true;
})
it('bubbles settle events after outerHTML swap when target has no previous sibling', async function () {
createProcessedHTML("
First
Second
")
let settleTarget;
htmx.on('htmx:after:settle', (e) => { settleTarget = e.target; });
await htmx.swap({"target":"#d1", "text":"New", "swap":"outerHTML"})
let replacement = find('#new-first');
assert.isNotNull(replacement);
settleTarget.should.equal(replacement);
document.body.contains(settleTarget).should.be.true;
})
it('bubbles settle events when outerHTML replacement content starts with a text node', async function () {
createProcessedHTML("
Original
")
let settleTarget;
htmx.on('htmx:after:settle', (e) => { settleTarget = e.target; });
await htmx.swap({"target":"#d1", "text":"Hello World", "swap":"outerHTML"})
playground().innerText.should.contain("Hello")
playground().innerText.should.contain("World")
assert.isOk(settleTarget);
document.body.contains(settleTarget).should.be.true;
})
it('bubbles settle events when outerHTML target comes after a text node', async function () {
playground().innerHTML = "Text before
Original
";
htmx.process(playground());
let settleTarget;
htmx.on('htmx:after:settle', (e) => { settleTarget = e.target; });
await htmx.swap({"target":"#d1", "text":"Replaced", "swap":"outerHTML"})
let replacement = find('#replaced');
assert.isNotNull(replacement);
settleTarget.should.equal(replacement);
document.body.contains(settleTarget).should.be.true;
})
it('bubbles settle events when outerHTML swap empties the parent (empty fragment, sole child)', async function () {
createProcessedHTML("
Original
")
let wrapper = find('#wrapper');
let beforeSettleTarget, afterSettleTarget;
wrapper.addEventListener('htmx:before:settle', (e) => { beforeSettleTarget = e.target; });
wrapper.addEventListener('htmx:after:settle', (e) => { afterSettleTarget = e.target; });
await htmx.swap({"target":"#d1", "text":"", "swap":"outerHTML"})
wrapper.children.length.should.equal(0);
beforeSettleTarget.should.equal(wrapper);
afterSettleTarget.should.equal(wrapper);
})
it('bubbles settle events when target follows a text node AND replacement starts with a text node', async function () {
playground().innerHTML = "Before text
Original
";
htmx.process(playground());
let settleTarget;
htmx.on('htmx:after:settle', (e) => { settleTarget = e.target; });
await htmx.swap({"target":"#d1", "text":"inserted text After", "swap":"outerHTML"})
assert.isOk(settleTarget);
document.body.contains(settleTarget).should.be.true;
playground().innerText.should.contain("Before text inserted text After");
})
it('triggers view transition events with transition:true', async function () {
if (!document.startViewTransition) {
this.skip();
return;
}
let beforeTriggered = false;
let afterTriggered = false;
htmx.on('htmx:before:viewTransition', () => {
beforeTriggered = true;
});
htmx.on('htmx:after:viewTransition', () => {
afterTriggered = true;
});
await htmx.swap({"target":"#test-playground", "text":"
Content
", "swap":"innerHTML transition:true"})
beforeTriggered.should.be.true;
afterTriggered.should.be.true;
})
it('sets document title from response', async function () {
let originalTitle = document.title;
await htmx.swap({"target":"#test-playground", "text":"New Title
Content
"})
document.title.should.equal('New Title');
document.title = originalTitle;
})
it('ignores title when ignoreTitle:true modifier is set', async function () {
let originalTitle = document.title;
await htmx.swap({"target":"#test-playground", "text":"Ignored Title
Content
", "swap":"innerHTML ignoreTitle:true"})
document.title.should.equal(originalTitle);
})
it('sets title from fragment without html/body tags', async function () {
let originalTitle = document.title;
await htmx.swap({"target":"#test-playground", "text":"Fragment Title
Content
"})
document.title.should.equal('Fragment Title');
document.title = originalTitle;
})
it('does not set title when response has no title tag', async function () {
let originalTitle = document.title;
await htmx.swap({"target":"#test-playground", "text":"
Content without title
"})
document.title.should.equal(originalTitle);
})
it('sets title with oob swap', async function () {
let originalTitle = document.title;
createProcessedHTML("")
await htmx.swap({"target":"#d1", "text":"OOB Title
Main
OOB
"})
document.title.should.equal('OOB Title');
document.title = originalTitle;
})
it('sets title with partial swap', async function () {
let originalTitle = document.title;
createProcessedHTML("")
await htmx.swap({"target":"#test-playground", "text":"Partial TitlePartial Content"})
document.title.should.equal('Partial Title');
document.title = originalTitle;
})
it('sets title from body tag response', async function () {
let originalTitle = document.title;
await htmx.swap({"target":"#test-playground", "text":"Body Title
Content
"})
document.title.should.equal('Body Title');
document.title = originalTitle;
})
it('decodes HTML entities in title', async function () {
let originalTitle = document.title;
await htmx.swap({"target":"#test-playground", "text":"</> htmx & friends
Content
"})
document.title.should.equal('> htmx & friends');
document.title = originalTitle;
})
it('does not swap title tag into page content', async function () {
await htmx.swap({"target":"#test-playground", "text":"Test Title
Main Content
"})
assert.isNull(playground().querySelector('title'));
find('#content').innerText.should.equal('Main Content');
})
it('supports autofocus', async function () {
let originalTitle = document.title;
await htmx.swap({"target":"#test-playground", "text":""})
document.activeElement.id.should.equal("i1")
})
it('swaps both main target and partial target when both are present', async function () {
createProcessedHTML("
Hello
OOB
")
await htmx.swap({
"target":"#target",
"text":"
Hello me!
OOB swap!
"
})
find('#target').textContent.should.equal("Hello me!");
find('#target_oob').textContent.should.equal("OOB swap!");
})
it('swaps only partial target when response contains only partial', async function () {
createProcessedHTML("
Original
OOB Original
")
await htmx.swap({
"target":"#target",
"text":"
OOB Updated
"
})
find('#target').textContent.should.equal("Original");
find('#target_oob').textContent.should.equal("OOB Updated");
})
it('does not swap main target when only whitespace and partial present', async function () {
createProcessedHTML("
\n"
})
find('#target').textContent.should.equal("Original");
find('#target_oob').textContent.should.equal("OOB swap!");
})
it('swaps both targets when empty element and partial present', async function () {
createProcessedHTML("
Original
OOB
")
await htmx.swap({
"target":"#target",
"text":"
OOB swap!
"
})
find('#target').querySelector('p').should.not.be.null;
find('#target_oob').textContent.should.equal("OOB swap!");
})
it('swaps both targets when plain text and partial present', async function () {
createProcessedHTML("
"
})
find('#target').textContent.should.equal("Hello");
find('#target_oob').innerText.should.equal("OOB swap!");
})
it('restores focus after innerHTML swap when element has same id', async function () {
createProcessedHTML("")
let input = find('#focused-input')
input.focus()
input.setSelectionRange(2, 2)
await htmx.swap({"target":"#test-playground", "text":""})
document.activeElement.id.should.equal('focused-input')
document.activeElement.selectionStart.should.equal(2)
document.activeElement.selectionEnd.should.equal(2)
})
it('restores focus after outerHTML swap when element has same id', async function () {
createProcessedHTML("")
let input = find('#focused-input')
input.focus()
input.setSelectionRange(1, 3)
await htmx.swap({"target":"#container", "text":"", "swap":"outerHTML"})
document.activeElement.id.should.equal('focused-input')
document.activeElement.selectionStart.should.equal(1)
document.activeElement.selectionEnd.should.equal(3)
})
it('does not restore focus when focused element has no id', async function () {
createProcessedHTML("")
let input = playground().querySelector('input')
input.focus()
await htmx.swap({"target":"#test-playground", "text":""})
document.activeElement.should.not.equal(input)
})
it('does not restore focus when new content lacks matching id', async function () {
createProcessedHTML("")
let input = find('#focused-input')
input.focus()
await htmx.swap({"target":"#test-playground", "text":""})
document.activeElement.id.should.not.equal('focused-input')
})
it('does not restore focus for morph swaps', async function () {
createProcessedHTML("")
let input = find('#focused-input')
input.focus()
input.setSelectionRange(2, 2)
await htmx.swap({"target":"#test-playground", "text":"", "swap":"innerMorph"})
// Morph should maintain focus naturally, not through restoration
document.activeElement.should.equal(input)
})
it('__insertContent accepts string swapSpec', async function () {
createProcessedHTML("
Old
")
let fragment = document.createDocumentFragment()
fragment.append(document.createTextNode('New'))
await htmx.__insertContent({target: find('#d1'), swapSpec: 'innerHTML', fragment})
find('#d1').innerText.should.equal('New')
})
it('outerSync cleans up powered children before replacing them', async function() {
mockResponse('GET', '/endpoint', 'response')
createProcessedHTML("")
const btn = find('#btn')
assert.isNotNull(btn._htmx?.initialized, 'child should start initialized')
await htmx.swap({
target: '#target',
swap: 'outerSync',
text: '
replaced
',
sourceElement: find('#target')
})
assert.isNull(btn.getAttribute('data-htmx-powered'), 'old child should be cleaned up')
assert.isNotOk(find('#btn'), 'old button should be gone')
assert.equal(find('#target').textContent, 'replaced')
})
it('outerSync adds hx-get to root element and initializes it', async function () {
createProcessedHTML("
',
sourceElement: find('#target')
})
let target = find('#target')
target.getAttribute('hx-get').should.equal('/dynamic')
assert.isNotNull(target._htmx?.initialized, 'target root should be initialized with htmx')
target.click()
await forRequest()
target.textContent.should.equal('fetched')
})
it('outerSync removes hx-get from root element and old handler no longer fires', async function () {
mockResponse('GET', '/should-not-fire', 'bad')
createProcessedHTML("
interactive
")
let target = find('#target')
assert.isNotNull(target._htmx?.initialized, 'target should start initialized')
// outerSync removes hx-get from the root
await htmx.swap({
target: '#target',
swap: 'outerSync',
text: '
no longer interactive
',
sourceElement: target
})
target = find('#target')
target.textContent.should.equal('no longer interactive')
assert.isNull(target.getAttribute('hx-get'), 'hx-get should be removed')
// clicking should NOT issue a request — track via event
let requestFired = false
let handler = () => { requestFired = true }
document.addEventListener('htmx:before:request', handler)
target.click()
await htmx.timeout(50)
document.removeEventListener('htmx:before:request', handler)
assert.isFalse(requestFired, 'no request should fire after hx-get removed')
})
it('outerSync changes hx-trigger on root element and new trigger fires', async function () {
mockResponse('GET', '/endpoint', 'response')
createProcessedHTML("
original
")
// Change trigger from click to mousedown
await htmx.swap({
target: '#target',
swap: 'outerSync',
text: '
updated
',
sourceElement: find('#target')
})
let target = find('#target')
target.getAttribute('hx-trigger').should.equal('mousedown')
// click should no longer trigger a request
let requestFired = false
let handler = () => { requestFired = true }
document.addEventListener('htmx:before:request', handler)
target.click()
await htmx.timeout(50)
document.removeEventListener('htmx:before:request', handler)
assert.isFalse(requestFired, 'click should not trigger after hx-trigger changed to mousedown')
// mousedown should trigger the request
target.dispatchEvent(new MouseEvent('mousedown', {bubbles: true}))
await forRequest()
target.textContent.should.equal('response')
})
it('swaps partial to all elements matching a class selector', async function () {
createProcessedHTML("
A
B
")
await htmx.swap({"target":"#test-playground", "text":"Updated"})
playground().querySelectorAll('.target').forEach(el => el.innerText.should.equal('Updated'))
})
it('swapEmpty:false prevents main swap when response is only oob', async function () {
createProcessedHTML("
"})
find('#target').innerText.should.equal('Original');
find('#oob').innerText.should.equal('Updated');
})
it('swapEmpty:false prevents main swap when response is only partials', async function () {
createProcessedHTML("
Original
Partial
")
await htmx.swap({"target":"#target", "swap":"innerHTML swapEmpty:false", "text":"Updated"})
find('#target').innerText.should.equal('Original');
find('#partial').innerText.should.equal('Updated');
})
it('swapEmpty:true forces main swap even on empty response with partials', async function () {
createProcessedHTML("
Original
Partial
")
await htmx.swap({"target":"#target", "swap":"innerHTML swapEmpty:true", "text":"Updated"})
find('#target').innerText.should.equal('');
find('#partial').innerText.should.equal('Updated');
})
it('swapEmpty:false still swaps main target when response has real content alongside oob', async function () {
createProcessedHTML("