Files
wekan/tests/boardViewPerBoard.test.cjs
T
Lauri Ojansivu 80fb317fda Remember the board view per board.
The view menu sits on a board, but choosing a view there changed it for
every board, so one board could not stay on Calendar while another stayed
on Swimlanes. A view chosen on a board is now that board's own
(profile.boardViews, published to its owner beside the shared value). A
board without its own choice uses the view every board shared before, then
the board's default, so nothing changes for anybody until they choose a
view on a board. A choice made off any board still sets the shared view,
and the browser's stored view follows only that one. The server accepts only
a known view under a safe board key. Label text visibility per board, the
other half of #4256, was done earlier.

The guards that pinned the single shared view now pin both; a new test runs
the stored-view logic per board, and a Playwright case checks two boards.

Thanks to DimDz, Meeques and xet7 !

Fixes #4906,
Fixes #4256.
2026-10-08 12:12:24 +03:00

68 lines
3.8 KiB
JavaScript

'use strict';
// #4906 / #4256: "I like to view some boards as calendar, others as
// swimlanes" - the view menu sits on a board but changed every board. A view
// chosen on a board is now that board's own (profile.boardViews[boardId]); a
// board without its own choice uses the view all boards shared before, then
// the board's default.
//
// Run: node tests/boardViewPerBoard.test.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const settings = require('../models/lib/boardViewSettings');
const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
const source = read('client/lib/utils.js');
const methods = source.slice(source.indexOf(' boardView() {'), source.indexOf(' // `swimlaneId` scopes'));
let user, boardId = null, pending = null;
const context = {
...settings,
allowBoardView: () => true,
pendingBoardView: { get: () => pending, set: value => { pending = value; } },
ReactiveCache: { getCurrentUser: () => user },
currentUserWith: () => user,
window: { localStorage: { getItem: () => null, setItem: () => { throw Error('Reading a view must not write storage'); } } },
};
vm.createContext(context);
vm.runInContext(`var Utils = {${methods} getCurrentBoard() { return null; }, getCurrentBoardId() { return currentBoardId(); } };`,
Object.assign(context, { currentBoardId: () => boardId }));
const Utils = context.Utils;
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('boardViewPerBoard:');
test('each board keeps its own view; others keep the shared one', () => {
user = { boardViewPreference: 'board-view-swimlanes', boardViewPreferences: { A: 'board-view-cal', B: 'board-view-lists' } };
boardId = 'A'; assert.equal(Utils.storedBoardView(), 'board-view-cal');
boardId = 'B'; assert.equal(Utils.storedBoardView(), 'board-view-lists');
boardId = 'C'; assert.equal(Utils.storedBoardView(), 'board-view-swimlanes', 'a board never chosen on uses the shared view');
boardId = null; assert.equal(Utils.storedBoardView(), 'board-view-swimlanes', 'off any board, the shared view');
});
test('a pending choice belongs to the board it was made on', () => {
user = { boardViewPreference: 'board-view-swimlanes', boardViewPreferences: {} };
pending = { view: 'board-view-cal', boardId: 'A' };
boardId = 'A'; assert.equal(Utils.storedBoardView(), 'board-view-cal');
boardId = 'B'; assert.equal(Utils.storedBoardView(), 'board-view-swimlanes', 'not on another board');
user.boardViewPreferences.A = 'board-view-cal';
boardId = 'A'; Utils.storedBoardView();
assert.equal(pending, null, 'acknowledged once board A has it');
});
test('negative: an unknown per-board value falls back rather than rendering nothing', () => {
user = { boardViewPreference: 'board-view-lists', boardViewPreferences: { A: 'board-view-nonsense' } };
boardId = 'A'; assert.equal(Utils.storedBoardView(), 'board-view-lists');
});
test('the server stores the choice under the board, checked, and publishes it', () => {
const model = read('models/users.js');
assert.match(model, /async setBoardView\(view, boardId\) \{\s*if \(boardId\) \{\s*assertSafeMapKey\(boardId\);[\s\S]*?if \(!isKnownBoardView\(view\)\) throw new Meteor\.Error\('invalid-board-view'[\s\S]*?\[`profile\.boardViews\.\$\{boardId\}`\]: view/);
assert.match(read('server/models/users.js'), /check\(boardId, Match\.Optional\(Match\.Maybe\(String\)\)\);/);
assert.match(read('server/publications/userBoardView.js'), /boardViewPreferences: fields\.profile\?\.boardViews \|\| \{\}/);
assert.match(source, /Meteor\.call\('setBoardView', view, boardId,/);
assert.match(source, /if \(!boardId\) window\.localStorage\.setItem\('boardView', view\);/);
});
console.log(`\nboardViewPerBoard: ${passed} tests passed`);