| 1 |
describe('hx-confirm attribute', function() { |
| 2 |
var confirm |
| 3 |
beforeEach(function() { |
| 4 |
this.server = makeServer() |
| 5 |
confirm = sinon.stub(window, 'confirm') |
| 6 |
clearWorkArea() |
| 7 |
}) |
| 8 |
afterEach(function() { |
| 9 |
this.server.restore() |
| 10 |
confirm.restore() |
| 11 |
clearWorkArea() |
| 12 |
}) |
| 13 |
|
| 14 |
it('prompts using window.confirm when hx-confirm is set', function() { |
| 15 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 16 |
confirm.returns(true) |
| 17 |
var btn = make('<button hx-get="/test" hx-confirm="Sure?">Click Me!</button>') |
| 18 |
btn.click() |
| 19 |
confirm.calledOnce.should.equal(true) |
| 20 |
this.server.respond() |
| 21 |
btn.innerHTML.should.equal('Clicked!') |
| 22 |
}) |
| 23 |
|
| 24 |
it('stops the request if confirm is cancelled', function() { |
| 25 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 26 |
confirm.returns(false) |
| 27 |
var btn = make('<button hx-get="/test" hx-confirm="Sure?">Click Me!</button>') |
| 28 |
btn.click() |
| 29 |
confirm.calledOnce.should.equal(true) |
| 30 |
this.server.respond() |
| 31 |
btn.innerHTML.should.equal('Click Me!') |
| 32 |
}) |
| 33 |
|
| 34 |
it('uses the value of hx-confirm as the prompt', function() { |
| 35 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 36 |
confirm.returns(false) |
| 37 |
var btn = make('<button hx-get="/test" hx-confirm="Sure?">Click Me!</button>') |
| 38 |
btn.click() |
| 39 |
confirm.firstCall.args[0].should.equal('Sure?') |
| 40 |
this.server.respond() |
| 41 |
btn.innerHTML.should.equal('Click Me!') |
| 42 |
}) |
| 43 |
|
| 44 |
it('should prompt when htmx:confirm handler calls issueRequest', function() { |
| 45 |
try { |
| 46 |
var btn = make('<button hx-get="/test" hx-confirm="Surely?">Click Me!</button>') |
| 47 |
var handler = htmx.on('htmx:confirm', function(evt) { |
| 48 |
evt.preventDefault() |
| 49 |
evt.detail.issueRequest() |
| 50 |
}) |
| 51 |
btn.click() |
| 52 |
confirm.calledOnce.should.equal(true) |
| 53 |
} finally { |
| 54 |
htmx.off('htmx:confirm', handler) |
| 55 |
} |
| 56 |
}) |
| 57 |
|
| 58 |
it('should include the question in htmx:confirm event', function() { |
| 59 |
var stub = sinon.stub() |
| 60 |
try { |
| 61 |
var btn = make('<button hx-get="/test" hx-confirm="Surely?">Click Me!</button>') |
| 62 |
var handler = htmx.on('htmx:confirm', stub) |
| 63 |
btn.click() |
| 64 |
stub.calledOnce.should.equal(true) |
| 65 |
stub.firstCall.args[0].detail.should.have.property('question', 'Surely?') |
| 66 |
} finally { |
| 67 |
htmx.off('htmx:confirm', handler) |
| 68 |
} |
| 69 |
}) |
| 70 |
|
| 71 |
it('should allow skipping built-in window.confirm when using issueRequest', function() { |
| 72 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 73 |
try { |
| 74 |
var btn = make('<button hx-get="/test" hx-confirm="Sure?">Click Me!</button>') |
| 75 |
var handler = htmx.on('htmx:confirm', function(evt) { |
| 76 |
evt.detail.question.should.equal('Sure?') |
| 77 |
evt.preventDefault() |
| 78 |
evt.detail.issueRequest(true) |
| 79 |
}) |
| 80 |
btn.click() |
| 81 |
confirm.called.should.equal(false) |
| 82 |
this.server.respond() |
| 83 |
btn.innerHTML.should.equal('Clicked!') |
| 84 |
} finally { |
| 85 |
htmx.off('htmx:confirm', handler) |
| 86 |
} |
| 87 |
}) |
| 88 |
|
| 89 |
it('should allow htmx:confirm even when no hx-confirm is set', function() { |
| 90 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 91 |
try { |
| 92 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 93 |
var handler = htmx.on('htmx:confirm', function(evt) { |
| 94 |
evt.detail.should.have.property('question', null) |
| 95 |
evt.preventDefault() |
| 96 |
evt.detail.issueRequest() |
| 97 |
}) |
| 98 |
btn.click() |
| 99 |
confirm.called.should.equal(false) |
| 100 |
this.server.respond() |
| 101 |
btn.innerHTML.should.equal('Clicked!') |
| 102 |
} finally { |
| 103 |
htmx.off('htmx:confirm', handler) |
| 104 |
} |
| 105 |
}) |
| 106 |
}) |