| 1 |
describe('htmx config test', function() { |
| 2 |
beforeEach(function() { |
| 3 |
this.server = makeServer() |
| 4 |
clearWorkArea() |
| 5 |
}) |
| 6 |
|
| 7 |
afterEach(function() { |
| 8 |
this.server.restore() |
| 9 |
clearWorkArea() |
| 10 |
}) |
| 11 |
|
| 12 |
it('swaps normally with no config update', function() { |
| 13 |
var responseCode = null |
| 14 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 15 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '' + responseCode) |
| 16 |
}) |
| 17 |
|
| 18 |
responseCode = 200 |
| 19 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 20 |
btn.click() |
| 21 |
this.server.respond() |
| 22 |
btn.innerHTML.should.equal('200') |
| 23 |
|
| 24 |
responseCode = 204 |
| 25 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 26 |
btn.click() |
| 27 |
this.server.respond() |
| 28 |
btn.innerHTML.should.equal('Click Me!') |
| 29 |
|
| 30 |
responseCode = 300 |
| 31 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 32 |
btn.click() |
| 33 |
this.server.respond() |
| 34 |
btn.innerHTML.should.equal('300') |
| 35 |
|
| 36 |
responseCode = 400 |
| 37 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 38 |
btn.click() |
| 39 |
this.server.respond() |
| 40 |
btn.innerHTML.should.equal('Click Me!') |
| 41 |
|
| 42 |
responseCode = 500 |
| 43 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 44 |
btn.click() |
| 45 |
this.server.respond() |
| 46 |
btn.innerHTML.should.equal('Click Me!') |
| 47 |
}) |
| 48 |
|
| 49 |
it('swap all config should swap everything', function() { |
| 50 |
var originalResponseHandling = htmx.config.responseHandling |
| 51 |
try { |
| 52 |
htmx.config.responseHandling = [{ code: '...', swap: true }] |
| 53 |
|
| 54 |
var responseCode = null |
| 55 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 56 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '' + responseCode) |
| 57 |
}) |
| 58 |
|
| 59 |
responseCode = 200 |
| 60 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 61 |
btn.click() |
| 62 |
this.server.respond() |
| 63 |
btn.innerHTML.should.equal('200') |
| 64 |
|
| 65 |
responseCode = 203 |
| 66 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 67 |
btn.click() |
| 68 |
this.server.respond() |
| 69 |
btn.innerHTML.should.equal('203') |
| 70 |
|
| 71 |
responseCode = 300 |
| 72 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 73 |
btn.click() |
| 74 |
this.server.respond() |
| 75 |
btn.innerHTML.should.equal('300') |
| 76 |
|
| 77 |
responseCode = 400 |
| 78 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 79 |
btn.click() |
| 80 |
this.server.respond() |
| 81 |
btn.innerHTML.should.equal('400') |
| 82 |
|
| 83 |
responseCode = 500 |
| 84 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 85 |
btn.click() |
| 86 |
this.server.respond() |
| 87 |
btn.innerHTML.should.equal('500') |
| 88 |
} finally { |
| 89 |
htmx.config.responseHandling = originalResponseHandling |
| 90 |
} |
| 91 |
}) |
| 92 |
|
| 93 |
it('non mapped responseHandling config will not swap', function() { |
| 94 |
var originalResponseHandling = htmx.config.responseHandling |
| 95 |
try { |
| 96 |
htmx.config.responseHandling = [{ code: '200', swap: true }] |
| 97 |
|
| 98 |
var responseCode = null |
| 99 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 100 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '' + responseCode) |
| 101 |
}) |
| 102 |
|
| 103 |
responseCode = 400 |
| 104 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 105 |
btn.click() |
| 106 |
this.server.respond() |
| 107 |
btn.innerHTML.should.equal('Click Me!') |
| 108 |
|
| 109 |
responseCode = 200 |
| 110 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 111 |
btn.click() |
| 112 |
this.server.respond() |
| 113 |
btn.innerHTML.should.equal('200') |
| 114 |
} finally { |
| 115 |
htmx.config.responseHandling = originalResponseHandling |
| 116 |
} |
| 117 |
}) |
| 118 |
|
| 119 |
it('can change the target of a given response code', function() { |
| 120 |
var originalResponseHandling = htmx.config.responseHandling |
| 121 |
try { |
| 122 |
htmx.config.responseHandling = originalResponseHandling.slice() |
| 123 |
htmx.config.responseHandling.unshift({ code: '444', swap: true, target: '#a-div' }) |
| 124 |
|
| 125 |
var responseCode = null |
| 126 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 127 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '' + responseCode) |
| 128 |
}) |
| 129 |
|
| 130 |
responseCode = 444 |
| 131 |
var div = make('<div id="a-div">Another Div</div>') |
| 132 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 133 |
btn.click() |
| 134 |
this.server.respond() |
| 135 |
btn.innerHTML.should.equal('Click Me!') |
| 136 |
div.innerHTML.should.equal('444') |
| 137 |
} finally { |
| 138 |
htmx.config.responseHandling = originalResponseHandling |
| 139 |
} |
| 140 |
}) |
| 141 |
|
| 142 |
it('throws targetError if you the target in responseHandling is invalid', function() { |
| 143 |
var originalResponseHandling = htmx.config.responseHandling |
| 144 |
try { |
| 145 |
var error = false |
| 146 |
var handler = htmx.on('htmx:targetError', function(evt) { |
| 147 |
evt.detail.target.should.equal('#a-div') |
| 148 |
error = true |
| 149 |
}) |
| 150 |
htmx.config.responseHandling = originalResponseHandling.slice() |
| 151 |
htmx.config.responseHandling.unshift({ code: '444', swap: true, target: '#a-div' }) |
| 152 |
|
| 153 |
var responseCode = null |
| 154 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 155 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '' + responseCode) |
| 156 |
}) |
| 157 |
|
| 158 |
responseCode = 444 |
| 159 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 160 |
btn.click() |
| 161 |
this.server.respond() |
| 162 |
btn.innerHTML.should.equal('Click Me!') |
| 163 |
} catch (e) { |
| 164 |
} finally { |
| 165 |
htmx.config.responseHandling = originalResponseHandling |
| 166 |
htmx.off('htmx:targetError', handler) |
| 167 |
error.should.equal(true) |
| 168 |
} |
| 169 |
}) |
| 170 |
|
| 171 |
it('can change the target to "this" in a given response code', function() { |
| 172 |
var originalResponseHandling = htmx.config.responseHandling |
| 173 |
try { |
| 174 |
htmx.config.responseHandling = originalResponseHandling.slice() |
| 175 |
htmx.config.responseHandling.unshift({ code: '444', swap: true, target: 'this' }) |
| 176 |
|
| 177 |
var responseCode = null |
| 178 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 179 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '' + responseCode) |
| 180 |
}) |
| 181 |
|
| 182 |
responseCode = 444 |
| 183 |
var div = make('<div id="a-div">Another Div</div>') |
| 184 |
var btn = make('<button hx-target="#a-div" hx-get="/test">Click Me!</button>') |
| 185 |
btn.click() |
| 186 |
this.server.respond() |
| 187 |
btn.innerHTML.should.equal('444') |
| 188 |
div.innerHTML.should.equal('Another Div') |
| 189 |
} finally { |
| 190 |
htmx.config.responseHandling = originalResponseHandling |
| 191 |
} |
| 192 |
}) |
| 193 |
|
| 194 |
it('can change the swap type of a given response code', function() { |
| 195 |
var originalResponseHandling = htmx.config.responseHandling |
| 196 |
try { |
| 197 |
htmx.config.responseHandling = originalResponseHandling.slice() |
| 198 |
htmx.config.responseHandling.unshift({ code: '444', swap: true, target: '#a-div', swapOverride: 'outerHTML' }) |
| 199 |
|
| 200 |
var responseCode = null |
| 201 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 202 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '' + responseCode) |
| 203 |
}) |
| 204 |
|
| 205 |
responseCode = 444 |
| 206 |
var div = make('<div><div id="a-div">Another Div</div></div>') |
| 207 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 208 |
btn.click() |
| 209 |
this.server.respond() |
| 210 |
btn.innerHTML.should.equal('Click Me!') |
| 211 |
div.innerHTML.should.equal('444') |
| 212 |
} finally { |
| 213 |
htmx.config.responseHandling = originalResponseHandling |
| 214 |
} |
| 215 |
}) |
| 216 |
|
| 217 |
it('can change the select of a given response code', function() { |
| 218 |
var originalResponseHandling = htmx.config.responseHandling |
| 219 |
try { |
| 220 |
htmx.config.responseHandling = originalResponseHandling.slice() |
| 221 |
htmx.config.responseHandling.unshift({ code: '444', swap: true, select: '.foo' }) |
| 222 |
|
| 223 |
var responseCode = null |
| 224 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 225 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, "<div><a class='foo'>" + responseCode + '</a></div>') |
| 226 |
}) |
| 227 |
|
| 228 |
responseCode = 444 |
| 229 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 230 |
btn.click() |
| 231 |
this.server.respond() |
| 232 |
btn.innerHTML.should.equal('<a class="foo">444</a>') |
| 233 |
} finally { |
| 234 |
htmx.config.responseHandling = originalResponseHandling |
| 235 |
} |
| 236 |
}) |
| 237 |
|
| 238 |
it('can change if the title is ignored for a given response code', function() { |
| 239 |
var originalResponseHandling = htmx.config.responseHandling |
| 240 |
var originalTitle = document.title |
| 241 |
try { |
| 242 |
htmx.config.responseHandling = originalResponseHandling.slice() |
| 243 |
htmx.config.responseHandling.unshift({ code: '444', swap: true, ignoreTitle: true }) |
| 244 |
|
| 245 |
var responseCode = null |
| 246 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 247 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '<title>Should Not Be Set</title>' + responseCode) |
| 248 |
}) |
| 249 |
|
| 250 |
responseCode = 444 |
| 251 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 252 |
btn.click() |
| 253 |
this.server.respond() |
| 254 |
btn.innerHTML.should.equal('444') |
| 255 |
document.title.should.equal(originalTitle) |
| 256 |
} finally { |
| 257 |
htmx.config.responseHandling = originalResponseHandling |
| 258 |
} |
| 259 |
}) |
| 260 |
|
| 261 |
it('can change if error for a given response code', function() { |
| 262 |
var originalResponseHandling = htmx.config.responseHandling |
| 263 |
var errorDetected = false |
| 264 |
var handler = htmx.on('htmx:responseError', function() { |
| 265 |
errorDetected = true |
| 266 |
}) |
| 267 |
try { |
| 268 |
htmx.config.responseHandling = originalResponseHandling.slice() |
| 269 |
htmx.config.responseHandling.unshift({ code: '444', swap: true, error: false }) |
| 270 |
|
| 271 |
var responseCode = null |
| 272 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 273 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '' + responseCode) |
| 274 |
}) |
| 275 |
|
| 276 |
responseCode = 444 |
| 277 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 278 |
btn.click() |
| 279 |
this.server.respond() |
| 280 |
btn.innerHTML.should.equal('444') |
| 281 |
errorDetected.should.equal(false) |
| 282 |
} finally { |
| 283 |
htmx.off('htmx:responseError', handler) |
| 284 |
htmx.config.responseHandling = originalResponseHandling |
| 285 |
} |
| 286 |
}) |
| 287 |
|
| 288 |
it('can trigger an event for a given response code', function() { |
| 289 |
var originalResponseHandling = htmx.config.responseHandling |
| 290 |
var myEventWasTriggered = false |
| 291 |
var handler = htmx.on('myEvent', function() { |
| 292 |
myEventWasTriggered = true |
| 293 |
}) |
| 294 |
try { |
| 295 |
htmx.config.responseHandling = originalResponseHandling.slice() |
| 296 |
htmx.config.responseHandling.unshift({ code: '444', swap: false, error: false, event: 'myEvent' }) |
| 297 |
|
| 298 |
var responseCode = null |
| 299 |
this.server.respondWith('GET', '/test', function(xhr, id) { |
| 300 |
xhr.respond(responseCode, { 'Content-Type': 'text/html' }, '' + responseCode) |
| 301 |
}) |
| 302 |
|
| 303 |
responseCode = 444 |
| 304 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 305 |
btn.click() |
| 306 |
this.server.respond() |
| 307 |
btn.innerHTML.should.equal('Click Me!') |
| 308 |
myEventWasTriggered.should.equal(true) |
| 309 |
} finally { |
| 310 |
htmx.off('htmx:responseError', handler) |
| 311 |
htmx.config.responseHandling = originalResponseHandling |
| 312 |
} |
| 313 |
}) |
| 314 |
}) |