describe("htmx config test", function () {
beforeEach(function () {
this.server = makeServer();
clearWorkArea();
});
afterEach(function () {
this.server.restore();
clearWorkArea();
});
it('swaps normally with no config update', function () {
var responseCode = null;
this.server.respondWith("GET", "/test", function (xhr, id) {
xhr.respond(responseCode, {"Content-Type": "application/json"}, "" + responseCode);
});
responseCode = 200; // 200 should cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("200");
responseCode = 203; // 203 should not cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("Click Me!");
responseCode = 300; // 300 should cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("300");
responseCode = 400; // 400 should not cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("Click Me!");
responseCode = 500; // 500 should not cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("Click Me!");
});
it('swap all config should swap everything', function () {
var originalResponseHandling = htmx.config.responseHandling;
try {
htmx.config.responseHandling = [{code: '...', swap: true}];
var responseCode = null;
this.server.respondWith("GET", "/test", function (xhr, id) {
xhr.respond(responseCode, {"Content-Type": "application/json"}, "" + responseCode);
});
responseCode = 200; // 200 should cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("200");
responseCode = 203; // 203 should not cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("203");
responseCode = 300; // 300 should cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("300");
responseCode = 400; // 400 should not cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("400");
responseCode = 500; // 500 should not cause a swap by default
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("500");
} finally {
htmx.config.responseHandling = originalResponseHandling;
}
});
it('can change the target of a given response code', function () {
var originalResponseHandling = htmx.config.responseHandling;
try {
htmx.config.responseHandling = originalResponseHandling.slice();
htmx.config.responseHandling.unshift({code: "444", swap: true, target: "#a-div"})
var responseCode = null;
this.server.respondWith("GET", "/test", function (xhr, id) {
xhr.respond(responseCode, {"Content-Type": "application/json"}, "" + responseCode);
});
responseCode = 444;
var div = make('
Another Div
')
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("Click Me!");
div.innerHTML.should.equal("444");
} finally {
htmx.config.responseHandling = originalResponseHandling;
}
});
it('can change the swap type of a given response code', function () {
var originalResponseHandling = htmx.config.responseHandling;
try {
htmx.config.responseHandling = originalResponseHandling.slice();
htmx.config.responseHandling.unshift({code: "444", swap: true, target: "#a-div", swapOverride:"outerHTML"})
var responseCode = null;
this.server.respondWith("GET", "/test", function (xhr, id) {
xhr.respond(responseCode, {"Content-Type": "application/json"}, "" + responseCode);
});
responseCode = 444;
var div = make('
Another Div
')
var btn = make('')
btn.click();
this.server.respond();
btn.innerHTML.should.equal("Click Me!");
div.innerHTML.should.equal("444");
} finally {
htmx.config.responseHandling = originalResponseHandling;
}
});
it('can change the select of a given response code', function () {
var originalResponseHandling = htmx.config.responseHandling;
try {
htmx.config.responseHandling = originalResponseHandling.slice();
htmx.config.responseHandling.unshift({code: "444", swap: true, select: ".foo"})
var responseCode = null;
this.server.respondWith("GET", "/test", function (xhr, id) {
xhr.respond(responseCode, {"Content-Type": "application/json"}, "