| 1 | describe('hx-prompt extension', function() { |
| 2 | |
| 3 | let extBackup; |
| 4 | let originalPrompt; |
| 5 | |
| 6 | before(async () => { |
| 7 | extBackup = backupExtensions(); |
| 8 | clearExtensions(); |
| 9 | htmx.config.extensions = 'hx-prompt'; |
| 10 | |
| 11 | let script = document.createElement('script'); |
| 12 | script.src = '../src/ext/hx-prompt.js'; |
| 13 | await new Promise(resolve => { |
| 14 | script.onload = resolve; |
| 15 | document.head.appendChild(script); |
| 16 | }); |
| 17 | }); |
| 18 | |
| 19 | after(() => { |
| 20 | restoreExtensions(extBackup); |
| 21 | }); |
| 22 | |
| 23 | beforeEach(() => { |
| 24 | setupTest(); |
| 25 | originalPrompt = window.prompt; |
| 26 | }); |
| 27 | |
| 28 | afterEach(() => { |
| 29 | cleanupTest(); |
| 30 | window.prompt = originalPrompt; |
| 31 | delete window.htmxPrompt; |
| 32 | }); |
| 33 | |
| 34 | it('sends prompt response as HX-Prompt header', async function() { |
| 35 | window.prompt = () => 'because'; |
| 36 | mockResponse('DELETE', '/items/1', 'ok'); |
| 37 | |
| 38 | let btn = createProcessedHTML('<button hx-delete="/items/1" hx-prompt="Reason for deletion?">Delete</button>'); |
| 39 | btn.click(); |
| 40 | await forRequest(); |
| 41 | |
| 42 | assert.equal(lastFetch().request.headers['HX-Prompt'], 'because'); |
| 43 | }); |
| 44 | |
| 45 | it('URI-encodes unicode prompt responses for fetch headers', async function() { |
| 46 | window.prompt = () => '中文 reason'; |
| 47 | |
| 48 | let fetchCalled = false; |
| 49 | let encodedHeader; |
| 50 | let btn = createProcessedHTML('<button hx-delete="/items/1" hx-prompt="Reason for deletion?">Delete</button>'); |
| 51 | btn.addEventListener('htmx:before:request', (e) => { |
| 52 | e.detail.ctx.fetch = async (action, request) => { |
| 53 | fetchCalled = true; |
| 54 | encodedHeader = request.headers['HX-Prompt']; |
| 55 | new Request(new URL(action, location.href), request); |
| 56 | return new Response('ok'); |
| 57 | }; |
| 58 | }); |
| 59 | |
| 60 | btn.click(); |
| 61 | await forRequest(); |
| 62 | |
| 63 | assert.isTrue(fetchCalled); |
| 64 | assert.equal(encodedHeader, encodeURI('中文 reason')); |
| 65 | }); |
| 66 | |
| 67 | it('aborts the request when the user cancels', async function() { |
| 68 | window.prompt = () => null; |
| 69 | mockResponse('DELETE', '/items/1', 'ok'); |
| 70 | |
| 71 | let btn = createProcessedHTML('<button hx-delete="/items/1" hx-prompt="Reason for deletion?">Delete</button>'); |
| 72 | btn.click(); |
| 73 | await forRequest(); |
| 74 | |
| 75 | assert.isUndefined(fetchMock.getLastCall()); |
| 76 | }); |
| 77 | |
| 78 | it('accepts an empty string as a valid answer', async function() { |
| 79 | window.prompt = () => ''; |
| 80 | mockResponse('DELETE', '/items/1', 'ok'); |
| 81 | |
| 82 | let btn = createProcessedHTML('<button hx-delete="/items/1" hx-prompt="Reason for deletion?">Delete</button>'); |
| 83 | btn.click(); |
| 84 | await forRequest(); |
| 85 | |
| 86 | assert.equal(lastFetch().request.headers['HX-Prompt'], ''); |
| 87 | }); |
| 88 | |
| 89 | it('does nothing when hx-prompt is absent', async function() { |
| 90 | let prompted = false; |
| 91 | window.prompt = () => { prompted = true; return 'x'; }; |
| 92 | mockResponse('GET', '/items/1', 'ok'); |
| 93 | |
| 94 | let btn = createProcessedHTML('<button hx-get="/items/1">Go</button>'); |
| 95 | btn.click(); |
| 96 | await forRequest(); |
| 97 | |
| 98 | assert.isFalse(prompted); |
| 99 | assert.isUndefined(lastFetch().request.headers['HX-Prompt']); |
| 100 | }); |
| 101 | |
| 102 | it('inherits via :inherited from a container', async function() { |
| 103 | window.prompt = () => 'inherited'; |
| 104 | mockResponse('DELETE', '/items/1', 'ok'); |
| 105 | |
| 106 | createProcessedHTML(` |
| 107 | <div hx-prompt:inherited="Reason?"> |
| 108 | <button hx-delete="/items/1">Delete</button> |
| 109 | <button hx-delete="/items/2">Delete</button> |
| 110 | </div> |
| 111 | `); |
| 112 | find('button').click(); |
| 113 | await forRequest(); |
| 114 | |
| 115 | assert.equal(lastFetch().request.headers['HX-Prompt'], 'inherited'); |
| 116 | }); |
| 117 | |
| 118 | it('composes with hx-confirm (both pass)', async function() { |
| 119 | window.prompt = () => 'a reason'; |
| 120 | let originalConfirm = window.confirm; |
| 121 | window.confirm = () => true; |
| 122 | mockResponse('DELETE', '/items/1', 'ok'); |
| 123 | |
| 124 | let btn = createProcessedHTML( |
| 125 | '<button hx-delete="/items/1" hx-prompt="Reason?" hx-confirm="Are you sure?">Delete</button>' |
| 126 | ); |
| 127 | btn.click(); |
| 128 | await forRequest(); |
| 129 | |
| 130 | assert.equal(lastFetch().request.headers['HX-Prompt'], 'a%20reason'); |
| 131 | window.confirm = originalConfirm; |
| 132 | }); |
| 133 | |
| 134 | it('cancelling hx-prompt skips hx-confirm and the request', async function() { |
| 135 | window.prompt = () => null; |
| 136 | let confirmShown = false; |
| 137 | let originalConfirm = window.confirm; |
| 138 | window.confirm = () => { confirmShown = true; return true; }; |
| 139 | mockResponse('DELETE', '/items/1', 'ok'); |
| 140 | |
| 141 | let btn = createProcessedHTML( |
| 142 | '<button hx-delete="/items/1" hx-prompt="Reason?" hx-confirm="Are you sure?">Delete</button>' |
| 143 | ); |
| 144 | btn.click(); |
| 145 | await forRequest(); |
| 146 | |
| 147 | assert.isFalse(confirmShown); |
| 148 | assert.isUndefined(fetchMock.getLastCall()); |
| 149 | window.confirm = originalConfirm; |
| 150 | }); |
| 151 | |
| 152 | it('exposes prompt and target on the htmx:prompt event detail', async function() { |
| 153 | window.prompt = () => 'hello'; |
| 154 | mockResponse('DELETE', '/items/1', 'ok'); |
| 155 | |
| 156 | let captured = null; |
| 157 | let btn = createProcessedHTML('<button hx-delete="/items/1" hx-prompt="Q?">Delete</button>'); |
| 158 | btn.addEventListener('htmx:prompt', (e) => captured = e.detail); |
| 159 | btn.click(); |
| 160 | await forRequest(); |
| 161 | |
| 162 | assert.equal(captured.prompt, 'hello'); |
| 163 | assert.equal(captured.target, btn); |
| 164 | }); |
| 165 | |
| 166 | it('hx-on::prompt can preventDefault to abort the request', async function() { |
| 167 | mockResponse('DELETE', '/items/1', 'ok'); |
| 168 | let btn = createProcessedHTML( |
| 169 | '<button hx-delete="/items/1" hx-prompt="Reason?" hx-on::prompt="if (prompt.length < 3) event.preventDefault()">Delete</button>' |
| 170 | ); |
| 171 | |
| 172 | window.prompt = () => 'no'; |
| 173 | btn.click(); |
| 174 | await forRequest(); |
| 175 | assert.isUndefined(fetchMock.getLastCall()); |
| 176 | |
| 177 | window.prompt = () => 'long enough'; |
| 178 | btn.click(); |
| 179 | await forRequest(); |
| 180 | assert.equal(lastFetch().request.headers['HX-Prompt'], 'long%20enough'); |
| 181 | }); |
| 182 | |
| 183 | it('uses window.htmxPrompt when defined', async function() { |
| 184 | window.htmxPrompt = (question) => 'answered: ' + question; |
| 185 | window.prompt = () => { throw new Error('window.prompt should not be called'); }; |
| 186 | mockResponse('DELETE', '/items/1', 'ok'); |
| 187 | |
| 188 | let btn = createProcessedHTML('<button hx-delete="/items/1" hx-prompt="Q?">Delete</button>'); |
| 189 | btn.click(); |
| 190 | await forRequest(); |
| 191 | |
| 192 | assert.equal(lastFetch().request.headers['HX-Prompt'], 'answered:%20Q?'); |
| 193 | }); |
| 194 | |
| 195 | it('null from window.htmxPrompt aborts the request', async function() { |
| 196 | window.htmxPrompt = () => null; |
| 197 | mockResponse('DELETE', '/items/1', 'ok'); |
| 198 | |
| 199 | let btn = createProcessedHTML('<button hx-delete="/items/1" hx-prompt="Q?">Delete</button>'); |
| 200 | btn.click(); |
| 201 | await forRequest(); |
| 202 | |
| 203 | assert.isUndefined(fetchMock.getLastCall()); |
| 204 | }); |
| 205 | |
| 206 | describe('hx-on recipe (no extension needed)', () => { |
| 207 | const recipe = "ctx.request.headers['HX-Prompt'] = prompt('Reason?') ?? event.preventDefault()"; |
| 208 | |
| 209 | it('sends HX-Prompt with the answer', async function() { |
| 210 | window.prompt = () => 'a reason'; |
| 211 | mockResponse('DELETE', '/items/1', 'ok'); |
| 212 | |
| 213 | let btn = createProcessedHTML(`<button hx-delete="/items/1" hx-on::config:request="${recipe}">Delete</button>`); |
| 214 | btn.click(); |
| 215 | await forRequest(); |
| 216 | |
| 217 | assert.equal(lastFetch().request.headers['HX-Prompt'], 'a reason'); |
| 218 | }); |
| 219 | |
| 220 | it('aborts the request on cancel', async function() { |
| 221 | window.prompt = () => null; |
| 222 | mockResponse('DELETE', '/items/1', 'ok'); |
| 223 | |
| 224 | let btn = createProcessedHTML(`<button hx-delete="/items/1" hx-on::config:request="${recipe}">Delete</button>`); |
| 225 | btn.click(); |
| 226 | await forRequest(); |
| 227 | |
| 228 | assert.isUndefined(fetchMock.getLastCall()); |
| 229 | }); |
| 230 | }); |
| 231 | }); |