| 1 | describe('Basic Functionality', () => { |
| 2 | |
| 3 | afterEach(() => { |
| 4 | cleanupTest(); |
| 5 | }) |
| 6 | |
| 7 | it('Button click triggers fetch and swaps content', async ()=> { |
| 8 | // Set up mock response |
| 9 | mockResponse('GET', '/demo', '<div id="result">Success!</div>'); |
| 10 | // Create test button |
| 11 | createProcessedHTML('<button id="test-btn" hx-action="/demo" hx-target="#target">Click</button><div id="target">Original</div>'); |
| 12 | // Click the button |
| 13 | find("#test-btn").click() |
| 14 | await forRequest(); |
| 15 | |
| 16 | // Verify the swap occurred |
| 17 | assertTextContentIs("#target", "Success!"); |
| 18 | }) |
| 19 | |
| 20 | it('validation errors prevent submission of a form', async function() { |
| 21 | // Set up mock response |
| 22 | mockResponse('POST', '/demo', '<div id="result">Success!</div>'); |
| 23 | |
| 24 | createProcessedHTML('<form><input id="i1" required/><button id="b1" hx-post="/demo" hx-validate="true">Demo</button></form>'); |
| 25 | |
| 26 | find('#b1').click(); |
| 27 | assert.equal(fetchMock.pendingRequests.length, 0); |
| 28 | |
| 29 | // fill in the required value |
| 30 | find("#i1").value = "foo" |
| 31 | find('#b1').click() |
| 32 | await forRequestWithDelay(); |
| 33 | |
| 34 | assert.isUndefined(find('#target')); |
| 35 | assertTextContentIs("#result", "Success!"); |
| 36 | }) |
| 37 | |
| 38 | it('validation errors do not prevent submission of an element within a form marked as hx-validate=false', async function() { |
| 39 | // Set up mock response |
| 40 | mockResponse('POST', '/demo', new MockResponse('<div id="result">Success!</div>')); |
| 41 | // Create test button |
| 42 | createProcessedHTML('<form><input id="i1" required/><button id="b1" hx-post="/demo" hx-validate="false">Demo</button></form>'); |
| 43 | |
| 44 | // Click the button |
| 45 | find('#b1').click() |
| 46 | await forRequestWithDelay(); |
| 47 | |
| 48 | assert.isUndefined(find('#target')); |
| 49 | assertTextContentIs("#result", "Success!"); |
| 50 | }) |
| 51 | |
| 52 | it('validation errors prevent submission of a single input with hx-validate=true', async function() { |
| 53 | mockResponse('POST', '/demo', '<div id="result">Success!</div>'); |
| 54 | createProcessedHTML('<input id="i1" required hx-post="/demo" hx-validate="true" name="test" hx-trigger="click"/>'); |
| 55 | |
| 56 | find('#i1').click(); |
| 57 | assert.equal(fetchMock.pendingRequests.length, 0); |
| 58 | |
| 59 | find("#i1").value = "foo" |
| 60 | find('#i1').click() |
| 61 | await forRequestWithDelay(); |
| 62 | |
| 63 | assertTextContentIs("#result", "Success!"); |
| 64 | }) |
| 65 | |
| 66 | it('validation errors prevent submission of hx-included inputs', async function() { |
| 67 | mockResponse('POST', '/demo', '<div id="result">Success!</div>'); |
| 68 | createProcessedHTML('<input id="i1" required name="test1"/><button id="b1" hx-post="/demo" hx-validate="true" hx-include="#i1">Submit</button>'); |
| 69 | |
| 70 | find('#b1').click(); |
| 71 | assert.equal(fetchMock.pendingRequests.length, 0); |
| 72 | |
| 73 | find("#i1").value = "foo" |
| 74 | find('#b1').click() |
| 75 | await forRequestWithDelay(); |
| 76 | |
| 77 | assertTextContentIs("#result", "Success!"); |
| 78 | }) |
| 79 | |
| 80 | it('form with noValidate does not validate by default', async function() { |
| 81 | mockResponse('POST', '/demo', '<div id="result">Success!</div>'); |
| 82 | createProcessedHTML('<form novalidate><input id="i1" required name="test"/><button id="b1" hx-post="/demo">Submit</button></form>'); |
| 83 | |
| 84 | find('#b1').click(); |
| 85 | await forRequestWithDelay(); |
| 86 | |
| 87 | assertTextContentIs("#result", "Success!"); |
| 88 | }) |
| 89 | |
| 90 | it('form with noValidate can be overridden with hx-validate=true', async function() { |
| 91 | mockResponse('POST', '/demo', '<div id="result">Success!</div>'); |
| 92 | createProcessedHTML('<form novalidate><input id="i1" required name="test"/><button id="b1" hx-post="/demo" hx-validate="true">Submit</button></form>'); |
| 93 | |
| 94 | find('#b1').click(); |
| 95 | assert.equal(fetchMock.pendingRequests.length, 0); |
| 96 | |
| 97 | find("#i1").value = "foo" |
| 98 | find('#b1').click() |
| 99 | await forRequestWithDelay(); |
| 100 | |
| 101 | assertTextContentIs("#result", "Success!"); |
| 102 | }) |
| 103 | |
| 104 | it('submit button with formNoValidate skips validation', async function() { |
| 105 | mockResponse('POST', '/demo', '<div id="result">Success!</div>'); |
| 106 | createProcessedHTML('<form><input id="i1" required name="test"/><button id="b1" hx-post="/demo" formnovalidate>Submit</button></form>'); |
| 107 | |
| 108 | find('#b1').click(); |
| 109 | await forRequestWithDelay(); |
| 110 | |
| 111 | assertTextContentIs("#result", "Success!"); |
| 112 | }) |
| 113 | |
| 114 | it('form validates by default without hx-validate attribute', async function() { |
| 115 | mockResponse('POST', '/demo', '<div id="result">Success!</div>'); |
| 116 | createProcessedHTML('<form hx-post="/demo"><input id="i1" required name="test"/><button id="b1" type="submit">Submit</button></form>'); |
| 117 | |
| 118 | find('#b1').click(); |
| 119 | assert.equal(fetchMock.pendingRequests.length, 0); |
| 120 | |
| 121 | find("#i1").value = "foo" |
| 122 | find('#b1').click() |
| 123 | await forRequestWithDelay(); |
| 124 | |
| 125 | assertTextContentIs("#result", "Success!"); |
| 126 | }) |
| 127 | |
| 128 | // it('Button added dynamically still triggers fetch and swaps', async function() { |
| 129 | // // Set up mock response |
| 130 | // fetchMock.mockResponse('/demo', new MockResponse('<div id="d1">Foo</div>')); |
| 131 | // // Add button dynamically |
| 132 | // const playground = find('#test-playground'); |
| 133 | // playground.insertAdjacentHTML('beforeend', `<button hx-action="/demo">Button 1</button>`); |
| 134 | // // Wait for htmx to initialize the new element |
| 135 | // await htmx.forEvent("mx:init", 2000); |
| 136 | // // Click the button |
| 137 | // find('button').click(); |
| 138 | // // Wait for the swap to complete |
| 139 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 140 | // // Verify the swap occurred |
| 141 | // const result = find('#d1'); |
| 142 | // assertExists(result); |
| 143 | // assertEquals("Foo", result.textContent); |
| 144 | // }) |
| 145 | // |
| 146 | // it('Button with hx-target swaps into target element', async function() { |
| 147 | // // Set up mock response |
| 148 | // fetchMock.mockResponse('/demo', new MockResponse('<div id="d1">Foo</div>')); |
| 149 | // // Create test button with target |
| 150 | // await initLiveContent(` |
| 151 | // <button hx-action="/demo" hx-target="#output1">Button 1</button> |
| 152 | // <output id="output1">Bar</output> |
| 153 | // `); |
| 154 | // // Click the button |
| 155 | // find('button').click(); |
| 156 | // // Wait for the swap to complete |
| 157 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 158 | // // Verify the swap occurred in the target |
| 159 | // const result = find('#d1'); |
| 160 | // assertExists(result); |
| 161 | // assertEquals("Foo", result.textContent); |
| 162 | // }) |
| 163 | // |
| 164 | // it('Button with swap afterend swaps after the end of the target', async function() { |
| 165 | // // Set up mock response |
| 166 | // fetchMock.mockResponse('/demo', new MockResponse('<div id="d1">Foo</div>')); |
| 167 | // // Create test button with target |
| 168 | // await initLiveContent(` |
| 169 | // <button id="b1" hx-action="/demo" hx-swap="afterend">Button 1</button>`); |
| 170 | // // Click the button |
| 171 | // find('button').click(); |
| 172 | // // Wait for the swap to complete |
| 173 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 174 | // |
| 175 | // // Verify the swap occurred after the target |
| 176 | // const btn = find('#b1'); |
| 177 | // assertExists(btn); |
| 178 | // assertEquals("Button 1", btn.textContent); |
| 179 | // |
| 180 | // // Verify the swap occurred after the target |
| 181 | // const result = btn.nextElementSibling; |
| 182 | // assertExists(result); |
| 183 | // assertEquals("Foo", result.textContent); |
| 184 | // }) |
| 185 | // |
| 186 | // it('Test attributes can be explicity inherited to children', async function() { |
| 187 | // // Set up mock response |
| 188 | // fetchMock.mockResponse('/demo1', new MockResponse('<div>Foo</div>')); |
| 189 | // fetchMock.mockResponse('/demo2', new MockResponse('<div>Bar</div>')); |
| 190 | // // Create test button with target |
| 191 | // await initLiveContent(` |
| 192 | // <div hx-swap:inherited="beforeend" hx-target:inherited="#output"> |
| 193 | // <button id="b1" hx-action="/demo1">Button 1</button> |
| 194 | // <button id="b2" hx-action="/demo2">Button 2</button> |
| 195 | // </div> |
| 196 | // <div id="output"></div> |
| 197 | // `); |
| 198 | // // Click the button |
| 199 | // find('#b1').click(); |
| 200 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 201 | // |
| 202 | // // Verify the swap occurred after the target |
| 203 | // const output = find('#output'); |
| 204 | // assertExists(output); |
| 205 | // assertEquals("Foo", output.textContent); |
| 206 | // |
| 207 | // find('#b2').click(); |
| 208 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 209 | // |
| 210 | // assertEquals("FooBar", output.textContent); |
| 211 | // }) |
| 212 | // }) |
| 213 | // |
| 214 | // describe('Advanced Triggers', function() { |
| 215 | // afterEach(function() { |
| 216 | // cleanup() |
| 217 | // }) |
| 218 | // |
| 219 | // it('Test multiple events can trigger', async function() { |
| 220 | // // Set up mock response |
| 221 | // fetchMock.mockResponse('/demo', new MockResponse('<div>Foo</div>')); |
| 222 | // // Create test button with target |
| 223 | // await initLiveContent(` |
| 224 | // <button id="b1" hx-action="/demo" hx-target="#output" hx-swap="beforeend" hx-trigger="foo, bar">Button 1</button> |
| 225 | // <output id="output"></output> |
| 226 | // `); |
| 227 | // // Click the button |
| 228 | // let btn = find('#b1'); |
| 229 | // htmx.trigger(btn, 'foo'); |
| 230 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 231 | // |
| 232 | // // Verify the swap occurred after the target |
| 233 | // const output = find('#output'); |
| 234 | // assertExists(output); |
| 235 | // assertEquals("Foo", output.textContent); |
| 236 | // |
| 237 | // htmx.trigger(btn, 'bar'); |
| 238 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 239 | // |
| 240 | // assertEquals("FooFoo", output.textContent); |
| 241 | // }) |
| 242 | // |
| 243 | // it('Test delay can delay a trigger', async function() { |
| 244 | // fetchMock.mockResponse('/demo', new MockResponse('<div>Foo</div>')); |
| 245 | // await initLiveContent(` |
| 246 | // <div id="d1" hx-action="/demo" hx-swap="innerHTML" hx-trigger="foo delay:1s">Div 1</div> |
| 247 | // `); |
| 248 | // |
| 249 | // let div = find('#d1'); |
| 250 | // htmx.trigger(div, 'foo'); |
| 251 | // // should still be Div 1 |
| 252 | // await htmx.timeout(300); |
| 253 | // assertEquals("Div 1", div.textContent); |
| 254 | // |
| 255 | // // delay should trigger eventually |
| 256 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 257 | // assertEquals("Foo", div.textContent); |
| 258 | // }) |
| 259 | // |
| 260 | // it('Test delay debounces a trigger', async function() { |
| 261 | // fetchMock.mockResponse('/demo', new MockResponse('<div>Foo</div>')); |
| 262 | // await initLiveContent(` |
| 263 | // <div id="d1" hx-action="/demo" hx-swap="innerHTML" hx-trigger="foo delay:1s">Div 1</div> |
| 264 | // `); |
| 265 | // |
| 266 | // let div = find('#d1'); |
| 267 | // htmx.trigger(div, 'foo'); |
| 268 | // // should still be Div 1 |
| 269 | // await htmx.timeout(600); |
| 270 | // assertEquals("Div 1", div.textContent); |
| 271 | // |
| 272 | // htmx.trigger(div, 'foo'); |
| 273 | // // should still be Div 1 due to retrigger |
| 274 | // await htmx.timeout(600); |
| 275 | // assertEquals("Div 1", div.textContent); |
| 276 | // |
| 277 | // // delay should trigger eventually |
| 278 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 279 | // assertEquals("Foo", div.textContent); |
| 280 | // }) |
| 281 | // |
| 282 | // it('Test \'every\' polls properly', async function() { |
| 283 | // fetchMock.mockResponse('/demo', new MockResponse('<div>Foo</div>')); |
| 284 | // await initLiveContent(` |
| 285 | // <div id="d1" hx-action="/demo" hx-swap="innerHTML" hx-trigger="every 1s">Div 1</div> |
| 286 | // `); |
| 287 | // |
| 288 | // let div = find('#d1'); |
| 289 | // |
| 290 | // // should still be Div 1 |
| 291 | // await htmx.timeout(600); |
| 292 | // assertEquals("Div 1", div.textContent); |
| 293 | // |
| 294 | // // delay should trigger eventually |
| 295 | // await htmx.forEvent("htmx:after:swap", 2000); |
| 296 | // assertEquals("Foo", div.textContent); |
| 297 | // }) |
| 298 | }) |
| 299 | |
| 300 |