| 1 |
describe('__issueRequest unit tests', function() { |
| 2 |
|
| 3 |
beforeEach(function() { |
| 4 |
setupTest(); |
| 5 |
}); |
| 6 |
|
| 7 |
afterEach(function() { |
| 8 |
cleanupTest(); |
| 9 |
}); |
| 10 |
|
| 11 |
it('triggers htmx:before:request event', async function () { |
| 12 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none"></div>') |
| 13 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 14 |
|
| 15 |
let mockFetch = async () => ({ |
| 16 |
status: 200, |
| 17 |
headers: new Headers(), |
| 18 |
text: async () => '' |
| 19 |
}) |
| 20 |
ctx.fetch = mockFetch |
| 21 |
|
| 22 |
let beforeRequestFired = false |
| 23 |
div.addEventListener('htmx:before:request', () => beforeRequestFired = true) |
| 24 |
|
| 25 |
await htmx.__issueRequest(ctx) |
| 26 |
assert.isTrue(beforeRequestFired) |
| 27 |
}) |
| 28 |
|
| 29 |
it('triggers htmx:after:request event', async function () { |
| 30 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none"></div>') |
| 31 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 32 |
|
| 33 |
let mockFetch = async () => ({ |
| 34 |
status: 200, |
| 35 |
headers: new Headers(), |
| 36 |
text: async () => '' |
| 37 |
}) |
| 38 |
ctx.fetch = mockFetch |
| 39 |
|
| 40 |
let afterRequestFired = false |
| 41 |
div.addEventListener('htmx:after:request', () => afterRequestFired = true) |
| 42 |
|
| 43 |
await htmx.__issueRequest(ctx) |
| 44 |
assert.isTrue(afterRequestFired) |
| 45 |
}) |
| 46 |
|
| 47 |
it('calls custom fetch implementation', async function () { |
| 48 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none"></div>') |
| 49 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 50 |
|
| 51 |
let fetchCalled = false |
| 52 |
let fetchAction = null |
| 53 |
ctx.fetch = async (action, request) => { |
| 54 |
fetchCalled = true |
| 55 |
fetchAction = action |
| 56 |
return { |
| 57 |
status: 200, |
| 58 |
headers: new Headers(), |
| 59 |
text: async () => '' |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
await htmx.__issueRequest(ctx) |
| 64 |
assert.isTrue(fetchCalled) |
| 65 |
assert.equal(fetchAction, '/test') |
| 66 |
}) |
| 67 |
|
| 68 |
it('does not execute when queue blocks request', async function () { |
| 69 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none" hx-sync="drop"></div>') |
| 70 |
|
| 71 |
|
| 72 |
let ctx1 = htmx.__createRequestContext(div, new Event('click')) |
| 73 |
ctx1.fetch = async () => new Promise(() => {}) |
| 74 |
htmx.__issueRequest(ctx1) |
| 75 |
|
| 76 |
|
| 77 |
let ctx2 = htmx.__createRequestContext(div, new Event('click')) |
| 78 |
let fetchCalled = false |
| 79 |
ctx2.fetch = async () => { |
| 80 |
fetchCalled = true |
| 81 |
return { status: 200, headers: new Headers(), text: async () => '' } |
| 82 |
} |
| 83 |
|
| 84 |
await htmx.__issueRequest(ctx2) |
| 85 |
assert.isFalse(fetchCalled) |
| 86 |
}) |
| 87 |
|
| 88 |
it('returns early if htmx:before:request is cancelled', async function () { |
| 89 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none"></div>') |
| 90 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 91 |
|
| 92 |
div.addEventListener('htmx:before:request', (e) => e.preventDefault()) |
| 93 |
|
| 94 |
let fetchCalled = false |
| 95 |
ctx.fetch = async () => { |
| 96 |
fetchCalled = true |
| 97 |
return { status: 200, headers: new Headers(), text: async () => '' } |
| 98 |
} |
| 99 |
|
| 100 |
await htmx.__issueRequest(ctx) |
| 101 |
assert.isFalse(fetchCalled) |
| 102 |
}) |
| 103 |
|
| 104 |
it('returns early if confirm returns false', async function () { |
| 105 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none" hx-confirm="Are you sure?"></div>') |
| 106 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 107 |
|
| 108 |
let originalConfirm = window.confirm |
| 109 |
window.confirm = () => false |
| 110 |
|
| 111 |
let fetchCalled = false |
| 112 |
ctx.fetch = async () => { |
| 113 |
fetchCalled = true |
| 114 |
return { status: 200, headers: new Headers(), text: async () => '' } |
| 115 |
} |
| 116 |
|
| 117 |
await htmx.__issueRequest(ctx) |
| 118 |
assert.isFalse(fetchCalled) |
| 119 |
|
| 120 |
window.confirm = originalConfirm |
| 121 |
}) |
| 122 |
|
| 123 |
it('cancels request when dropRequest called after htmx:confirm preventDefault', async function () { |
| 124 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none" hx-confirm="Are you sure?"></div>') |
| 125 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 126 |
|
| 127 |
div.addEventListener('htmx:confirm', (e) => { |
| 128 |
e.preventDefault() |
| 129 |
e.detail.dropRequest() |
| 130 |
}) |
| 131 |
|
| 132 |
let fetchCalled = false |
| 133 |
ctx.fetch = async () => { |
| 134 |
fetchCalled = true |
| 135 |
return { status: 200, headers: new Headers(), text: async () => '' } |
| 136 |
} |
| 137 |
|
| 138 |
await htmx.__issueRequest(ctx) |
| 139 |
assert.isFalse(fetchCalled) |
| 140 |
}) |
| 141 |
|
| 142 |
it('creates response object with correct structure', async function () { |
| 143 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none"></div>') |
| 144 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 145 |
|
| 146 |
let mockHeaders = new Headers() |
| 147 |
ctx.fetch = async () => ({ |
| 148 |
status: 201, |
| 149 |
headers: mockHeaders, |
| 150 |
text: async () => 'response text' |
| 151 |
}) |
| 152 |
|
| 153 |
await htmx.__issueRequest(ctx) |
| 154 |
|
| 155 |
assert.equal(ctx.response.status, 201) |
| 156 |
assert.equal(ctx.response.headers, mockHeaders) |
| 157 |
assert.isDefined(ctx.response.raw) |
| 158 |
}) |
| 159 |
|
| 160 |
it('catches errors and triggers htmx:error event', async function () { |
| 161 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none"></div>') |
| 162 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 163 |
|
| 164 |
let errorFired = false |
| 165 |
let capturedError = null |
| 166 |
div.addEventListener('htmx:error', (e) => { |
| 167 |
errorFired = true |
| 168 |
capturedError = e.detail.error |
| 169 |
}) |
| 170 |
|
| 171 |
let testError = new Error('fetch failed') |
| 172 |
ctx.fetch = async () => { throw testError } |
| 173 |
|
| 174 |
await htmx.__issueRequest(ctx) |
| 175 |
|
| 176 |
assert.isTrue(errorFired) |
| 177 |
assert.equal(capturedError, testError) |
| 178 |
}) |
| 179 |
|
| 180 |
it('always triggers htmx:finally:request', async function () { |
| 181 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none"></div>') |
| 182 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 183 |
|
| 184 |
let finallyFired = false |
| 185 |
div.addEventListener('htmx:finally:request', () => finallyFired = true) |
| 186 |
|
| 187 |
ctx.fetch = async () => { throw new Error('fail') } |
| 188 |
|
| 189 |
await htmx.__issueRequest(ctx) |
| 190 |
assert.isTrue(finallyFired) |
| 191 |
}) |
| 192 |
|
| 193 |
it('updates ctx.status through request lifecycle', async function () { |
| 194 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none"></div>') |
| 195 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 196 |
|
| 197 |
let statuses = [] |
| 198 |
div.addEventListener('htmx:before:request', () => statuses.push(ctx.status)) |
| 199 |
|
| 200 |
ctx.fetch = async () => { |
| 201 |
statuses.push(ctx.status) |
| 202 |
return { status: 200, headers: new Headers(), text: async () => '' } |
| 203 |
} |
| 204 |
|
| 205 |
await htmx.__issueRequest(ctx) |
| 206 |
statuses.push(ctx.status) |
| 207 |
|
| 208 |
assert.include(statuses, 'issuing') |
| 209 |
assert.include(statuses, 'swapped') |
| 210 |
}) |
| 211 |
|
| 212 |
it('processes next queued request after completion', async function () { |
| 213 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none" hx-sync="queue all"></div>') |
| 214 |
|
| 215 |
let request1Complete = false |
| 216 |
let request2Started = false |
| 217 |
|
| 218 |
|
| 219 |
let ctx1 = htmx.__createRequestContext(div, new Event('click')) |
| 220 |
ctx1.fetch = async () => { |
| 221 |
await new Promise(r => setTimeout(r, 10)) |
| 222 |
request1Complete = true |
| 223 |
return { status: 200, headers: new Headers(), text: async () => '' } |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
let ctx2 = htmx.__createRequestContext(div, new Event('click')) |
| 228 |
ctx2.fetch = async () => { |
| 229 |
request2Started = true |
| 230 |
return { status: 200, headers: new Headers(), text: async () => '' } |
| 231 |
} |
| 232 |
|
| 233 |
let p1 = htmx.__issueRequest(ctx1) |
| 234 |
await new Promise(r => setTimeout(r, 5)) |
| 235 |
let p2 = htmx.__issueRequest(ctx2) |
| 236 |
|
| 237 |
await Promise.all([p1, p2]) |
| 238 |
|
| 239 |
assert.isTrue(request1Complete) |
| 240 |
assert.isTrue(request2Started) |
| 241 |
}) |
| 242 |
|
| 243 |
it('aborts request after timeout fires', async function () { |
| 244 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none" hx-config="timeout:50"></div>') |
| 245 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 246 |
|
| 247 |
ctx.fetch = (url, opts) => new Promise((_, reject) => { |
| 248 |
opts.signal.addEventListener('abort', () => { |
| 249 |
reject(new DOMException('The operation was aborted', 'AbortError')) |
| 250 |
}) |
| 251 |
}) |
| 252 |
|
| 253 |
let errorFired = false |
| 254 |
div.addEventListener('htmx:error', () => errorFired = true) |
| 255 |
|
| 256 |
await htmx.__issueRequest(ctx) |
| 257 |
assert.isTrue(errorFired) |
| 258 |
assert.isTrue(ctx.request.signal.aborted) |
| 259 |
}) |
| 260 |
|
| 261 |
it('htmx:abort event aborts in-flight request', async function () { |
| 262 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="none"></div>') |
| 263 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 264 |
|
| 265 |
ctx.fetch = (url, opts) => new Promise((_, reject) => { |
| 266 |
setTimeout(() => htmx.trigger(div, 'htmx:abort'), 10) |
| 267 |
opts.signal.addEventListener('abort', () => { |
| 268 |
reject(new DOMException('The operation was aborted', 'AbortError')) |
| 269 |
}) |
| 270 |
}) |
| 271 |
|
| 272 |
let errorFired = false |
| 273 |
div.addEventListener('htmx:error', () => errorFired = true) |
| 274 |
|
| 275 |
await htmx.__issueRequest(ctx) |
| 276 |
assert.isTrue(errorFired) |
| 277 |
assert.isTrue(ctx.request.signal.aborted) |
| 278 |
}) |
| 279 |
|
| 280 |
it('does not crash when scroll target selector matches nothing', async function () { |
| 281 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="innerHTML scroll:top scrollTarget:#nonexistent"></div>') |
| 282 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 283 |
|
| 284 |
ctx.fetch = async () => ({ |
| 285 |
status: 200, |
| 286 |
headers: new Headers(), |
| 287 |
text: async () => '<div>Response</div>' |
| 288 |
}) |
| 289 |
|
| 290 |
let errorFired = false |
| 291 |
div.addEventListener('htmx:error', () => errorFired = true) |
| 292 |
|
| 293 |
await htmx.__issueRequest(ctx) |
| 294 |
assert.isFalse(errorFired) |
| 295 |
}) |
| 296 |
|
| 297 |
it('does not crash when show target selector matches nothing', async function () { |
| 298 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="innerHTML show:top showTarget:#nonexistent"></div>') |
| 299 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 300 |
|
| 301 |
ctx.fetch = async () => ({ |
| 302 |
status: 200, |
| 303 |
headers: new Headers(), |
| 304 |
text: async () => '<div>Response</div>' |
| 305 |
}) |
| 306 |
|
| 307 |
let errorFired = false |
| 308 |
div.addEventListener('htmx:error', () => errorFired = true) |
| 309 |
|
| 310 |
await htmx.__issueRequest(ctx) |
| 311 |
assert.isFalse(errorFired) |
| 312 |
}) |
| 313 |
|
| 314 |
it('throws clean error for unknown swap style with no extensions', async function () { |
| 315 |
let div = createProcessedHTML('<div hx-get="/test" hx-swap="foobar"></div>') |
| 316 |
let ctx = htmx.__createRequestContext(div, new Event('click')) |
| 317 |
|
| 318 |
ctx.fetch = async () => ({ |
| 319 |
status: 200, |
| 320 |
headers: new Headers(), |
| 321 |
text: async () => '<div>Response</div>' |
| 322 |
}) |
| 323 |
|
| 324 |
let capturedError = null |
| 325 |
div.addEventListener('htmx:error', (e) => capturedError = e.detail.error) |
| 326 |
|
| 327 |
await htmx.__issueRequest(ctx) |
| 328 |
assert.isNotNull(capturedError) |
| 329 |
assert.include(capturedError.message, 'Unknown swap style') |
| 330 |
}) |
| 331 |
|
| 332 |
}); |