describe('hx-swap-oob attribute', function() {
const savedConfig = htmx.config
beforeEach(function() {
this.server = makeServer()
htmx.config = Object.assign({}, savedConfig)
clearWorkArea()
})
afterEach(function() {
this.server.restore()
htmx.config = savedConfig
clearWorkArea()
})
// Repeat the same test to make sure it works with different configurations
for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
it('handles basic response properly with config ' + JSON.stringify(config), function() {
Object.assign(htmx.config, config)
this.server.respondWith('GET', '/test', "Clicked
Swapped0
")
var div = make('
click me
')
make('')
div.click()
this.server.respond()
div.innerHTML.should.equal('Clicked')
byId('d1').innerHTML.should.equal('Swapped0')
})
}
for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
it('oob swap works when the response has a body tag with config ' + JSON.stringify(config), function() {
Object.assign(htmx.config, config)
this.server.respondWith('GET', '/test', "Clicked
Swapped0
")
var div = make('
click me
')
make('')
div.click()
this.server.respond()
div.innerHTML.should.equal('Clicked')
byId('d2').innerHTML.should.equal('Swapped0')
})
}
for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
it('oob swap works when the response has html and body tags with config ' + JSON.stringify(config), function() {
Object.assign(htmx.config, config)
this.server.respondWith('GET', '/test', "Clicked
Swapped0
")
var div = make('
click me
')
make('')
div.click()
this.server.respond()
div.innerHTML.should.equal('Clicked')
byId('d3').innerHTML.should.equal('Swapped0')
})
}
for (const config of [{ allowNestedOobSwaps: true }, { allowNestedOobSwaps: false }]) {
it('handles more than one oob swap properly with config ' + JSON.stringify(config), function() {
Object.assign(htmx.config, config)
this.server.respondWith('GET', '/test', "Clicked
Swapped1
Swapped2
")
var div = make('
click me
')
make('')
make('')
div.click()
this.server.respond()
div.innerHTML.should.equal('Clicked')
byId('d1').innerHTML.should.equal('Swapped1')
byId('d2').innerHTML.should.equal('Swapped2')
})
}
it('handles no id match properly', function() {
this.server.respondWith('GET', '/test', "Clicked
')
byId('d1').innerHTML.should.equal('Swapped7')
})
it('swaps into all targets that match the selector (innerHTML)', function() {
this.server.respondWith('GET', '/test', "
Clicked
Swapped8
")
var div = make('
click me
')
make('
No swap
')
make('
Not swapped
')
make('
Not swapped
')
div.click()
this.server.respond()
byId('d1').innerHTML.should.equal('No swap')
byId('d2').innerHTML.should.equal('Swapped8')
byId('d3').innerHTML.should.equal('Swapped8')
})
it('swaps into all targets that match the selector (outerHTML)', function() {
var oobSwapContent = '