| 1 |
if (typeof installFetchMock !== 'undefined') { |
| 2 |
installFetchMock() |
| 3 |
} |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
let linkNavPreventer = (e) => { |
| 10 |
let anchor = e.target.closest('a'); |
| 11 |
|
| 12 |
if (!anchor || !anchor.href) return; |
| 13 |
if (e.defaultPrevented) return; |
| 14 |
if (e.button !== 0) return; |
| 15 |
if (e.ctrlKey || e.metaKey || e.shiftKey) return; |
| 16 |
if (anchor.download) return; |
| 17 |
if (anchor.href.startsWith('javascript:')) return; |
| 18 |
if (anchor.href.startsWith('#')) return; |
| 19 |
|
| 20 |
e.preventDefault(); |
| 21 |
console.warn('Navigation prevented:', anchor.href, new Error().stack); |
| 22 |
}; |
| 23 |
|
| 24 |
let submitPreventer = (e) => { |
| 25 |
if (e.defaultPrevented) return; |
| 26 |
e.preventDefault(); |
| 27 |
console.warn('Form submission prevented:', e, new Error().stack); |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
let testDebugging = false; |
| 36 |
|
| 37 |
const savedUrl = window.location.href; |
| 38 |
|
| 39 |
function setupTest(test) { |
| 40 |
if (test) { |
| 41 |
console.log("RUNNING TEST: ", test.title); |
| 42 |
} |
| 43 |
if (!playground().hasAttribute("data-is-navsafe")) { |
| 44 |
|
| 45 |
playground().addEventListener('click', linkNavPreventer); |
| 46 |
|
| 47 |
playground().addEventListener('submit', submitPreventer); |
| 48 |
playground().setAttribute("data-is-navsafe", 'true') |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function cleanupTest() { |
| 53 |
let pg = playground() |
| 54 |
if (pg && !testDebugging) { |
| 55 |
pg.innerHTML = '' |
| 56 |
} |
| 57 |
testDebugging = false; |
| 58 |
if (typeof fetchMock !== 'undefined' && fetchMock.reset) { |
| 59 |
|
| 60 |
if (fetchMock.pendingRequests && fetchMock.pendingRequests.length > 0) { |
| 61 |
console.warn(`WARNING: Test is leaving ${fetchMock.pendingRequests.length} request(s) in flight. Tests should wait for all requests to complete.`); |
| 62 |
} |
| 63 |
fetchMock.reset() |
| 64 |
} |
| 65 |
history.replaceState(null, '', savedUrl); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
function backupExtensions() { |
| 73 |
return { |
| 74 |
extMethods: new Map(htmx.__extMethods), |
| 75 |
registeredExt: new Set(htmx.__registeredExt), |
| 76 |
approvedExt: htmx.__approvedExt |
| 77 |
}; |
| 78 |
} |
| 79 |
|
| 80 |
function restoreExtensions(backup) { |
| 81 |
htmx.__extMethods = backup.extMethods; |
| 82 |
htmx.__registeredExt = backup.registeredExt; |
| 83 |
htmx.__approvedExt = backup.approvedExt; |
| 84 |
} |
| 85 |
|
| 86 |
function clearExtensions() { |
| 87 |
htmx.__extMethods.clear(); |
| 88 |
htmx.__registeredExt.clear(); |
| 89 |
} |
| 90 |
|
| 91 |
function debug(test) { |
| 92 |
test.timeout(0); |
| 93 |
testDebugging = true; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
function createProcessedHTML(innerHTML) { |
| 103 |
let pg = playground(); |
| 104 |
if (pg) { |
| 105 |
pg.innerHTML = innerHTML |
| 106 |
htmx.process(pg) |
| 107 |
} |
| 108 |
return pg.firstElementChild |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
function createHTMLNoProcessing(html) { |
| 113 |
const div = document.createElement('div'); |
| 114 |
div.innerHTML = html; |
| 115 |
const elt = div.firstElementChild; |
| 116 |
playground().appendChild(elt); |
| 117 |
return elt |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
function createDisconnectedHTML(html) { |
| 122 |
const div = document.createElement('div'); |
| 123 |
div.innerHTML = html; |
| 124 |
const elt = div.firstElementChild; |
| 125 |
return elt |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
function mockResponse(action, pattern, response, options = {}) { |
| 133 |
fetchMock.mockResponse(action, pattern, response, options); |
| 134 |
} |
| 135 |
|
| 136 |
function mockFailure(action, pattern, message = 'Network failure') { |
| 137 |
fetchMock.mockFailure(action, pattern, message); |
| 138 |
} |
| 139 |
|
| 140 |
function mockSequentialResponses(method, urlPattern, response, options = {}) { |
| 141 |
return fetchMock.mockSequentialResponses(method, urlPattern, response, options); |
| 142 |
} |
| 143 |
|
| 144 |
function mockStreamResponse(url) { |
| 145 |
const controllers = []; |
| 146 |
const enc = new TextEncoder(); |
| 147 |
|
| 148 |
|
| 149 |
fetchMock.mockResponse('GET', url, () => { |
| 150 |
let ctrl; |
| 151 |
const body = new ReadableStream({ start(c) { ctrl = c; controllers.push(c); } }); |
| 152 |
const response = new MockResponse(body, { |
| 153 |
headers: { 'Content-Type': 'text/event-stream' } |
| 154 |
}); |
| 155 |
response.body = body; |
| 156 |
return response; |
| 157 |
}); |
| 158 |
|
| 159 |
return { |
| 160 |
send(data, event, id) { |
| 161 |
|
| 162 |
const ctrl = controllers[controllers.length - 1]; |
| 163 |
if (!ctrl) return; |
| 164 |
let msg = (event ? `event: ${event}\n` : '') + (id ? `id: ${id}\n` : '') + `data: ${data}\n\n`; |
| 165 |
ctrl.enqueue(enc.encode(msg)); |
| 166 |
}, |
| 167 |
sendRaw(raw) { |
| 168 |
const ctrl = controllers[controllers.length - 1]; |
| 169 |
if (ctrl) ctrl.enqueue(enc.encode(raw)); |
| 170 |
}, |
| 171 |
close: () => { |
| 172 |
|
| 173 |
const ctrl = controllers[controllers.length - 1]; |
| 174 |
if (ctrl) ctrl.close(); |
| 175 |
}, |
| 176 |
error: (e) => { |
| 177 |
const ctrl = controllers[controllers.length - 1]; |
| 178 |
if (ctrl) ctrl.error(e); |
| 179 |
} |
| 180 |
}; |
| 181 |
} |
| 182 |
|
| 183 |
function lastFetch() { |
| 184 |
let lastCall = fetchMock.getLastCall(); |
| 185 |
assert.isNotNull(lastCall, "No fetch call was made!") |
| 186 |
return lastCall; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
function waitForEvent(eventName, timeout = 200) { |
| 194 |
let ms = testDebugging ? 0 : timeout; |
| 195 |
return new Promise(resolve => { |
| 196 |
let h = evt => { clearTimeout(id); resolve(evt); }; |
| 197 |
let id = ms && setTimeout(() => { |
| 198 |
document.removeEventListener(eventName, h); |
| 199 |
resolve(null); |
| 200 |
}, ms); |
| 201 |
document.addEventListener(eventName, h, { once: true }); |
| 202 |
}); |
| 203 |
} |
| 204 |
|
| 205 |
function forRequest(timeout = 200) { |
| 206 |
return waitForEvent("htmx:finally:request", timeout); |
| 207 |
} |
| 208 |
|
| 209 |
function forRequestWithDelay(timeout = 200) { |
| 210 |
return htmx.timeout(50).then(() => forRequest(timeout)); |
| 211 |
} |
| 212 |
|
| 213 |
function playground() { |
| 214 |
return htmx.find("#test-playground"); |
| 215 |
} |
| 216 |
|
| 217 |
function find(selector) { |
| 218 |
return htmx.find(playground(), selector) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
function assertPropertyIs(css, property, content) { |
| 226 |
let elt = playground().querySelector(css); |
| 227 |
if (!elt) { |
| 228 |
assert.fail("Could not find element with css '" + css + "' in :\n\n" + playground().innerHTML + "\n\n") |
| 229 |
} |
| 230 |
assert.equal(elt[property], content) |
| 231 |
} |
| 232 |
|
| 233 |
function assertTextContentIs(css, content) { |
| 234 |
assertPropertyIs(css, 'textContent', content) |
| 235 |
} |