| 1 |
describe('process() unit tests', function() { |
| 2 |
|
| 3 |
beforeEach(function() { |
| 4 |
setupTest(); |
| 5 |
}); |
| 6 |
|
| 7 |
afterEach(function() { |
| 8 |
cleanupTest(); |
| 9 |
}); |
| 10 |
|
| 11 |
it('initializes element with hx-get', function () { |
| 12 |
let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 13 |
htmx.process(div) |
| 14 |
assert.isTrue(div.hasAttribute('data-htmx-powered')) |
| 15 |
}) |
| 16 |
|
| 17 |
it('initializes descendant elements', function () { |
| 18 |
let container = createProcessedHTML('<div><button hx-get="/test"></button></div>') |
| 19 |
htmx.process(container) |
| 20 |
let button = container.querySelector('button') |
| 21 |
assert.isTrue(button.hasAttribute('data-htmx-powered')) |
| 22 |
}) |
| 23 |
|
| 24 |
it('initializes boosted elements', function () { |
| 25 |
let a = createProcessedHTML('<a href="/test" hx-boost="true">Link</a>') |
| 26 |
htmx.process(a) |
| 27 |
assert.isTrue(a.hasAttribute('data-htmx-powered')) |
| 28 |
}) |
| 29 |
|
| 30 |
it('processes hx-on attributes', function () { |
| 31 |
let div = createProcessedHTML('<div hx-on:custom="this.setAttribute(\'fired\', \'true\')"></div>') |
| 32 |
htmx.process(div) |
| 33 |
div.dispatchEvent(new Event('custom')) |
| 34 |
assert.equal(div.getAttribute('fired'), 'true') |
| 35 |
}) |
| 36 |
|
| 37 |
it('ignores elements with hx-ignore', function () { |
| 38 |
let container = createProcessedHTML('<div hx-ignore><button hx-get="/test"></button></div>') |
| 39 |
htmx.process(container) |
| 40 |
let button = container.querySelector('button') |
| 41 |
assert.isFalse(button.hasAttribute('data-htmx-powered')) |
| 42 |
}) |
| 43 |
|
| 44 |
it('ignores descendants of hx-ignore', function () { |
| 45 |
let container = createProcessedHTML('<div><div hx-ignore><button hx-get="/test"></button></div></div>') |
| 46 |
htmx.process(container) |
| 47 |
let button = container.querySelector('button') |
| 48 |
assert.isFalse(button.hasAttribute('data-htmx-powered')) |
| 49 |
}) |
| 50 |
|
| 51 |
it('processes element itself if it matches', function () { |
| 52 |
let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 53 |
htmx.process(div) |
| 54 |
assert.isTrue(div.hasAttribute('data-htmx-powered')) |
| 55 |
}) |
| 56 |
|
| 57 |
it('triggers htmx:before:process event', function () { |
| 58 |
let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 59 |
let fired = false |
| 60 |
div.addEventListener('htmx:before:process', () => fired = true) |
| 61 |
htmx.process(div) |
| 62 |
assert.isTrue(fired) |
| 63 |
}) |
| 64 |
|
| 65 |
it('triggers htmx:after:process event', function () { |
| 66 |
let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 67 |
let fired = false |
| 68 |
div.addEventListener('htmx:after:process', () => fired = true) |
| 69 |
htmx.process(div) |
| 70 |
assert.isTrue(fired) |
| 71 |
}) |
| 72 |
|
| 73 |
it('ignores hx-on: attributes under hx-ignore', function () { |
| 74 |
let container = createProcessedHTML('<div><div hx-ignore><button hx-on:click="this.setAttribute(\'fired\', \'true\')">Click</button></div></div>') |
| 75 |
htmx.process(container) |
| 76 |
let button = container.querySelector('button') |
| 77 |
button.click() |
| 78 |
assert.isNull(button.getAttribute('fired')) |
| 79 |
}) |
| 80 |
|
| 81 |
it('hx-on:htmx:after:init fires during process', function () { |
| 82 |
let div = createProcessedHTML('<div hx-get="/test" hx-on:htmx:after:init="this.setAttribute(\'inited\', \'true\')"></div>') |
| 83 |
htmx.process(div) |
| 84 |
assert.equal(div.getAttribute('inited'), 'true') |
| 85 |
}) |
| 86 |
|
| 87 |
it('processes elements inside a ShadowRoot', function () { |
| 88 |
let host = createProcessedHTML('<div></div>') |
| 89 |
let shadow = host.attachShadow({mode: 'open'}) |
| 90 |
shadow.innerHTML = '<button hx-get="/test"></button><div id="target"></div>' |
| 91 |
htmx.process(shadow) |
| 92 |
let button = shadow.querySelector('button') |
| 93 |
assert.isTrue(button.hasAttribute('data-htmx-powered')) |
| 94 |
}) |
| 95 |
|
| 96 |
it('skips processing if htmx:before:process is cancelled', function () { |
| 97 |
let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 98 |
div.removeAttribute('data-htmx-powered') |
| 99 |
delete(div._htmx) |
| 100 |
div.addEventListener('htmx:before:process', (e) => e.preventDefault()) |
| 101 |
htmx.process(div) |
| 102 |
assert.isFalse(div.hasAttribute('data-htmx-powered')) |
| 103 |
}) |
| 104 |
|
| 105 |
it('hx-trigger="load" does not re-fire on re-process', function () { |
| 106 |
mockResponse('GET', '/loadtest', '<span/>') |
| 107 |
let div = createProcessedHTML('<div hx-get="/loadtest" hx-trigger="load">x</div>') |
| 108 |
let count = 0 |
| 109 |
div.addEventListener('htmx:before:request', () => count++) |
| 110 |
div.setAttribute('hx-trigger', 'load delay:0') |
| 111 |
htmx.process(div) |
| 112 |
assert.equal(count, 0, 'load should not re-fire on re-process') |
| 113 |
}) |
| 114 |
|
| 115 |
it('process(elt, true) picks up a changed hx-trigger value', async function () { |
| 116 |
mockResponse('GET', '/test', '<span/>') |
| 117 |
let btn = createProcessedHTML('<button hx-get="/test" hx-trigger="click">x</button>') |
| 118 |
let fired = 0 |
| 119 |
btn.addEventListener('htmx:before:request', () => fired++) |
| 120 |
|
| 121 |
btn.click() |
| 122 |
await waitForEvent('htmx:after:request', 100) |
| 123 |
assert.equal(fired, 1, 'baseline click fires') |
| 124 |
|
| 125 |
btn.setAttribute('hx-trigger', 'keyup') |
| 126 |
htmx.process(btn, true) |
| 127 |
|
| 128 |
btn.click() |
| 129 |
await new Promise(r => setTimeout(r, 20)) |
| 130 |
assert.equal(fired, 1, 'click no longer fires after trigger change') |
| 131 |
|
| 132 |
btn.dispatchEvent(new KeyboardEvent('keyup')) |
| 133 |
await waitForEvent('htmx:after:request', 100) |
| 134 |
assert.equal(fired, 2, 'keyup fires after force reprocess') |
| 135 |
}) |
| 136 |
|
| 137 |
it('process(parent, true) reprocesses descendants with mutated hx-on', function () { |
| 138 |
window._n = 0 |
| 139 |
let div = createProcessedHTML('<div><button id="b" hx-on:click="window._n++">b</button></div>') |
| 140 |
let btn = div.querySelector('#b') |
| 141 |
btn.click(); assert.equal(window._n, 1) |
| 142 |
btn.setAttribute('hx-on:click', 'window._n += 10') |
| 143 |
htmx.process(div, true) |
| 144 |
btn.click() |
| 145 |
assert.equal(window._n, 11, 'descendant rebound from current attribute') |
| 146 |
delete window._n |
| 147 |
}) |
| 148 |
|
| 149 |
it('process(elt, true) unwires a removed hx-on attribute', function () { |
| 150 |
window._n = 0 |
| 151 |
let btn = createProcessedHTML('<button hx-on:click="window._n++">b</button>') |
| 152 |
btn.click(); assert.equal(window._n, 1) |
| 153 |
btn.removeAttribute('hx-on:click') |
| 154 |
htmx.process(btn, true) |
| 155 |
btn.click() |
| 156 |
assert.equal(window._n, 1, 'removed handler is gone') |
| 157 |
delete window._n |
| 158 |
}) |
| 159 |
|
| 160 |
it('process(elt, true) mid-request keeps the indicator wired to the running request', async function () { |
| 161 |
mockResponse('GET', '/slow', '<span/>') |
| 162 |
let btn = createProcessedHTML('<button hx-get="/slow">x</button>') |
| 163 |
btn.click() |
| 164 |
assert.isTrue(btn.classList.contains('htmx-request'), 'indicator on during request') |
| 165 |
htmx.process(btn, true) |
| 166 |
await forRequest() |
| 167 |
assert.isFalse(btn.classList.contains('htmx-request'), 'indicator cleared when request completes') |
| 168 |
}) |
| 169 |
|
| 170 |
it('cleanup fires before:cleanup and after:cleanup exactly once per element', async function () { |
| 171 |
let div = createProcessedHTML('<div id="target"><div id="parent" hx-get="/a"><button id="child" hx-get="/b">x</button></div></div>') |
| 172 |
let events = [] |
| 173 |
div.addEventListener('htmx:before:cleanup', (e) => events.push('before:' + e.target.id)) |
| 174 |
div.addEventListener('htmx:after:cleanup', (e) => events.push('after:' + e.target.id)) |
| 175 |
await htmx.swap({target: '#target', text: '<p>replaced</p>', swap: 'innerHTML', sourceElement: div}) |
| 176 |
assert.deepEqual(events, ['before:parent', 'after:parent', 'before:child', 'after:child']) |
| 177 |
}) |
| 178 |
|
| 179 |
it('cleanup fires exactly once per element in nested tree (no double-fire)', async function () { |
| 180 |
let html = '<div id="target"><div id="gp" hx-get="/a"><div id="p" hx-get="/b"><button id="c" hx-get="/c">x</button></div></div></div>' |
| 181 |
let div = createProcessedHTML(html) |
| 182 |
let counts = {} |
| 183 |
div.addEventListener('htmx:before:cleanup', (e) => { |
| 184 |
counts[e.target.id] = (counts[e.target.id] || 0) + 1 |
| 185 |
}) |
| 186 |
await htmx.swap({target: '#target', text: '<p>replaced</p>', swap: 'innerHTML', sourceElement: div}) |
| 187 |
assert.equal(counts['gp'], 1, 'grandparent cleaned once') |
| 188 |
assert.equal(counts['p'], 1, 'parent cleaned once') |
| 189 |
assert.equal(counts['c'], 1, 'child cleaned once') |
| 190 |
}) |
| 191 |
|
| 192 |
}); |