describe('Morph Swap Styles Tests', function() {
beforeEach(function() {
setupTest();
});
afterEach(function() {
cleanupTest();
});
describe('innerMorph', function() {
it('morphs children while preserving element with matching id', async function() {
mockResponse('GET', '/test', '
updated
new
');
const div = createProcessedHTML('');
const child1 = div.querySelector('#child1');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.equal(div.querySelector('#child1'), child1, 'Element with id should be preserved');
assert.equal(child1.textContent, 'updated');
assert.isNotNull(div.querySelector('#child2'));
});
it('morphs text content', async function() {
mockResponse('GET', '/test', 'new text
');
const div = createProcessedHTML('');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.include(div.innerHTML, 'new text');
assert.notInclude(div.innerHTML, 'old text');
});
it('morphs attributes', async function() {
mockResponse('GET', '/test', 'content
');
const div = createProcessedHTML('');
const child = div.querySelector('#child');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.equal(child.className, 'new-class');
assert.equal(child.getAttribute('data-value'), '123');
});
it('removes old elements not in new content', async function() {
mockResponse('GET', '/test', 'kept
');
const div = createProcessedHTML('');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.isNotNull(div.querySelector('#keep'));
assert.isNull(div.querySelector('#remove'));
});
it('adds new elements', async function() {
mockResponse('GET', '/test', 'old
new
');
const div = createProcessedHTML('');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.isNotNull(div.querySelector('#old'));
assert.isNotNull(div.querySelector('#new'));
});
it('preserves element references with matching ids', async function() {
mockResponse('GET', '/test', '');
const div = createProcessedHTML('');
const input1 = div.querySelector('#input1');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.equal(div.querySelector('#input1'), input1, 'Input element should be same reference');
assert.equal(input1.value, 'new');
assert.isNotNull(div.querySelector('#input2'));
});
it('handles nested elements with ids', async function() {
mockResponse('GET', '/test', '');
const div = createProcessedHTML('');
const outer = div.querySelector('#outer');
const inner = div.querySelector('#inner');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.equal(div.querySelector('#outer'), outer);
assert.equal(div.querySelector('#inner'), inner);
assert.equal(inner.textContent, 'updated');
});
it('morphs without ids using tag matching', async function() {
mockResponse('GET', '/test', 'new paragraph
new span');
const div = createProcessedHTML('');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.include(div.innerHTML, 'new paragraph');
assert.include(div.innerHTML, 'new span');
});
});
describe('outerMorph', function() {
it('morphs the target element itself', async function() {
mockResponse('GET', '/test', 'updated
');
const container = createProcessedHTML('');
const target = container.querySelector('#target');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'});
const newTarget = container.querySelector('#target');
assert.equal(newTarget, target, 'Target element should be morphed, not replaced');
assert.equal(newTarget.className, 'new-class');
assert.equal(newTarget.textContent, 'updated');
});
it('morphs target attributes', async function() {
mockResponse('GET', '/test', 'content
');
const container = createProcessedHTML('');
const target = container.querySelector('#target');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'});
assert.equal(container.querySelector('#target'), target);
assert.equal(target.className, 'morphed');
assert.equal(target.getAttribute('data-value'), '123');
});
it('morphs target children', async function() {
mockResponse('GET', '/test', 'new child
');
const container = createProcessedHTML('');
const target = container.querySelector('#target');
const child = target.querySelector('#child');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'});
assert.equal(container.querySelector('#target'), target);
assert.equal(target.querySelector('#child'), child);
assert.equal(child.textContent, 'new child');
});
it('preserves element identity during morph', async function() {
mockResponse('GET', '/test', '');
const container = createProcessedHTML('');
const btn = container.querySelector('#btn');
let clicked = false;
btn.addEventListener('click', () => clicked = true);
await htmx.ajax('GET', '/test', {target: '#btn', swap: 'outerMorph'});
const newBtn = container.querySelector('#btn');
assert.equal(newBtn, btn, 'Button should be same element');
newBtn.click();
assert.isTrue(clicked, 'Event listener should still work');
});
});
describe('innerMorph vs innerHTML comparison', function() {
it('innerMorph preserves elements, innerHTML replaces', async function() {
mockResponse('GET', '/test', '');
// Test innerHTML
const div1 = createProcessedHTML('');
const input1 = div1.querySelector('#field');
await htmx.ajax('GET', '/test', {target: '#target1', swap: 'innerHTML'});
const newInput1 = div1.querySelector('#field');
assert.notEqual(newInput1, input1, 'innerHTML should create new element');
// Test innerMorph
mockResponse('GET', '/test', '');
const div2 = createProcessedHTML('');
const input2 = div2.querySelector('#field');
await htmx.ajax('GET', '/test', {target: '#target2', swap: 'innerMorph'});
const newInput2 = div2.querySelector('#field');
assert.equal(newInput2, input2, 'innerMorph should preserve element');
});
});
describe('outerMorph vs outerHTML comparison', function() {
it('outerMorph preserves target, outerHTML replaces', async function() {
mockResponse('GET', '/test', 'updated
');
// Test outerHTML
const container1 = createProcessedHTML('');
const target1 = container1.querySelector('#target');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerHTML'});
const newTarget1 = container1.querySelector('#target');
assert.notEqual(newTarget1, target1, 'outerHTML should create new element');
// Test outerMorph
mockResponse('GET', '/test', 'updated
');
const container2 = createProcessedHTML('');
const target2 = container2.querySelector('#target');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'outerMorph'});
const newTarget2 = container2.querySelector('#target');
assert.equal(newTarget2, target2, 'outerMorph should preserve element');
});
});
describe('edge cases', function() {
it('handles empty content', async function() {
mockResponse('GET', '/test', '');
const div = createProcessedHTML('');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.equal(div.innerHTML, '');
});
it('handles complex nested structures', async function() {
mockResponse('GET', '/test',
'');
const div = createProcessedHTML(
'');
const a = div.querySelector('#a');
const b = div.querySelector('#b');
const c = div.querySelector('#c');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.equal(div.querySelector('#a'), a);
assert.equal(div.querySelector('#b'), b);
assert.equal(div.querySelector('#c'), c);
assert.equal(c.textContent, 'updated');
});
it('handles mixed content with and without ids', async function() {
mockResponse('GET', '/test',
'has id
no id
another');
const div = createProcessedHTML(
'');
const withId = div.querySelector('#with-id');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.equal(div.querySelector('#with-id'), withId);
assert.equal(withId.textContent, 'has id');
assert.isNotNull(div.querySelector('#another'));
});
it('handles numeric ids', async function() {
mockResponse('GET', '/test', '
');
const div = createProcessedHTML('
');
const hr = div.querySelector('#\\31');
await htmx.ajax('GET', '/test', {target: '#target', swap: 'innerMorph'});
assert.equal(div.querySelector('#\\31'), hr);
});
it('does not re-execute identical script tags during morph', async function() {
window._scriptMorphCount = 0;
createProcessedHTML('');
await htmx.swap({target: '#target', text: '