diff --git a/public/repeater-info.js b/public/repeater-info.js index 0cef0225..8421f889 100644 --- a/public/repeater-info.js +++ b/public/repeater-info.js @@ -135,9 +135,14 @@ var form = el.querySelector('[data-ri-form]'); if (form) drafts[slot] = { pk: pk, values: rawForm(form) }; } + // shown is the mode on screen, or '' while nothing is (the view before + // the GET answered). + var shown = ''; function show(mode, values) { var user = auth.user(); if (mode !== 'edit') delete drafts[slot]; + if (mode === 'view' && !info) { shown = ''; el.innerHTML = ''; el.hidden = true; return; } + shown = mode; el.innerHTML = '

Repeater info

' + (mode === 'edit' ? formHtml(values || info) : mode === 'confirm' ? confirmHtml() : viewHtml(info, user)); if (mode === 'edit' && !values) keep(); @@ -182,12 +187,17 @@ return Promise.resolve(auth.ready()).then(function () { if (!auth.isEnabled()) return; bind(); + // An open draft comes back at once, in the same task as the re-render: + // waiting for the GET would empty the section for a round trip. + var d = drafts[slot]; + if (d && d.pk === pk) { show('edit', d.values); el.hidden = false; } return auth.request('GET', base + '/info').then(function (r) { - if (!r.ok) return; + // A section the node page already replaced must not touch the draft + // its successor owns. + if (!r.ok || el.isConnected === false) return; + if (info) return; // a save on the early form already brought newer data info = r.data; - var d = drafts[slot]; - if (d && d.pk === pk && info.canEdit) show('edit', d.values); - else show('view'); + if (shown !== 'edit' || !info.canEdit) show('view'); el.hidden = false; var hash = String(window.location.hash || ''); if (hash.indexOf('section=node-repeater-info') !== -1 && el.scrollIntoView) el.scrollIntoView(); diff --git a/tests/e2e/test-user-management-e2e.js b/tests/e2e/test-user-management-e2e.js index bd8462f9..0cd47ef1 100644 --- a/tests/e2e/test-user-management-e2e.js +++ b/tests/e2e/test-user-management-e2e.js @@ -742,7 +742,7 @@ async function until(fn, label) { await userA.click(row); const pane = '#node-repeater-info-pane'; await userA.waitForSelector(pane + ':not([hidden]) .repeater-info-list'); - const t = (await userA.innerText(pane)).replace(/s+/g, ' '); + const t = (await userA.innerText(pane)).replace(/\s+/g, ' '); assert(t.indexOf('25 m') !== -1, 'pane height missing: ' + t); }); diff --git a/tests/unit/test-repeater-info.js b/tests/unit/test-repeater-info.js index 39c0f34c..9feb6b3f 100644 --- a/tests/unit/test-repeater-info.js +++ b/tests/unit/test-repeater-info.js @@ -264,6 +264,93 @@ test('no draft after Cancel, a save, a hashchange, another repeater or lost edit l.auth.request = () => Promise.resolve({ ok: true, status: 200, data: INFO({ canEdit: false }) }); assert(!(await remountShowsForm(l)), 'draft shown without edit rights'); }); +// A re-mount with an open draft shows the form at once: waiting for the GET +// left the section empty for a round trip, the form vanished from under the +// user and the E2E read a missing notes field. +function remountWithPendingGet() { + const opts = { info: INFO({ canEdit: true }), form: { heightM: '25', gainDbi: '0', status: '', power: '', notes: 'typed' } }; + const l = load(opts); + const a = mountFlow(opts, l); + return l.R.mount(a.el, { public_key: PK, role: 'repeater' }).then(() => { + a.click('data-ri-edit'); + a.el.handlers.input({}); + const pend = []; + l.auth.request = (method) => new Promise((ok, fail) => { pend.push({ method, ok, fail }); }); + const b = mountFlow(opts, l); + const done = l.R.mount(b.el, { public_key: PK, role: 'repeater' }); + return tick().then(() => ({ b, done, pend, answer: (res) => { pend[0].ok(res); return done; } })); + }); +} +test('a re-mount shows the draft form before the GET answers', async () => { + const { b, answer } = await remountWithPendingGet(); + assert(b.el.innerHTML.includes('data-ri-form') && b.el.innerHTML.includes('typed'), 'no form while the GET is out: ' + b.el.innerHTML); + assert.strictEqual(b.el.hidden, false); + b.el.innerHTML += ''; + await answer({ ok: true, status: 200, data: INFO({ canEdit: true }) }); + assert(b.el.innerHTML.includes(''), 'the GET re-rendered the open form'); +}); +test('the early draft form gives way to the view when the GET says no edit rights', async () => { + const { b, answer } = await remountWithPendingGet(); + await answer({ ok: true, status: 200, data: INFO({ canEdit: false }) }); + assert(!b.el.innerHTML.includes('data-ri-form'), b.el.innerHTML); + assert(b.el.innerHTML.includes('No details yet.'), b.el.innerHTML); +}); +test('Cancel on the early draft form before the GET answers, then the view', async () => { + const { b, answer } = await remountWithPendingGet(); + b.click('data-ri-cancel'); + assert(!b.el.innerHTML.includes('data-ri-form'), b.el.innerHTML); + await answer({ ok: true, status: 200, data: INFO({ canEdit: true, heightM: 9, updatedAt: 'x' }) }); + assert(b.el.innerHTML.includes('9 m'), b.el.innerHTML); +}); +test('a failed GET hides the section unless the draft form is open', async () => { + let r = await remountWithPendingGet(); + await r.answer({ ok: false, status: 500, data: {} }); + assert(r.b.el.innerHTML.includes('data-ri-form') && r.b.el.hidden === false, 'open form dropped on a failed GET'); + r = await remountWithPendingGet(); + r.b.click('data-ri-cancel'); + await r.answer({ ok: false, status: 500, data: {} }); + assert.strictEqual(r.b.el.hidden, true); +}); +test('a save on the early form is not overwritten by the older GET answer', async () => { + const { b, pend, answer } = await remountWithPendingGet(); + b.click('data-ri-save'); + assert.strictEqual(pend[1].method, 'PUT'); + pend[1].ok({ ok: true, status: 200, data: INFO({ canEdit: true, heightM: 30, updatedAt: 'x' }) }); + await tick(); + await answer({ ok: true, status: 200, data: INFO({ canEdit: true, heightM: 5, updatedAt: 'x' }) }); + assert(b.el.innerHTML.includes('30 m') && !b.el.innerHTML.includes('5 m'), b.el.innerHTML); +}); +test('a network error on the GET hides the section unless the draft form is open', async () => { + let r = await remountWithPendingGet(); + r.pend[0].fail(new Error('net')); + await r.done; + assert(r.b.el.innerHTML.includes('data-ri-form') && r.b.el.hidden === false, 'open form dropped on a network error'); + r = await remountWithPendingGet(); + r.b.click('data-ri-cancel'); + r.pend[0].fail(new Error('net')); + await r.done; + assert.strictEqual(r.b.el.hidden, true); +}); +test('a late GET of a replaced section leaves the draft of its successor alone', async () => { + const opts = { info: INFO({ canEdit: true }), form: { heightM: '25', gainDbi: '0', status: '', power: '', notes: 'typed' } }; + const l = load(opts); + let answerA; + l.auth.request = () => new Promise((r) => { answerA = r; }); + const a = mountFlow(opts, l); + const doneA = l.R.mount(a.el, { public_key: PK, role: 'repeater' }); + await tick(); + a.el.isConnected = false; + l.auth.request = () => Promise.resolve({ ok: true, status: 200, data: INFO({ canEdit: true }) }); + const b = mountFlow(opts, l); + await l.R.mount(b.el, { public_key: PK, role: 'repeater' }); + b.click('data-ri-edit'); + b.el.handlers.input({}); + answerA({ ok: true, status: 200, data: INFO({ canEdit: true }) }); + await doneA; + const c = mountFlow(opts, l); + await l.R.mount(c.el, { public_key: PK, role: 'repeater' }); + assert(c.el.innerHTML.includes('data-ri-form'), 'the stale GET dropped the draft: ' + c.el.innerHTML); +}); test('anonymous visitor sees one login hint, a logged-in user does not', () => { const anon = load({ user: null }).R._test.viewHtml(INFO(), null); assert(anon.includes('Log in to claim or edit this repeater.'));