| 1 | describe('hx-confirm attribute', function() { |
| 2 | |
| 3 | beforeEach(() => { |
| 4 | setupTest(this.currentTest) |
| 5 | }) |
| 6 | |
| 7 | afterEach(() => { |
| 8 | cleanupTest(this.currentTest) |
| 9 | }) |
| 10 | |
| 11 | it('blocks request when window.confirm returns false', async function () { |
| 12 | let originalConfirm = window.confirm |
| 13 | window.confirm = () => false |
| 14 | |
| 15 | mockResponse('GET', '/test', 'Success') |
| 16 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Are you sure?">Click</button>') |
| 17 | btn.click() |
| 18 | await forRequest() |
| 19 | |
| 20 | btn.innerText.should.equal('Click') |
| 21 | window.confirm = originalConfirm |
| 22 | }) |
| 23 | |
| 24 | it('allows request when window.confirm returns true', async function () { |
| 25 | let originalConfirm = window.confirm |
| 26 | window.confirm = () => true |
| 27 | |
| 28 | mockResponse('GET', '/test', 'Success') |
| 29 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Are you sure?">Click</button>') |
| 30 | btn.click() |
| 31 | await forRequest() |
| 32 | |
| 33 | playground().innerText.should.equal('Success') |
| 34 | window.confirm = originalConfirm |
| 35 | }) |
| 36 | |
| 37 | it('fires htmx:confirm event', async function () { |
| 38 | let originalConfirm = window.confirm |
| 39 | window.confirm = () => true |
| 40 | |
| 41 | mockResponse('GET', '/test', 'Success') |
| 42 | let confirmFired = false |
| 43 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Delete this?">Click</button>') |
| 44 | btn.addEventListener('htmx:confirm', () => confirmFired = true) |
| 45 | btn.click() |
| 46 | await forRequest() |
| 47 | |
| 48 | assert.isTrue(confirmFired) |
| 49 | window.confirm = originalConfirm |
| 50 | }) |
| 51 | |
| 52 | it('provides ctx in htmx:confirm event detail', async function () { |
| 53 | let originalConfirm = window.confirm |
| 54 | window.confirm = () => true |
| 55 | |
| 56 | mockResponse('GET', '/test', 'Success') |
| 57 | let capturedCtx = null |
| 58 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Proceed?">Click</button>') |
| 59 | btn.addEventListener('htmx:confirm', (e) => capturedCtx = e.detail.ctx) |
| 60 | btn.click() |
| 61 | await forRequest() |
| 62 | |
| 63 | assert.isNotNull(capturedCtx) |
| 64 | assert.equal(capturedCtx.confirm, 'Proceed?') |
| 65 | window.confirm = originalConfirm |
| 66 | }) |
| 67 | |
| 68 | it('provides issueRequest callback in event detail', async function () { |
| 69 | let originalConfirm = window.confirm |
| 70 | window.confirm = () => true |
| 71 | |
| 72 | mockResponse('GET', '/test', 'Success') |
| 73 | let issueRequestCallback = null |
| 74 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Continue?">Click</button>') |
| 75 | btn.addEventListener('htmx:confirm', (e) => issueRequestCallback = e.detail.issueRequest) |
| 76 | btn.click() |
| 77 | await forRequest() |
| 78 | |
| 79 | assert.isFunction(issueRequestCallback) |
| 80 | window.confirm = originalConfirm |
| 81 | }) |
| 82 | |
| 83 | it('allows custom confirmation UI with issueRequest callback', async function () { |
| 84 | mockResponse('GET', '/test', 'Custom confirmed') |
| 85 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Custom?">Click</button>') |
| 86 | |
| 87 | btn.addEventListener('htmx:confirm', (e) => { |
| 88 | e.preventDefault() |
| 89 | setTimeout(() => e.detail.issueRequest(), 10) |
| 90 | }) |
| 91 | |
| 92 | btn.click() |
| 93 | await forRequest() |
| 94 | |
| 95 | playground().innerText.should.equal('Custom confirmed') |
| 96 | }) |
| 97 | |
| 98 | it('cancels request when issueRequest called with false', async function () { |
| 99 | mockResponse('GET', '/test', 'Success') |
| 100 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Cancel?">Click</button>') |
| 101 | |
| 102 | btn.addEventListener('htmx:confirm', (e) => { |
| 103 | e.preventDefault() |
| 104 | e.detail.dropRequest() |
| 105 | }) |
| 106 | |
| 107 | btn.click() |
| 108 | await forRequest() |
| 109 | |
| 110 | btn.innerText.should.equal('Click') |
| 111 | }) |
| 112 | |
| 113 | it('supports js: expressions', async function () { |
| 114 | mockResponse('GET', '/test', 'JS confirmed') |
| 115 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="js:true">Click</button>') |
| 116 | btn.click() |
| 117 | await forRequest() |
| 118 | |
| 119 | playground().innerText.should.equal('JS confirmed') |
| 120 | }) |
| 121 | |
| 122 | it('blocks request with js: returning false', async function () { |
| 123 | mockResponse('GET', '/test', 'Success') |
| 124 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="js:false">Click</button>') |
| 125 | btn.click() |
| 126 | await forRequest() |
| 127 | |
| 128 | btn.innerText.should.equal('Click') |
| 129 | }) |
| 130 | |
| 131 | it('can modify ctx.confirm in htmx:config:request', async function () { |
| 132 | let originalConfirm = window.confirm |
| 133 | window.confirm = (msg) => { |
| 134 | assert.equal(msg, 'Modified message') |
| 135 | return true |
| 136 | } |
| 137 | |
| 138 | mockResponse('GET', '/test', 'Success') |
| 139 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Original">Click</button>') |
| 140 | btn.addEventListener('htmx:config:request', (e) => { |
| 141 | e.detail.ctx.confirm = 'Modified message' |
| 142 | }) |
| 143 | btn.click() |
| 144 | await forRequest() |
| 145 | |
| 146 | playground().innerText.should.equal('Success') |
| 147 | window.confirm = originalConfirm |
| 148 | }) |
| 149 | |
| 150 | it('can disable confirmation by setting ctx.confirm to null', async function () { |
| 151 | mockResponse('GET', '/test', 'No confirm') |
| 152 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Should not see">Click</button>') |
| 153 | |
| 154 | let confirmFired = false |
| 155 | btn.addEventListener('htmx:confirm', () => confirmFired = true) |
| 156 | btn.addEventListener('htmx:config:request', (e) => { |
| 157 | e.detail.ctx.confirm = null |
| 158 | }) |
| 159 | |
| 160 | btn.click() |
| 161 | await forRequest() |
| 162 | |
| 163 | assert.isFalse(confirmFired) |
| 164 | playground().innerText.should.equal('No confirm') |
| 165 | }) |
| 166 | |
| 167 | it('works with forms', async function () { |
| 168 | let originalConfirm = window.confirm |
| 169 | window.confirm = () => true |
| 170 | |
| 171 | mockResponse('POST', '/submit', 'Form submitted') |
| 172 | let form = createProcessedHTML('<form hx-post="/submit" hx-confirm="Submit form?"><button>Submit</button></form>') |
| 173 | form.requestSubmit() |
| 174 | await forRequest() |
| 175 | |
| 176 | playground().innerText.should.equal('Form submitted') |
| 177 | window.confirm = originalConfirm |
| 178 | }) |
| 179 | |
| 180 | it('cleans up indicators when confirmation cancelled', async function () { |
| 181 | let originalConfirm = window.confirm |
| 182 | window.confirm = () => false |
| 183 | |
| 184 | mockResponse('GET', '/test', 'Success') |
| 185 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Cancel?" hx-indicator="#ind">Click</button>') |
| 186 | createProcessedHTML('<div id="ind" class="htmx-indicator">Loading...</div>') |
| 187 | |
| 188 | btn.click() |
| 189 | await forRequest() |
| 190 | |
| 191 | let indicator = find('#ind') |
| 192 | assert.isFalse(indicator.classList.contains('htmx-request')) |
| 193 | window.confirm = originalConfirm |
| 194 | }) |
| 195 | |
| 196 | it('re-enables disabled elements when confirmation cancelled', async function () { |
| 197 | let originalConfirm = window.confirm |
| 198 | window.confirm = () => false |
| 199 | |
| 200 | mockResponse('GET', '/test', 'Success') |
| 201 | let btn = createProcessedHTML('<button hx-get="/test" hx-confirm="Cancel?" hx-disable>Click</button>') |
| 202 | |
| 203 | btn.click() |
| 204 | await forRequest() |
| 205 | |
| 206 | assert.isFalse(btn.disabled) |
| 207 | window.confirm = originalConfirm |
| 208 | }) |
| 209 | }) |