Files
wekan/tests/spacebarsFunctionArgs.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

52 lines
2.3 KiB
JavaScript

'use strict';
// Spacebars calls a function it finds in a data context when a template
// passes it on: `+child(done=done)` inside a template whose own data has a
// function `done` calls done() with nothing and hands the child its result.
// That kept the Cloud Panel's domain form from ever rendering (done(undefined)
// threw on .then) and made Budget and billing's invoice reload instead of
// passing its reload on. A template that passes a function on takes it from a
// helper of its own, which Spacebars calls once and whose return value it
// passes as it is. Run: node tests/spacebarsFunctionArgs.test.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const read = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
// The function-valued arguments these pages hand from template to template.
const FUNCTIONS = ['done', 'reload', 'close', 'setPrinted', 'setPeriod'];
const PAGES = [
['client/components/main/billing.jade', 'client/components/main/billing.js'],
['client/components/cloud/cloudPanel.jade', 'client/components/cloud/cloudPanel.js'],
['client/components/main/workload.jade', 'client/components/main/workload.js'],
];
function helpersOf(js, template) {
const at = js.indexOf(`Template.${template}.helpers({`);
if (at === -1) return new Set();
const body = js.slice(at, js.indexOf('\n});', at));
return new Set([...body.matchAll(/^ {2}(\w+)(?:\(|:)/gm)].map(m => m[1]));
}
const problems = [];
for (const [jadeFile, jsFile] of PAGES) {
const jade = read(jadeFile);
const js = read(jsFile);
for (const block of jade.split(/^template\(name="/m).slice(1)) {
const name = block.slice(0, block.indexOf('"'));
const helpers = helpersOf(js, name);
for (const m of block.matchAll(/\+(\w+)\(([^)]*)\)/g)) {
for (const arg of m[2].split(/\s+/)) {
const [key, value] = arg.split('=');
if (FUNCTIONS.includes(key) && value && !helpers.has(value)) problems.push(`${jadeFile}: ${name} passes ${arg} to ${m[1]}, not from a helper`);
}
}
}
}
assert.deepEqual(problems, [], 'a function handed on comes from a helper of the template that hands it on');
// Negative: the old shape is found.
assert.ok(!helpersOf('Template.x.helpers({\n printed() {}\n});', 'x').has('reload'));
console.log(`spacebarsFunctionArgs: ${PAGES.length} pages checked`);