Files
wekan/tests/dueDateKeyboardShortcut.test.cjs
T
Lauri Ojansivu 138f727ebb Card shortcuts open only their popup.
d, Shift+M and Shift+C on a hovered or selected minicard opened the card
first, and Shift+M / Shift+C then opened the card's hamburger menu and
clicked its item: card, menu and popup for one key. Now the Due date, Move
card or Copy card popup opens by itself next to the minicard, as a click on
a minicard's date or label opens only its popup, and the card stays closed.
On an opened card Move / Copy open next to its hamburger without showing the
menu; d there still clicks the card's own Due date control.

The popup is opened directly, so client/lib/cardShortcutPopup.js applies the
conditions the controls a click would use are rendered under: the board
allows due dates and the user may modify the card (a Worker only edits an
existing date), Move card for a board admin, Copy card for a member who is
not a Worker, both only with cardMenuWrites. Without them nothing opens.
tests/cardShortcutPopup.test.cjs drives the rules and ties each one to its
template condition; the browser specs check the card stays closed and no
menu is shown, and that a non-admin and disabled shortcuts open nothing.
The help-list check matched "Move Card to Archive" too and now matches the
action text exactly.

Thanks to mimZD and xet7 !

Fixes #6924.
2026-11-16 16:46:06 +02:00

162 lines
10 KiB
JavaScript

'use strict';
// #6750: the `d` keyboard shortcut opens the Due date editor of the opened card.
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const root = path.join(__dirname, '..');
const read = rel => fs.readFileSync(path.join(root, rel), 'utf8');
const keyboard = read('client/lib/keyboard.js');
// A minimal DOM: elements with a class list, children and data contexts.
class Node {
constructor(classes = [], children = [], data) {
this.classes = classes;
this.children = children;
this.data = data;
}
matches(selector) {
// Only the selectors findDueDateControl uses: '.a', '.a .b' and lists.
return selector.split(',').some(part => {
const chain = part.trim().split(/\s+/).map(s => s.replace(/^\./, ''));
if (!this.classes.includes(chain[chain.length - 1])) return false;
let node = this.parent;
for (let i = chain.length - 2; i >= 0; i--) {
while (node && !node.classes.includes(chain[i])) node = node.parent;
if (!node) return false;
node = node.parent;
}
return true;
});
}
all() {
const out = [];
for (const child of this.children) {
child.parent = this;
out.push(child, ...child.all());
}
return out;
}
querySelectorAll(selector) { return this.all().filter(n => n.matches(selector)); }
querySelector(selector) { return this.querySelectorAll(selector)[0] || null; }
}
const details = (id, control) => new Node(['card-details', 'js-card-details'], [
new Node(['card-details-item', 'card-details-item-due'], control ? [new Node([control])] : []),
// Other date fields use the same badge class; they must never be chosen.
new Node(['card-details-item', 'card-details-item-start'], [new Node(['js-edit-date'])]),
], id && { _id: id });
const dataOf = element => element.data;
test('finds the due date + or badge of the opened card only', async () => {
const { findDueDateControl, DUE_DATE_CONTROL_SELECTOR } = await import('../client/lib/dueDateHotkey.js');
assert.match(DUE_DATE_CONTROL_SELECTOR, /card-details-item-due \.js-due-date/);
assert.match(DUE_DATE_CONTROL_SELECTOR, /card-details-item-due \.js-edit-date/);
const plus = new Node([], [details('card1', 'js-due-date')]);
const control = findDueDateControl(plus, { cardId: 'card1', canModifyCard: true, dataOf });
assert.ok(control && control.classes.includes('js-due-date'));
assert.ok(control.parent.classes.includes('card-details-item-due'), 'never the start date badge');
const badge = new Node([], [details('card1', 'js-edit-date')]);
const edit = findDueDateControl(badge, { cardId: 'card1', canModifyCard: true, dataOf });
assert.ok(edit && edit.parent.classes.includes('card-details-item-due'));
// Two card details open: the opened card's, not the first one on the page.
const two = new Node([], [details('other', 'js-due-date'), details('card1', 'js-edit-date')]);
const chosen = findDueDateControl(two, { cardId: 'card1', canModifyCard: true, dataOf });
assert.equal(chosen.parent.parent.data._id, 'card1');
// A details whose data context wraps the card is still recognised.
const wrapped = details(null, 'js-due-date');
wrapped.data = { card: { _id: 'card1' }, cardIndex: 0 };
assert.ok(findDueDateControl(new Node([], [wrapped]), { cardId: 'card1', canModifyCard: true, dataOf }));
});
test('negative: does nothing without an opened card, permission, control or match', async () => {
const { findDueDateControl } = await import('../client/lib/dueDateHotkey.js');
const page = new Node([], [details('card1', 'js-due-date')]);
assert.equal(findDueDateControl(page, { cardId: null, canModifyCard: true, dataOf }), null, 'no opened card');
assert.equal(findDueDateControl(page, { cardId: 'card1', canModifyCard: false, dataOf }), null, 'may not modify the card');
assert.equal(findDueDateControl(page, { cardId: 'card2', canModifyCard: true, dataOf }), null, 'another card is open');
// The card details render no control (due dates off for the board, a
// read-only member, or a Worker without a due date): nothing to click.
const none = new Node([], [details('card1', null)]);
assert.equal(findDueDateControl(none, { cardId: 'card1', canModifyCard: true, dataOf }), null);
// An unreadable details is used only when it is the only one on the page.
const unknown = new Node([], [details(null, 'js-due-date')]);
assert.ok(findDueDateControl(unknown, { cardId: 'card1', canModifyCard: true, dataOf: () => { throw new Error('x'); } }));
const ambiguous = new Node([], [details(null, 'js-due-date'), details(null, 'js-due-date')]);
assert.equal(findDueDateControl(ambiguous, { cardId: 'card1', canModifyCard: true, dataOf }), null);
assert.equal(findDueDateControl(null, { cardId: 'card1', canModifyCard: true, dataOf }), null);
});
test('d is bound once, through the shared filter, to the opened card with canModifyCard', () => {
const bindings = [...keyboard.matchAll(/hotkeys\(\s*(['"])([^'"]*)\1/g)].map(m => m[2]);
const keysOf = binding => binding.split(',').map(k => k.trim().toLowerCase());
const withD = bindings.filter(b => keysOf(b).some(k => k === 'd' || k === 'shift+d'));
assert.deepEqual(withD, ['d, shift+d'], 'd and Shift+D belong to the due date shortcut only');
for (const other of bindings.filter(b => b !== 'd, shift+d')) {
assert.ok(!keysOf(other).includes('d'), `${other} must not also take d`);
}
// Every hotkey runs through hotkeys.filter, which refuses shortcuts when the
// user turned them off and while typing in an input, textarea or editable.
const filter = keyboard.slice(keyboard.indexOf('hotkeys.filter ='), keyboard.indexOf('// Handle non-Latin keyboards'));
assert.match(filter, /isKeyboardShortcuts\(\)\)\s*\n\s*return false/);
assert.match(filter, /isContentEditable\)\s*\n\s*return false/);
assert.match(filter, /HTMLInputElement[^\n]*HTMLTextAreaElement/);
const fn = keyboard.slice(keyboard.indexOf('function clickDueDateControl'), keyboard.indexOf("hotkeys('d, shift+d'"));
assert.match(fn, /Utils\.getCurrentCardId\(\)/, 'targets the opened card');
assert.match(fn, /canModifyCard: Utils\.canModifyCard\(card\)/, 'same permission as the due date control');
assert.match(fn, /findDueDateControl\(document/);
assert.match(fn, /control\.click\(\)/, 'opens the same popup a click opens');
assert.doesNotMatch(fn, /Popup\.open/, 'never opens the popup past the rendered control');
});
// #6924 changed the minicard half: `d` on a hovered or selected minicard used
// to open the card and then click its Due date control (#6755); now it opens
// only the Due date popup next to the minicard, as a click on the minicard's
// date does, and the card stays closed. The permission it needs is the
// control's own, in client/lib/cardShortcutPopup.js
// (tests/cardShortcutPopup.test.cjs). An opened card still clicks its control.
test('#6755 / #6924: with no card opened, d opens only the popup of the hovered or selected minicard', () => {
const fn = keyboard.slice(keyboard.indexOf('export function openDueDateEditorOfOpenedCard'), keyboard.indexOf("hotkeys('d, shift+d'"));
assert.match(fn, /if \(cardId\) \{\n\s+const card = ReactiveCache\.getCard\(cardId\);\n\s+if \(!card \|\| !Utils\.canModifyCard\(card\)\) return false;\n\s+clickDueDateControlWhenRendered\(cardId\);/,
'an opened card wins, and #6840: its click waits until its details are rendered');
assert.match(fn, /return openCardShortcutPopupAtMinicard\('due'\);/);
assert.doesNotMatch(fn, /Utils\.goCardId/, 'negative: #6924 - the minicard\'s card is not opened');
const atMinicard = keyboard.slice(keyboard.indexOf('function openCardShortcutPopupAtMinicard'), keyboard.indexOf('function openCardShortcutPopupWhenRendered'));
assert.match(atMinicard, /Session\.get\('selectedCard'\) \|\| getHoveredCardId\(\)/, 'the same card the other card shortcuts use');
const wait = keyboard.slice(keyboard.indexOf('function clickDueDateControlWhenRendered'), keyboard.indexOf('export function openDueDateEditorOfOpenedCard'));
assert.match(wait, /if \(Utils\.getCurrentCardId\(\) !== cardId\) return;/,
'negative: stops when another card was opened meanwhile - never clicks for a card no longer open');
assert.match(wait, /if \(clickDueDateControl\(cardId\)\) return;/, 'clicks once, through the same control and permission');
assert.match(wait, /Date\.now\(\) - started < 3000/, 'gives up rather than waiting forever');
});
test('the due date control it clicks is the one card details renders only for editors', () => {
const jade = read('client/components/cards/cardDetails.jade');
const due = jade.slice(jade.indexOf('if $eq field "dueDate"'), jade.indexOf('if $eq field "endDate"'));
assert.match(due, /if currentBoard\.allowsDueDate/);
assert.match(due, /if canModifyCard\s*\n\s*unless currentUser\.isWorker\s*\n\s*a\.card-label\.add-label\.js-due-date/);
assert.match(due, /\.card-details-item-due/);
const date = read('client/components/cards/cardDate.jade');
assert.match(date, /template\(name="dateBadgeBody"\)\s*\n\s*if canModifyCard\s*\n\s*a\.js-edit-date/);
const details = read('client/components/cards/cardDetails.js');
assert.match(details, /'click \.js-due-date': Popup\.open\('editCardDueDate'\)/);
// cardDueDate and minicardDueDate are both built from CARD_DATE_KINDS.Due.
const dateJs = read('client/components/cards/cardDate.js');
assert.match(dateJs, /Due: \{\s*\n\s*read: card => card\.getDue\(\),\s*\n\s*editor: 'editCardDueDate',/);
assert.match(dateJs, /'click \.js-edit-date': openDateEditor\(spec\.editor\)/);
});
test('appears in the keyboard shortcuts help list with English text', () => {
const mapping = keyboard.slice(keyboard.indexOf('Template.keyboardShortcuts.helpers'));
assert.match(mapping, /keys: \['d'\],\s*\n\s*action: 'shortcut-edit-due-date'/);
const en = JSON.parse(read('imports/i18n/data/en.i18n.json'));
assert.equal(typeof en['shortcut-edit-due-date'], 'string');
assert.ok(en['shortcut-edit-due-date'].trim().length > 0);
});