| 1 | describe('hx-preload attribute', function() { |
| 2 | |
| 3 | let extBackup; |
| 4 | |
| 5 | before(async () => { |
| 6 | extBackup = backupExtensions(); |
| 7 | clearExtensions(); |
| 8 | let script = document.createElement('script'); |
| 9 | script.src = '../src/ext/hx-preload.js'; |
| 10 | await new Promise(resolve => { |
| 11 | script.onload = resolve; |
| 12 | document.head.appendChild(script); |
| 13 | }); |
| 14 | }) |
| 15 | |
| 16 | after(() => { |
| 17 | restoreExtensions(extBackup); |
| 18 | }) |
| 19 | |
| 20 | beforeEach(() => { |
| 21 | setupTest(this.currentTest) |
| 22 | }) |
| 23 | |
| 24 | afterEach(() => { |
| 25 | cleanupTest(this.currentTest) |
| 26 | }) |
| 27 | |
| 28 | it('preloads on mousedown with bare attribute on hx-get element', async function () { |
| 29 | mockResponse('GET', '/test', 'Preloaded') |
| 30 | let btn = createProcessedHTML('<button hx-get="/test" hx-preload>Click</button>'); |
| 31 | btn.dispatchEvent(new Event('mousedown')) |
| 32 | await htmx.timeout(20) |
| 33 | assert.isDefined(btn._htmx.preload) |
| 34 | }) |
| 35 | |
| 36 | it('preloads on mousedown with bare attribute on boosted anchor', async function () { |
| 37 | mockResponse('GET', '/test', 'Preloaded') |
| 38 | let div = createProcessedHTML('<div hx-boost:inherited="true"><a id="a1" href="/test" hx-preload>Link</a></div>'); |
| 39 | let a = div.querySelector('#a1') |
| 40 | a.dispatchEvent(new Event('mousedown')) |
| 41 | await htmx.timeout(20) |
| 42 | assert.isDefined(a._htmx.preload) |
| 43 | }) |
| 44 | |
| 45 | it('does not preload with bare attribute on non-hx-get element', async function () { |
| 46 | mockResponse('POST', '/test', 'Posted') |
| 47 | let btn = createProcessedHTML('<button hx-post="/test" hx-preload>Click</button>'); |
| 48 | btn.dispatchEvent(new Event('mousedown')) |
| 49 | await htmx.timeout(20) |
| 50 | assert.isUndefined(btn._htmx?.preload) |
| 51 | }) |
| 52 | |
| 53 | it('preloads on specified event', async function () { |
| 54 | mockResponse('GET', '/test', 'Preloaded') |
| 55 | let btn = createProcessedHTML('<button hx-get="/test" hx-preload="mouseenter">Click</button>'); |
| 56 | btn.dispatchEvent(new Event('mouseenter')) |
| 57 | await htmx.timeout(20) |
| 58 | assert.isDefined(btn._htmx.preload) |
| 59 | }) |
| 60 | |
| 61 | it('does not preload POST requests', async function () { |
| 62 | mockResponse('POST', '/test', 'Posted') |
| 63 | let btn = createProcessedHTML('<button hx-post="/test" hx-preload="mouseenter">Click</button>'); |
| 64 | btn.dispatchEvent(new Event('mouseenter')) |
| 65 | await htmx.timeout(20) |
| 66 | assert.isUndefined(btn._htmx.preload) |
| 67 | }) |
| 68 | |
| 69 | it('uses default 5s timeout', async function () { |
| 70 | mockResponse('GET', '/test', 'Response') |
| 71 | let btn = createProcessedHTML('<button hx-get="/test" hx-preload="mouseenter">Click</button>'); |
| 72 | btn.dispatchEvent(new Event('mouseenter')) |
| 73 | await htmx.timeout(20) |
| 74 | assert.isTrue(btn._htmx.preload.expiresAt > Date.now() + 4000) |
| 75 | }) |
| 76 | |
| 77 | it('respects custom timeout', async function () { |
| 78 | mockResponse('GET', '/test', 'Response') |
| 79 | let btn = createProcessedHTML('<button hx-get="/test" hx-preload="mouseenter timeout:100ms">Click</button>'); |
| 80 | btn.dispatchEvent(new Event('mouseenter')) |
| 81 | await htmx.timeout(20) |
| 82 | assert.isTrue(btn._htmx.preload.expiresAt < Date.now() + 200) |
| 83 | }) |
| 84 | |
| 85 | it('skips duplicate preload events', async function () { |
| 86 | mockResponse('GET', '/test', 'Response') |
| 87 | let btn = createProcessedHTML('<button hx-get="/test" hx-preload="mouseenter">Click</button>'); |
| 88 | btn.dispatchEvent(new Event('mouseenter')) |
| 89 | await htmx.timeout(10) |
| 90 | let firstPreload = btn._htmx.preload |
| 91 | btn.dispatchEvent(new Event('mouseenter')) |
| 92 | assert.equal(btn._htmx.preload, firstPreload) |
| 93 | }) |
| 94 | |
| 95 | it('works with different event types', async function () { |
| 96 | mockResponse('GET', '/test', 'Response') |
| 97 | let btn = createProcessedHTML('<button hx-get="/test" hx-preload="focus">Click</button>'); |
| 98 | btn.dispatchEvent(new Event('focus')) |
| 99 | await htmx.timeout(20) |
| 100 | assert.isDefined(btn._htmx.preload) |
| 101 | }) |
| 102 | |
| 103 | it('works with multiple event types', async function () { |
| 104 | mockResponse('GET', '/test', 'Response') |
| 105 | let btn = createProcessedHTML('<button hx-get="/test" hx-preload="focus, foo">Click</button>'); |
| 106 | btn.dispatchEvent(new Event('foo')) |
| 107 | await htmx.timeout(20) |
| 108 | assert.isDefined(btn._htmx.preload) |
| 109 | }) |
| 110 | |
| 111 | it('auto-preloads boosted anchors by default', async function () { |
| 112 | mockResponse('GET', '/test', 'Preloaded') |
| 113 | let div = createProcessedHTML('<div hx-boost:inherited="true"><a id="a1" href="/test">Link</a></div>'); |
| 114 | let a = div.querySelector('#a1') |
| 115 | a.dispatchEvent(new Event('mousedown')) |
| 116 | await htmx.timeout(20) |
| 117 | assert.isDefined(a._htmx.preload) |
| 118 | }) |
| 119 | |
| 120 | it('does not auto-preload boosted anchors when autoBoost is false', async function () { |
| 121 | htmx.config.preload = { autoBoost: false } |
| 122 | try { |
| 123 | mockResponse('GET', '/test', 'Preloaded') |
| 124 | let div = createProcessedHTML('<div hx-boost:inherited="true"><a id="a1" href="/test">Link</a></div>'); |
| 125 | let a = div.querySelector('#a1') |
| 126 | a.dispatchEvent(new Event('mousedown')) |
| 127 | await htmx.timeout(20) |
| 128 | assert.isUndefined(a._htmx?.preload) |
| 129 | } finally { |
| 130 | delete htmx.config.preload |
| 131 | } |
| 132 | }) |
| 133 | |
| 134 | it('builds URL with form params', async function () { |
| 135 | mockResponse('GET', '/test?name=test', 'Response') |
| 136 | let form = createProcessedHTML('<form><input name="name" value="test"><button hx-get="/test" hx-preload="mouseenter">Click</button></form>'); |
| 137 | let btn = form.querySelector('button') |
| 138 | btn.dispatchEvent(new Event('mouseenter')) |
| 139 | await htmx.timeout(20) |
| 140 | assert.equal(btn._htmx.preload.action, '/test?name=test') |
| 141 | }) |
| 142 | }) |