| @@ -0,0 +1,248 @@ |
| 1 | + | // Tests of the loader of the files of a diff (static/js/lazy.js). Run with |
| 2 | + | // node --test 'internal/adapters/web/jstest/*.test.mjs' |
| 3 | + | import assert from 'node:assert/strict'; |
| 4 | + | import { test } from 'node:test'; |
| 5 | + | import { readFileSync } from 'node:fs'; |
| 6 | + | |
| 7 | + | const SRC = readFileSync(new URL('../static/js/lazy.js', import.meta.url), 'utf8'); |
| 8 | + | |
| 9 | + | globalThis.window = globalThis; |
| 10 | + | const saved = { document: globalThis.document, fetch: globalThis.fetch, IO: globalThis.IntersectionObserver }; |
| 11 | + | |
| 12 | + | // A placeholder: an element with a dataset; its link finds it back. |
| 13 | + | function placeholder(src, manual) { |
| 14 | + | const details = { id: 'f1', querySelector: () => null }; |
| 15 | + | const ph = { dataset: manual ? { src, manual: '' } : { src }, setAttribute() {}, removeAttribute() {}, closest: (s) => (s === 'details' ? details : null) }; |
| 16 | + | const link = { closest: (s) => (s === '.dlazy' ? ph : s === '.dlazy[data-manual] > a' && manual ? link : null) }; |
| 17 | + | return { ph, link, details }; |
| 18 | + | } |
| 19 | + | |
| 20 | + | async function setup(phs, io = true, opts = {}) { |
| 21 | + | const observed = []; |
| 22 | + | const urls = []; |
| 23 | + | if (io) globalThis.IntersectionObserver = class { observe(p) { observed.push(p); } unobserve() {} disconnect() {} }; |
| 24 | + | else delete globalThis.IntersectionObserver; |
| 25 | + | const reply = opts.reply || { status: 200, ok: true, body: '' }; |
| 26 | + | globalThis.fetch = async (u) => { urls.push(u); return { status: reply.status, ok: reply.ok, text: async () => reply.body }; }; |
| 27 | + | globalThis.document = { |
| 28 | + | readyState: 'complete', |
| 29 | + | querySelector: (s) => (s === '#diffs' ? { dataset: {} } : null), |
| 30 | + | querySelectorAll: (s) => (s.includes(':not([data-manual])') ? phs.filter((p) => !('manual' in p.dataset)) : phs), |
| 31 | + | getElementById: opts.getElementById || (() => null), |
| 32 | + | createElement: () => ({ content: { querySelectorAll: () => opts.files || [] } }), |
| 33 | + | addEventListener() {}, |
| 34 | + | }; |
| 35 | + | delete globalThis.ermiteLazy; |
| 36 | + | (0, eval)(SRC); |
| 37 | + | return { observed, urls }; |
| 38 | + | } |
| 39 | + | const settle = () => new Promise((r) => setTimeout(r, 0)); |
| 40 | + | const restore = () => { |
| 41 | + | delete globalThis.ermiteLazy; |
| 42 | + | for (const [k, v] of [['document', saved.document], ['fetch', saved.fetch], ['IntersectionObserver', saved.IO]]) { |
| 43 | + | if (v === undefined) delete globalThis[k]; else globalThis[k] = v; |
| 44 | + | } |
| 45 | + | }; |
| 46 | + | |
| 47 | + | test('manual placeholders are not observed', async () => { |
| 48 | + | const a = placeholder('/r/commit/abc/files?one=1&from=0', true); |
| 49 | + | const b = placeholder('/r/merge-requests/1/files?from=25', false); |
| 50 | + | try { |
| 51 | + | const { observed, urls } = await setup([a.ph, b.ph]); |
| 52 | + | assert.deepEqual(observed, [b.ph]); |
| 53 | + | assert.deepEqual(urls, []); |
| 54 | + | } finally { await settle(); restore(); } |
| 55 | + | }); |
| 56 | + | |
| 57 | + | test('without IntersectionObserver only the automatic placeholders load', async () => { |
| 58 | + | const a = placeholder('/m?one=1&from=0', true); |
| 59 | + | const b = placeholder('/w?from=25', false); |
| 60 | + | try { |
| 61 | + | const { urls } = await setup([a.ph, b.ph], false); |
| 62 | + | assert.deepEqual(urls, ['/w?from=25']); |
| 63 | + | } finally { await settle(); restore(); } |
| 64 | + | }); |
| 65 | + | |
| 66 | + | test('a click on "load diff" loads that one file and does not navigate', async () => { |
| 67 | + | const a = placeholder('/r/commit/abc/files?one=1&from=3', true); |
| 68 | + | try { |
| 69 | + | const { urls } = await setup([a.ph]); |
| 70 | + | let prevented = false; |
| 71 | + | const used = window.ermiteLazy.onClick({ target: a.link, button: 0, preventDefault() { prevented = true; } }); |
| 72 | + | assert.equal(used, true); |
| 73 | + | assert.equal(prevented, true); |
| 74 | + | assert.deepEqual(urls, ['/r/commit/abc/files?one=1&from=3']); |
| 75 | + | } finally { await settle(); restore(); } |
| 76 | + | }); |
| 77 | + | |
| 78 | + | test('Enter and Space on the link load it; other keys and modified clicks do not', async () => { |
| 79 | + | const a = placeholder('/f?one=1&from=1', true); |
| 80 | + | try { |
| 81 | + | const { urls } = await setup([a.ph]); |
| 82 | + | const key = (k, extra = {}) => window.ermiteLazy.onKey({ target: a.link, key: k, preventDefault() {}, ...extra }); |
| 83 | + | assert.equal(key('a'), false); |
| 84 | + | assert.equal(key('Enter', { ctrlKey: true }), false); |
| 85 | + | assert.equal(window.ermiteLazy.onClick({ target: a.link, button: 0, ctrlKey: true, preventDefault() {} }), false); |
| 86 | + | assert.equal(urls.length, 0); |
| 87 | + | assert.equal(key('Enter'), true); |
| 88 | + | assert.equal(key(' '), true); |
| 89 | + | assert.equal(urls.length, 1, 'a file is fetched once'); |
| 90 | + | } finally { await settle(); restore(); } |
| 91 | + | }); |
| 92 | + | |
| 93 | + | test('a manual placeholder without an address is a plain link', async () => { |
| 94 | + | const a = placeholder('', true); |
| 95 | + | a.ph.dataset.src = ''; |
| 96 | + | try { |
| 97 | + | await setup([a.ph]); |
| 98 | + | assert.equal(window.ermiteLazy.onClick({ target: a.link, button: 0, preventDefault() { assert.fail(); } }), false); |
| 99 | + | } finally { await settle(); restore(); } |
| 100 | + | }); |
| 101 | + | |
| 102 | + | test('onSwap runs after a window came in', async () => { |
| 103 | + | const b = placeholder('/w?from=25', false); |
| 104 | + | try { |
| 105 | + | await setup([b.ph]); |
| 106 | + | let n = 0; |
| 107 | + | window.ermiteLazy.onSwap(() => n++); |
| 108 | + | await window.ermiteLazy.load(b.ph); |
| 109 | + | assert.equal(n, 1); |
| 110 | + | } finally { await settle(); restore(); } |
| 111 | + | }); |
| 112 | + | |
| 113 | + | test('reset drops the response of a load in flight and lets the same window load again', async () => { |
| 114 | + | const b = placeholder('/w?from=25', false); |
| 115 | + | try { |
| 116 | + | await setup([b.ph]); |
| 117 | + | let release; |
| 118 | + | const gate = new Promise((r) => { release = r; }); |
| 119 | + | let fetches = 0; |
| 120 | + | globalThis.fetch = async () => { fetches++; await gate; return { status: 200, ok: true, text: async () => '' }; }; |
| 121 | + | let n = 0; |
| 122 | + | window.ermiteLazy.onSwap(() => n++); |
| 123 | + | const first = window.ermiteLazy.load(b.ph); |
| 124 | + | window.ermiteLazy.reset(); |
| 125 | + | release(); |
| 126 | + | await first; |
| 127 | + | assert.equal(n, 0, 'the old generation does not swap'); |
| 128 | + | await window.ermiteLazy.load(b.ph); |
| 129 | + | assert.equal(fetches, 2); |
| 130 | + | assert.equal(n, 1); |
| 131 | + | } finally { await settle(); restore(); } |
| 132 | + | }); |
| 133 | + | |
| 134 | + | test('reset clears the stale state; watch disconnects the previous observer', async () => { |
| 135 | + | const b = placeholder('/w?from=25', false); |
| 136 | + | try { |
| 137 | + | await setup([b.ph]); |
| 138 | + | const made = []; |
| 139 | + | globalThis.IntersectionObserver = class { |
| 140 | + | constructor() { this.off = false; made.push(this); } |
| 141 | + | observe() {} unobserve() {} disconnect() { this.off = true; } |
| 142 | + | }; |
| 143 | + | globalThis.fetch = async () => ({ status: 409, ok: false, text: async () => 'moved' }); |
| 144 | + | await window.ermiteLazy.load(b.ph); |
| 145 | + | const urls = []; |
| 146 | + | globalThis.fetch = async (u) => { urls.push(u); return { status: 200, ok: true, text: async () => '' }; }; |
| 147 | + | await window.ermiteLazy.load(b.ph); |
| 148 | + | assert.deepEqual(urls, [], 'stale: nothing loads'); |
| 149 | + | window.ermiteLazy.watch(); |
| 150 | + | window.ermiteLazy.watch(); |
| 151 | + | assert.equal(made.length, 2); |
| 152 | + | assert.ok(made[0].off && !made[1].off); |
| 153 | + | window.ermiteLazy.reset(); |
| 154 | + | assert.ok(made[1].off); |
| 155 | + | await window.ermiteLazy.load(b.ph); |
| 156 | + | assert.equal(urls.length, 1); |
| 157 | + | } finally { await settle(); restore(); } |
| 158 | + | }); |
| 159 | + | |
| 160 | + | |
| 161 | + | // A file of the page (old) or of a fragment (new), with the queries swap makes. |
| 162 | + | function fileEl(id, { lazy = false, manual = false, open = false } = {}) { |
| 163 | + | const f = { id, open, queried: [] }; |
| 164 | + | f.querySelector = (sel) => { |
| 165 | + | if (sel === '.dlazy[data-manual]') return manual ? {} : null; |
| 166 | + | if (sel === '.dlazy') return lazy || manual ? {} : null; |
| 167 | + | if (sel === '.ln a') return f.line || null; |
| 168 | + | return null; |
| 169 | + | }; |
| 170 | + | f.querySelectorAll = () => []; |
| 171 | + | return f; |
| 172 | + | } |
| 173 | + | |
| 174 | + | test('a loaded manual file opens and takes the focus without scrolling', async () => { |
| 175 | + | const a = placeholder('/f?one=1&from=0', true); |
| 176 | + | const old = fileEl('f1', { manual: true }); |
| 177 | + | const fresh = fileEl('f1'); |
| 178 | + | const focused = []; |
| 179 | + | fresh.line = { focus: (o) => focused.push(o) }; |
| 180 | + | let current = old; |
| 181 | + | old.replaceWith = (n) => { current = n; }; |
| 182 | + | try { |
| 183 | + | await setup([a.ph], true, { reply: { status: 200, ok: true, body: '<x>' }, files: [fresh], getElementById: () => current }); |
| 184 | + | window.ermiteLazy.onClick({ target: a.link, button: 0, preventDefault() {} }); |
| 185 | + | await settle(); await settle(); |
| 186 | + | assert.equal(current, fresh); |
| 187 | + | assert.equal(fresh.open, true); |
| 188 | + | assert.deepEqual(focused, [{ preventScroll: true }]); |
| 189 | + | } finally { await settle(); restore(); } |
| 190 | + | }); |
| 191 | + | |
| 192 | + | test('a window that still holds a collapsed file keeps it closed', async () => { |
| 193 | + | const w = placeholder('/w?from=25', false); |
| 194 | + | const old = fileEl('f26', { lazy: true, open: true }); |
| 195 | + | const fresh = fileEl('f26', { manual: true, open: true }); |
| 196 | + | let replaced = null; |
| 197 | + | old.replaceWith = (n) => { replaced = n; }; |
| 198 | + | try { |
| 199 | + | await setup([w.ph], true, { reply: { status: 200, ok: true, body: '<x>' }, files: [fresh], getElementById: () => old }); |
| 200 | + | await window.ermiteLazy.load(w.ph); |
| 201 | + | assert.equal(replaced, fresh); |
| 202 | + | assert.equal(fresh.open, false); |
| 203 | + | } finally { await settle(); restore(); } |
| 204 | + | }); |
| 205 | + | |
| 206 | + | test('a 409 makes the page stale: no later load fetches', async () => { |
| 207 | + | const a = placeholder('/f?one=1&from=0', true); |
| 208 | + | const b = placeholder('/f?one=1&from=1', true); |
| 209 | + | try { |
| 210 | + | const { urls } = await setup([a.ph, b.ph], true, { reply: { status: 409, ok: false, body: '<p>moved</p>' } }); |
| 211 | + | await window.ermiteLazy.load(a.ph); |
| 212 | + | assert.equal(a.ph.innerHTML, '<p>moved</p>'); |
| 213 | + | assert.equal(urls.length, 1); |
| 214 | + | await window.ermiteLazy.load(b.ph); |
| 215 | + | assert.equal(urls.length, 1, 'stale: nothing is fetched'); |
| 216 | + | } finally { await settle(); restore(); } |
| 217 | + | }); |
| 218 | + | |
| 219 | + | test('a failed load clears the loading mark and can be retried', async () => { |
| 220 | + | const a = placeholder('/f?one=1&from=0', true); |
| 221 | + | const marks = new Set(); |
| 222 | + | a.ph.setAttribute = (n) => marks.add(n); |
| 223 | + | a.ph.removeAttribute = (n) => marks.delete(n); |
| 224 | + | try { |
| 225 | + | const { urls } = await setup([a.ph], true, { reply: { status: 500, ok: false, body: 'boom' } }); |
| 226 | + | const p = window.ermiteLazy.load(a.ph); |
| 227 | + | assert.ok(marks.has('data-loading')); |
| 228 | + | await p; |
| 229 | + | assert.equal(marks.has('data-loading'), false); |
| 230 | + | await window.ermiteLazy.load(a.ph); |
| 231 | + | assert.equal(urls.length, 2, 'a retry fetches again'); |
| 232 | + | } finally { await settle(); restore(); } |
| 233 | + | }); |
| 234 | + | |
| 235 | + | test('Space is prevented on the link; a held key is ignored', async () => { |
| 236 | + | const a = placeholder('/f?one=1&from=1', true); |
| 237 | + | try { |
| 238 | + | const { urls } = await setup([a.ph]); |
| 239 | + | let prevented = 0; |
| 240 | + | const ev = (extra) => ({ target: a.link, key: ' ', preventDefault() { prevented++; }, ...extra }); |
| 241 | + | assert.equal(window.ermiteLazy.onKey(ev({ repeat: true })), false); |
| 242 | + | assert.equal(prevented, 0); |
| 243 | + | assert.equal(urls.length, 0); |
| 244 | + | assert.equal(window.ermiteLazy.onKey(ev({})), true); |
| 245 | + | assert.equal(prevented, 1, 'Space does not scroll the page'); |
| 246 | + | assert.equal(urls.length, 1); |
| 247 | + | } finally { await settle(); restore(); } |
| 248 | + | }); |