Files
wekan/tests/templateDataInPromise.test.cjs
T
Lauri Ojansivu 1032b472f3 Budget and billing, Workload and the Cloud Panel: saves show, forms render, PDFs print.
Found by running their Chromium specs through build.sh:

- Budget and billing did not refresh after a save: the board pane read the
  current template data inside a promise, where there is none. It uses its
  own instance's data now, and a save refreshes only the board's detail and
  its row, without the spinner that took the pane down and without the
  billingBoards call that ran into its rate limit.
- Workload's capacity form lost its "saved" note: the refresh after a save
  showed the spinner, which replaces the form. The spinner is now only for the
  first load.
- The Cloud Panel's forms did not render: a function passed to an inclusion as
  done=done is called by Spacebars, so they received its result. Errors and
  the domain being edited are module state now.
- The checkboxes of the three pages were hidden by the app-wide form styles;
  each page shows its own again.
- An invoice PDF failed in the bundle with "Cannot find module
  #standard-fonts/Helvetica": pdfkit is given the bundled font.

Tests: templateDataInPromise, spacebarsFunctionArgs and nativeCheckboxesShown
find each shape anywhere in the client, invoiceFiles requires the font in
every PDFDocument, and workload pins the spinner to the first load.

Thanks to xet7 !
2026-11-22 07:20:48 +02:00

36 lines
1.9 KiB
JavaScript

'use strict';
// Template.currentData() is only the current template's data while Blaze is
// running a helper, an event handler or an autorun. Inside a promise's then()
// there is no current template: it returns null, and whatever the code meant
// to read - Budget and billing's reload() - is silently skipped. That left
// the Budget and billing page unrefreshed after every save. Code that needs
// the data later reads the instance's own `this.data`, or takes what it
// needs before the promise. Run: node tests/templateDataInPromise.test.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const ROOT = path.join(__dirname, '..');
const walk = dir => fs.readdirSync(path.join(ROOT, dir), { withFileTypes: true }).flatMap(e => {
const rel = `${dir}/${e.name}`;
if (e.isDirectory()) return walk(rel);
return e.name.endsWith('.js') ? [rel] : [];
});
// A then() callback whose own body, up to its closing brace, reads it.
// One level of braces inside it - `const { reload } =` - is still its body.
const SHAPE = /\.then\(\s*(?:\([^)]*\)|\w+)\s*=>\s*\{(?:[^{}]|\{[^{}]*\})*?Template\.currentData\(\)/;
const found = walk('client').filter(file => SHAPE.test(fs.readFileSync(path.join(ROOT, file), 'utf8')));
assert.deepEqual(found, [], 'Template.currentData() inside a then() callback is null there');
// Negative: the shape is found, and reading the data before the promise is not it.
assert.ok(SHAPE.test("p.then(result => {\n const { reload } = Template.currentData() || {};\n})"));
assert.ok(!SHAPE.test("const { reload } = Template.currentData();\np.then(() => reload())"));
assert.match(fs.readFileSync(path.join(ROOT, 'client/components/main/billing.js'), 'utf8'),
/this\.done = promise => promise\.then\(result => \{[\s\S]{0,300}const \{ reload \} = this\.data \|\| \{\};/);
console.log(`templateDataInPromise: ${found.length} found in client/`);