| 1 |
describe('ajax() unit Tests', function() { |
| 2 |
|
| 3 |
beforeEach(function() { |
| 4 |
setupTest(); |
| 5 |
}); |
| 6 |
|
| 7 |
afterEach(function() { |
| 8 |
cleanupTest(); |
| 9 |
}); |
| 10 |
|
| 11 |
it('ajax works with element target', async function() { |
| 12 |
mockResponse('GET', '/test', 'foo!'); |
| 13 |
const div = createProcessedHTML('<div></div>'); |
| 14 |
await htmx.ajax('GET', '/test', {target: div, swap: 'innerHTML'}); |
| 15 |
assert.equal(div.innerHTML, 'foo!'); |
| 16 |
}); |
| 17 |
|
| 18 |
it('ajax works with selector target', async function() { |
| 19 |
mockResponse('GET', '/test', 'foo!'); |
| 20 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 21 |
await htmx.ajax('GET', '/test', {target: '#d1', swap: 'innerHTML'}); |
| 22 |
assert.equal(div.innerHTML, 'foo!'); |
| 23 |
}); |
| 24 |
|
| 25 |
it('ajax rejects when target selector invalid', async function() { |
| 26 |
mockResponse('GET', '/test', 'foo!'); |
| 27 |
createProcessedHTML('<div id="d1"></div>'); |
| 28 |
try { |
| 29 |
await htmx.ajax('GET', '/test', '#d2'); |
| 30 |
assert.fail('Should have rejected'); |
| 31 |
} catch (e) { |
| 32 |
assert.include(e.message, 'Target not found'); |
| 33 |
} |
| 34 |
}); |
| 35 |
|
| 36 |
it('ajax rejects when target invalid even if source set', async function() { |
| 37 |
mockResponse('GET', '/test', 'foo!'); |
| 38 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 39 |
try { |
| 40 |
await htmx.ajax('GET', '/test', { |
| 41 |
source: div, |
| 42 |
target: '#d2' |
| 43 |
}); |
| 44 |
assert.fail('Should have rejected'); |
| 45 |
} catch (e) { |
| 46 |
assert.include(e.message, 'Target not found'); |
| 47 |
} |
| 48 |
}); |
| 49 |
|
| 50 |
it('ajax rejects when source selector invalid', async function() { |
| 51 |
mockResponse('GET', '/test', 'foo!'); |
| 52 |
createProcessedHTML('<div id="d1"></div>'); |
| 53 |
try { |
| 54 |
await htmx.ajax('GET', '/test', { |
| 55 |
source: '#d2' |
| 56 |
}); |
| 57 |
assert.fail('Should have rejected'); |
| 58 |
} catch (e) { |
| 59 |
assert.include(e.message, 'Source not found'); |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
it('ajax targets source if target not set', async function() { |
| 64 |
mockResponse('GET', '/test', 'foo!'); |
| 65 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 66 |
await htmx.ajax('GET', '/test', { |
| 67 |
source: div, |
| 68 |
swap: 'innerHTML' |
| 69 |
}); |
| 70 |
assert.equal(div.innerHTML, 'foo!'); |
| 71 |
}); |
| 72 |
|
| 73 |
it('ajax works with swap option', async function() { |
| 74 |
mockResponse('GET', '/test', '<p class="test">foo!</p>'); |
| 75 |
const container = createProcessedHTML('<div><div id="target"></div></div>'); |
| 76 |
await htmx.ajax('GET', '/test', { |
| 77 |
target: '#target', |
| 78 |
swap: 'outerHTML' |
| 79 |
}); |
| 80 |
assert.equal(container.innerHTML, '<p class="test">foo!</p>'); |
| 81 |
}); |
| 82 |
|
| 83 |
it('ajax works with select option', async function() { |
| 84 |
mockResponse('GET', '/test', '<div id="d1">foo</div><div id="d2">bar</div>'); |
| 85 |
const div = createProcessedHTML('<div id="target"></div>'); |
| 86 |
await htmx.ajax('GET', '/test', { |
| 87 |
target: '#target', |
| 88 |
swap: 'innerHTML', |
| 89 |
select: '#d2' |
| 90 |
}); |
| 91 |
assert.include(div.innerHTML, 'bar'); |
| 92 |
assert.notInclude(div.innerHTML, 'foo'); |
| 93 |
}); |
| 94 |
|
| 95 |
it('ajax returns a promise', async function() { |
| 96 |
mockResponse('GET', '/test', 'foo!'); |
| 97 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 98 |
const promise = htmx.ajax('GET', '/test', {target: '#d1', swap: 'innerHTML'}); |
| 99 |
assert.instanceOf(promise, Promise); |
| 100 |
await promise; |
| 101 |
assert.equal(div.innerHTML, 'foo!'); |
| 102 |
}); |
| 103 |
|
| 104 |
it('ajax can pass values', async function() { |
| 105 |
mockResponse('POST', '/test', 'Clicked!'); |
| 106 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 107 |
await htmx.ajax('POST', '/test', { |
| 108 |
target: '#d1', |
| 109 |
swap: 'innerHTML', |
| 110 |
values: { i1: 'test' } |
| 111 |
}); |
| 112 |
assert.equal(div.innerHTML, 'Clicked!'); |
| 113 |
const lastCall = lastFetch(); |
| 114 |
const params = new URLSearchParams(lastCall.request.body); |
| 115 |
assert.equal(params.get('i1'), 'test'); |
| 116 |
}); |
| 117 |
|
| 118 |
it('ajax can pass custom headers', async function() { |
| 119 |
mockResponse('POST', '/test', 'Clicked!'); |
| 120 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 121 |
await htmx.ajax('POST', '/test', { |
| 122 |
target: '#d1', |
| 123 |
swap: 'innerHTML', |
| 124 |
headers: { |
| 125 |
'X-Custom': 'test-value' |
| 126 |
}, |
| 127 |
values: { i1: 'test' } |
| 128 |
}); |
| 129 |
assert.equal(div.innerHTML, 'Clicked!'); |
| 130 |
const lastCall = lastFetch(); |
| 131 |
assert.equal(lastCall.request.headers['X-Custom'], 'test-value'); |
| 132 |
}); |
| 133 |
|
| 134 |
it('ajax collects form data from source element', async function() { |
| 135 |
mockResponse('POST', '/test', 'Submitted!'); |
| 136 |
createProcessedHTML('<form id="myForm"><input name="field1" value="value1"/></form><div id="result"></div>'); |
| 137 |
const form = find('#myForm'); |
| 138 |
const div = find('#result'); |
| 139 |
await htmx.ajax('POST', '/test', { |
| 140 |
source: form, |
| 141 |
target: '#result', |
| 142 |
swap: 'innerHTML' |
| 143 |
}); |
| 144 |
assert.equal(div.innerHTML, 'Submitted!'); |
| 145 |
const lastCall = lastFetch(); |
| 146 |
const params = new URLSearchParams(lastCall.request.body); |
| 147 |
assert.equal(params.get('field1'), 'value1'); |
| 148 |
}); |
| 149 |
|
| 150 |
it('ajax values override form data', async function() { |
| 151 |
mockResponse('POST', '/test', 'Submitted!'); |
| 152 |
createProcessedHTML('<form id="myForm"><input name="field1" value="original"/></form><div id="result"></div>'); |
| 153 |
const form = find('#myForm'); |
| 154 |
const div = find('#result'); |
| 155 |
await htmx.ajax('POST', '/test', { |
| 156 |
source: form, |
| 157 |
target: '#result', |
| 158 |
swap: 'innerHTML', |
| 159 |
values: { field1: 'override' } |
| 160 |
}); |
| 161 |
assert.equal(div.innerHTML, 'Submitted!'); |
| 162 |
const lastCall = lastFetch(); |
| 163 |
const params = new URLSearchParams(lastCall.request.body); |
| 164 |
assert.equal(params.get('field1'), 'override'); |
| 165 |
}); |
| 166 |
|
| 167 |
it('ajax works with GET and query parameters', async function() { |
| 168 |
mockResponse('GET', /\/test\?.*/, 'Got it!'); |
| 169 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 170 |
await htmx.ajax('GET', '/test', { |
| 171 |
target: '#d1', |
| 172 |
swap: 'innerHTML', |
| 173 |
values: { param1: 'value1', param2: 'value2' } |
| 174 |
}); |
| 175 |
assert.equal(div.innerHTML, 'Got it!'); |
| 176 |
const lastCall = lastFetch(); |
| 177 |
assert.include(lastCall.url, 'param1=value1'); |
| 178 |
assert.include(lastCall.url, 'param2=value2'); |
| 179 |
}); |
| 180 |
|
| 181 |
it('ajax works with optimistic UI', async function() { |
| 182 |
mockResponse('POST', '/test', 'final!'); |
| 183 |
createProcessedHTML('<div id="loading">Loading...</div><div id="target">initial</div>'); |
| 184 |
const div = find('#target'); |
| 185 |
|
| 186 |
|
| 187 |
await htmx.ajax('POST', '/test', { |
| 188 |
target: '#target', |
| 189 |
swap: 'innerHTML', |
| 190 |
optimistic: '#loading' |
| 191 |
}); |
| 192 |
|
| 193 |
assert.equal(div.innerHTML, 'final!'); |
| 194 |
}); |
| 195 |
|
| 196 |
it('ajax respects transition option', async function() { |
| 197 |
mockResponse('GET', '/test', 'content!'); |
| 198 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 199 |
await htmx.ajax('GET', '/test', { |
| 200 |
target: '#d1', |
| 201 |
swap: 'innerHTML', |
| 202 |
transition: false |
| 203 |
}); |
| 204 |
assert.equal(div.innerHTML, 'content!'); |
| 205 |
}); |
| 206 |
|
| 207 |
it('ajax works with no context (defaults to body)', async function() { |
| 208 |
this.skip() |
| 209 |
return; |
| 210 |
mockResponse('GET', '/test', '<div id="ajax-result">body content</div>'); |
| 211 |
await htmx.ajax('GET', '/test', {swap: 'beforeend'}); |
| 212 |
|
| 213 |
const result = document.getElementById('ajax-result'); |
| 214 |
assert.isNotNull(result); |
| 215 |
assert.equal(result.textContent, 'body content'); |
| 216 |
|
| 217 |
result.remove(); |
| 218 |
}); |
| 219 |
|
| 220 |
it('ajax case insensitive verb', async function() { |
| 221 |
mockResponse('POST', '/test', 'posted!'); |
| 222 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 223 |
await htmx.ajax('post', '/test', { |
| 224 |
target: '#d1', |
| 225 |
swap: 'innerHTML', |
| 226 |
values: {test: 'value'} |
| 227 |
}); |
| 228 |
assert.equal(div.innerHTML, 'posted!'); |
| 229 |
}); |
| 230 |
|
| 231 |
it('ajax uppercase verb', async function() { |
| 232 |
mockResponse('DELETE', '/test', 'deleted!'); |
| 233 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 234 |
await htmx.ajax('DELETE', '/test', {target: '#d1', swap: 'innerHTML'}); |
| 235 |
assert.equal(div.innerHTML, 'deleted!'); |
| 236 |
}); |
| 237 |
|
| 238 |
it('ajax with event context', async function() { |
| 239 |
mockResponse('POST', '/test', 'clicked!'); |
| 240 |
createProcessedHTML('<button id="btn">Click</button><div id="result"></div>'); |
| 241 |
const div = find('#result'); |
| 242 |
const clickEvent = new MouseEvent('click', { bubbles: true }); |
| 243 |
|
| 244 |
await htmx.ajax('POST', '/test', { |
| 245 |
target: '#result', |
| 246 |
swap: 'innerHTML', |
| 247 |
event: clickEvent |
| 248 |
}); |
| 249 |
assert.equal(div.innerHTML, 'clicked!'); |
| 250 |
}); |
| 251 |
|
| 252 |
it('ajax triggers htmx events', async function() { |
| 253 |
mockResponse('GET', '/test', 'content!'); |
| 254 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 255 |
|
| 256 |
let beforeRequestFired = false; |
| 257 |
let afterRequestFired = false; |
| 258 |
|
| 259 |
div.addEventListener('htmx:before:request', () => { |
| 260 |
beforeRequestFired = true; |
| 261 |
}); |
| 262 |
|
| 263 |
div.addEventListener('htmx:after:request', () => { |
| 264 |
afterRequestFired = true; |
| 265 |
}); |
| 266 |
|
| 267 |
await htmx.ajax('GET', '/test', {target: div, swap: 'innerHTML'}); |
| 268 |
|
| 269 |
assert.isTrue(beforeRequestFired); |
| 270 |
assert.isTrue(afterRequestFired); |
| 271 |
}); |
| 272 |
|
| 273 |
it('ajax with multiple values', async function() { |
| 274 |
mockResponse('POST', '/test', 'OK'); |
| 275 |
const div = createProcessedHTML('<div id="d1"></div>'); |
| 276 |
await htmx.ajax('POST', '/test', { |
| 277 |
target: '#d1', |
| 278 |
swap: 'innerHTML', |
| 279 |
values: { |
| 280 |
field1: 'value1', |
| 281 |
field2: 'value2', |
| 282 |
field3: 'value3' |
| 283 |
} |
| 284 |
}); |
| 285 |
const lastCall = lastFetch(); |
| 286 |
const params = new URLSearchParams(lastCall.request.body); |
| 287 |
assert.equal(params.get('field1'), 'value1'); |
| 288 |
assert.equal(params.get('field2'), 'value2'); |
| 289 |
assert.equal(params.get('field3'), 'value3'); |
| 290 |
}); |
| 291 |
|
| 292 |
it('ajax with swap innerHTML', async function() { |
| 293 |
mockResponse('GET', '/test', '<span>inner</span>'); |
| 294 |
const div = createProcessedHTML('<div id="target"><p>old</p></div>'); |
| 295 |
await htmx.ajax('GET', '/test', { |
| 296 |
target: '#target', |
| 297 |
swap: 'innerHTML' |
| 298 |
}); |
| 299 |
assert.equal(div.innerHTML, '<span>inner</span>'); |
| 300 |
}); |
| 301 |
|
| 302 |
it('ajax with swap beforeend', async function() { |
| 303 |
mockResponse('GET', '/test', '<span>new</span>'); |
| 304 |
const div = createProcessedHTML('<div id="target"><p>old</p></div>'); |
| 305 |
await htmx.ajax('GET', '/test', { |
| 306 |
target: '#target', |
| 307 |
swap: 'beforeend' |
| 308 |
}); |
| 309 |
assert.include(div.innerHTML, '<p>old</p>'); |
| 310 |
assert.include(div.innerHTML, '<span>new</span>'); |
| 311 |
}); |
| 312 |
}); |