| 1 |
describe('hx-swap modifiers', function() { |
| 2 |
|
| 3 |
beforeEach(() => { |
| 4 |
setupTest() |
| 5 |
}) |
| 6 |
|
| 7 |
afterEach(() => { |
| 8 |
cleanupTest() |
| 9 |
}) |
| 10 |
|
| 11 |
it('properly parses various swap specifications', function() { |
| 12 |
assert.equal(htmx.__parseSwapSpec('innerHTML').style, 'innerHTML') |
| 13 |
assert.equal(htmx.__parseSwapSpec('innerHTML').swap, undefined) |
| 14 |
assert.equal(htmx.__parseSwapSpec('innerHTML swap:10').swap, '10') |
| 15 |
assert.equal(htmx.__parseSwapSpec('innerHTML swap:0').swap, '0') |
| 16 |
assert.equal(htmx.__parseSwapSpec('innerHTML swap:0ms').swap, '0ms') |
| 17 |
|
| 18 |
assert.equal(htmx.__parseSwapSpec('swap:10').style, 'innerHTML') |
| 19 |
assert.equal(htmx.__parseSwapSpec('swap:10').swap, '10') |
| 20 |
assert.equal(htmx.__parseSwapSpec('swap:0').swap, '0') |
| 21 |
assert.equal(htmx.__parseSwapSpec('swap:0s').swap, '0s') |
| 22 |
|
| 23 |
assert.equal(htmx.__parseSwapSpec('transition:true').transition, true) |
| 24 |
assert.equal(htmx.__parseSwapSpec('strip:true').strip, true) |
| 25 |
assert.equal(htmx.__parseSwapSpec('target:"#table tbody"').target, '#table tbody') |
| 26 |
assert.equal(htmx.__parseSwapSpec('target:"#table tbody" swap:10s').target, '#table tbody') |
| 27 |
assert.equal(htmx.__parseSwapSpec('customstyle swap:10').style, 'customstyle') |
| 28 |
}) |
| 29 |
|
| 30 |
it('swap with scroll:top modifier scrolls to top', async function () { |
| 31 |
mockResponse('GET', '/test', '<div style="height:2000px">Tall content</div>') |
| 32 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="innerHTML scroll:top" style="height:100px;overflow:auto"><div style="height:2000px">Old</div></div>'); |
| 33 |
div.scrollTop = 500; |
| 34 |
div.click() |
| 35 |
await forRequest() |
| 36 |
assert.equal(div.scrollTop, 0) |
| 37 |
}) |
| 38 |
|
| 39 |
it('swap with scroll:bottom modifier scrolls to bottom', async function () { |
| 40 |
mockResponse('GET', '/test', '<div style="height:2000px">Tall content</div>') |
| 41 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="innerHTML scroll:bottom" style="height:100px;overflow:auto"><div style="height:2000px">Old</div></div>'); |
| 42 |
div.click() |
| 43 |
await forRequest() |
| 44 |
assert.isAbove(div.scrollTop, 0) |
| 45 |
}) |
| 46 |
|
| 47 |
it('processes scripts in swapped content', async function () { |
| 48 |
mockResponse('GET', '/test', '<div><script>window.testScriptRan = true;</script></div>') |
| 49 |
let div = createProcessedHTML('<div hx-get="/test">Old</div>'); |
| 50 |
window.testScriptRan = false; |
| 51 |
div.click() |
| 52 |
await forRequest() |
| 53 |
assert.isTrue(window.testScriptRan) |
| 54 |
delete window.testScriptRan; |
| 55 |
}) |
| 56 |
|
| 57 |
it('swap with delay (blocking - default behavior) waits for delay before completing request', async function () { |
| 58 |
mockResponse('GET', '/test', '<div>New Content</div>') |
| 59 |
createProcessedHTML('<div id="test-div" hx-get="/test" hx-swap="innerHTML swap:100ms transistion:false">Old Content</div>'); |
| 60 |
|
| 61 |
find('#test-div').click() |
| 62 |
await forRequest() |
| 63 |
assertTextContentIs('#test-div', 'New Content') |
| 64 |
}) |
| 65 |
|
| 66 |
it('main swap with delay respects blocking behavior', async function () { |
| 67 |
mockResponse('GET', '/test', 'Main Content') |
| 68 |
createProcessedHTML('<div id="main" hx-get="/test" hx-swap="innerHTML swap:100ms">Original</div>'); |
| 69 |
|
| 70 |
let startTime = Date.now(); |
| 71 |
find('#main').click() |
| 72 |
await forRequest() |
| 73 |
let elapsed = Date.now() - startTime; |
| 74 |
|
| 75 |
|
| 76 |
assert.isAtLeast(elapsed, 100, 'Should wait at least 100ms') |
| 77 |
assertTextContentIs('#main', 'Main Content') |
| 78 |
}) |
| 79 |
|
| 80 |
it('textContent swap replaces text only', async function () { |
| 81 |
mockResponse('GET', '/test', '<div><b>Bold</b> Text</div>') |
| 82 |
createProcessedHTML('<div id="target" hx-get="/test" hx-swap="textContent"><span>Old</span></div>'); |
| 83 |
find('#target').click() |
| 84 |
await forRequest() |
| 85 |
assert.equal(find('#target').textContent, 'Bold Text') |
| 86 |
assert.equal(find('#target').innerHTML, 'Bold Text') |
| 87 |
}) |
| 88 |
|
| 89 |
it('textContent swap preserves target element', async function () { |
| 90 |
mockResponse('GET', '/test', '<p>New Text</p>') |
| 91 |
createProcessedHTML('<div id="target" class="test" hx-get="/test" hx-swap="textContent">Old</div>'); |
| 92 |
find('#target').click() |
| 93 |
await forRequest() |
| 94 |
assert.equal(find('#target').tagName, 'DIV') |
| 95 |
assert.equal(find('#target').className, 'test') |
| 96 |
assert.equal(find('#target').textContent, 'New Text') |
| 97 |
}) |
| 98 |
}) |