Files
wekan/tests/dateTemplates.test.cjs
T
Lauri Ojansivu 9a73bf9173 Describe each card date once for its card and minicard badge.
Received, Start, Due and End were each written twice in cardDate.js, once
for the opened card (card*Date) and once for the minicard (minicard*Date):
the same field, the same colouring, the same title and the same editor,
eight copies of about 40 lines. CARD_DATE_KINDS describes each date once
and both templates are built from it; only the due date counts down, as
before. cardDate.js goes from 596 to 410 lines.

The tests that matched each template's own helpers block read the
CARD_DATE_KINDS entry instead, and still fail if Received, Start or End
gains the countdown.

Thanks to xet7 !
2026-10-17 00:50:41 +03:00

173 lines
9.2 KiB
JavaScript

'use strict';
// The date markup is written once.
// Run: node tests/dateTemplates.test.cjs
//
// Two shapes were copied across three files: the "edit a date" form (eight
// identical copies - the card's Received, Start, Due and End, a vote's end, a
// poker's end, a custom field's date, and a ninth `datepicker` template nothing
// included) and the date BADGE (fourteen copies - the same four dates on the
// card and again on the minicard, a generic one, the two end dates, and three
// custom-field ones).
//
// The JavaScript was already shared: client/lib/datepicker.js has the state and
// the handlers, and each popup differs only in what it stores. Only the markup
// was duplicated, so a change to the form meant eight edits and a change to the
// badge meant fourteen, with nothing to say so.
//
// Each shape is one template now, and it takes what it draws as ARGUMENTS -
// because a helper is looked up on the template it is written in, not on the
// one including it. That is what lets the markup be shared while every popup
// keeps its own state, its own click and its own name.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const ROOT = path.join(__dirname, '..');
const read = rel => fs.readFileSync(path.join(ROOT, rel), 'utf8');
const cardDate = read('client/components/cards/cardDate.jade');
const cardDetails = read('client/components/cards/cardDetails.jade');
const customFields = read('client/components/cards/cardCustomFields.jade');
const datepicker = read('client/components/forms/datepicker.jade');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('dateTemplates:');
test('the edit form is written once', () => {
assert.ok(/template\(name="editDateForm"\)/.test(datepicker), 'one template');
const copies = (cardDate + customFields + datepicker)
// The field is now the shared selected-calendar input, with native ISO
// state behind it. Keep the guard against duplicating date forms.
.match(/\+calendarDateInput\(inputId="date"/g) || [];
assert.strictEqual(copies.length, 1, 'and the date field exists in exactly one place');
// All eight popups include it, and pass the three things it draws.
for (const popup of ['editCardReceivedDatePopup', 'editCardStartDatePopup',
'editCardDueDatePopup', 'editCardEndDatePopup', 'editVoteEndDatePopup',
'editPokerEndDatePopup']) {
const at = cardDate.indexOf(`template(name="${popup}")`);
assert.ok(at !== -1, `${popup} still exists`);
assert.ok(/\+editDateForm\(showDate=showDate showTime=showTime error=error datePicker=datePicker\)/
.test(cardDate.slice(at, at + 200)), `${popup} includes it`);
}
assert.ok(/\+editDateForm\(showDate=showDate showTime=showTime error=error datePicker=datePicker customFieldCopyValue=value customFieldControls=true\)/.test(customFields),
'and so does the custom field date popup, with its date-specific controls');
});
test('the badge is written once', () => {
assert.ok(/template\(name="dateBadgeBody"\)/.test(cardDate), 'one template');
const links = cardDate.match(/a\.js-edit-date/g) || [];
assert.strictEqual(links.length, 1, 'and the badge link exists in exactly one place');
const includes = cardDate.match(/\+dateBadgeBody\(/g) || [];
// dateBadge, dateCustomField and cardCustomFieldDate drew it too, but were
// rendered nowhere and were removed, leaving eleven.
assert.strictEqual(includes.length, 11, 'eleven templates draw it');
for (const gone of ['dateBadge', 'dateCustomField', 'cardCustomFieldDate']) {
assert.ok(!cardDate.includes(`template(name="${gone}")`), `${gone} stays removed`);
}
});
test('a custom field date is still not a .card-date (negative)', () => {
// The trap in sharing one badge: some callers never carried
// `card-date`, and folding them into a template that adds it would restyle
// them. `baseClass` is what the caller's own markup carried.
const body = cardDate.slice(cardDate.indexOf('template(name="dateBadgeBody")'),
cardDate.indexOf('template(name="cardReceivedDate")'));
assert.ok(/class="\{\{baseClass\}\} \{\{classes\}\}"/.test(body),
'the base class comes from the caller');
assert.ok(!/a\.js-edit-date\.card-date|a\.card-date/.test(body),
'and is not baked into the shared markup');
for (const plain of ['minicardCustomFieldDate']) {
const at = cardDate.indexOf(`template(name="${plain}")`);
const include = cardDate.slice(at, cardDate.indexOf('\n', cardDate.indexOf('+dateBadgeBody', at)));
assert.ok(!/baseClass=/.test(include), `${plain} passes no base class`);
assert.ok(!/canModifyCard=/.test(include), `${plain} is not a link either, as before`);
}
});
test('a minicard badge keeps its own kind class (negative)', () => {
// `.due-date` and friends are what colour it; they moved into `baseClass`,
// not away.
for (const [tpl, kind] of [['minicardReceivedDate', 'received-date'],
['minicardStartDate', 'start-date'], ['minicardDueDate', 'due-date'],
['minicardEndDate', 'end-date']]) {
const at = cardDate.indexOf(`template(name="${tpl}")`);
const include = cardDate.slice(at, cardDate.indexOf('\n', cardDate.indexOf('+dateBadgeBody', at)));
assert.ok(include.includes(`baseClass="card-date ${kind}"`), `${tpl} keeps ${kind}`);
}
});
test('opened-card date badges receive the edit permission explicitly', () => {
for (const template of [
'cardReceivedDate', 'cardStartDate', 'cardDueDate', 'cardEndDate',
]) {
assert.ok(cardDetails.includes(`+${template}(card=this canModifyCard=canModifyCard)`),
`${template} stays editable for members who can modify the card`);
}
});
test('the ninth copy of the form is gone, with the file that wired it', () => {
// `datepicker` was a template nothing included, and a popup cannot open it
// either - a popup's template name ends in `Popup`.
assert.ok(!/template\(name="datepicker"\)/.test(datepicker), 'the template is gone');
assert.ok(!fs.existsSync(path.join(ROOT, 'client/components/forms/datepicker.js')),
'and so is its module');
const forms = read('client/features/forms.js');
assert.ok(!/datepicker\.js/.test(forms.replace(/\/\/.*/g, '')), 'nothing imports it');
assert.ok(/client\/lib\/datepicker\.js/.test(forms), 'and the note says where the state lives');
});
test('the child form owns the event map and receives each popup state (#6607)', () => {
// Blaze event maps do not cross a child-template boundary. Registering the
// map on each popup made this included form look right but made Save, Delete
// and validation inert. The form must own the map and receive datePicker.
const js = read('client/components/cards/cardDate.js');
const picker = read('client/lib/datepicker.js');
assert.ok(/Template\.editDateForm\.events\(datePickerEvents\(\)\)/.test(picker),
'the child template handles its own events');
assert.ok(/Template\.currentData\(\)\?\.datePicker/.test(picker),
'the handlers use state passed by the popup');
for (const [popup, setter] of [['editCardReceivedDatePopup', 'setReceived'],
['editCardStartDatePopup', 'setStart'], ['editCardDueDatePopup', 'setDue'],
['editCardEndDatePopup', 'setEnd']]) {
assert.ok(js.includes(`Template.${popup}.onCreated`), `${popup} still has its own state`);
assert.ok(js.includes(setter), `${popup} still stores its own field`);
}
assert.ok(!/\.events\(datePickerEvents/.test(js),
'negative: no ineffective parent event map remains');
});
test('the shared template exists before its event map is registered', () => {
const imports = read('client/imports.js');
assert.ok(imports.indexOf("import '/client/features/forms'")
< imports.indexOf("import '/client/lib/datepicker'"),
'Template.editDateForm.events() must not run before datepicker.jade creates it');
assert.strictEqual((imports.match(/import '\/client\/lib\/datepicker'/g) || []).length, 1,
'the ordering fix must not load a second copy');
});
test('clicking an existing date reopens its editor with the Card context', () => {
const js = read('client/components/cards/cardDate.js');
const opener = js.slice(js.indexOf('function openDateEditor(name) {'),
js.indexOf('// Shared onCreated logic'));
assert.ok(/Popup\.open\(name\)\.call\(\s*cardFromDateContext\(templateInstance\.data\),\s*event,\s*templateInstance,?\s*\)/.test(opener),
'the surrounding date template supplies its Card, not dateBadgeBody arguments');
assert.ok(/event\.stopPropagation\(\)/.test(opener),
'cardDetails must not open a second popup while the badge click bubbles');
// Each date's card and minicard badge are built from its CARD_DATE_KINDS
// entry, whose editor the shared click handler opens.
for (const kind of ['Received', 'Start', 'Due', 'End']) {
assert.ok(new RegExp(`${kind}: \\{\\s*\\n\\s*read: card => card\\.get${kind}\\(\\),\\s*\\n\\s*editor: 'editCard${kind}Date',`)
.test(js), `${kind} dates reopen through the Card-aware handler`);
}
assert.ok(/'click \.js-edit-date': openDateEditor\(spec\.editor\)/.test(js),
'and the handler is openDateEditor for every one of them');
assert.ok(!/'click \.js-edit-date': Popup\.open\('editCard/.test(js),
'negative: no existing card date passes child display data to a popup');
});
console.log(`\ndateTemplates: ${passed} tests passed`);