Files
wekan/tests/filterQueryParams4540.test.cjs
T
Lauri Ojansivu 5ffecf7aab Sync the board Filter sidebar's state back into the URL query params.
#4540 (963b01ff5) already lets a board be opened pre-filtered from
`?assignee=johndoe`/`?member=janedoe`/`?label=urgent`, read once on board
load. #319 additionally asked for the other direction: applying a filter
from the sidebar should update the URL too, so a manually filtered view is
itself bookmarkable/shareable without hand-typing the query string.

Adds `client/lib/filterQueryParams.js`'s `resolveIdsToUsernames`,
`resolveIdsToLabelNames` and `buildBoardFilterQueryParams` - the exact
inverse of the existing parser, reusing the same `assignee`/`member`/`label`
token vocabulary - and a reactive `list()` getter on `SetFilter`
(`client/lib/filter.js`) so its current selection can be read outside the
sidebar template. `boardBody.js`'s new `syncFilterQueryParams`, run from a
separate `Tracker.autorun`, resolves `Filter.assignees`/`members`/`labelIds`
back to names and writes them via `FlowRouter.setQueryParams` wrapped in
`FlowRouter.withReplaceState`, so toggling a filter replaces the current
history entry instead of piling up a new one per click.

Covered by tests/filterQueryParams319.test.cjs: the id->name resolvers, the
query-param builder (including empty selections clearing their param and
unknown ids being dropped), a full round trip through #4540's own
`parseBoardFilterQueryParams` proving both directions agree, and source
checks pinning how boardBody.js wires it up.

Thanks to netei and xet7 !

Closes #319,
2026-09-10 21:11:34 +03:00

126 lines
4.3 KiB
JavaScript

'use strict';
// #4540: a board's filter state can be driven by URL query params, e.g.
// `?assignee=johndoe&member=janedoe&label=urgent`. This pins the pure
// param-parsing/resolution logic in client/lib/filterQueryParams.js, which
// client/components/boards/boardBody.js wires into the existing `Filter`
// object once the board is ready.
const assert = require('node:assert/strict');
const path = require('node:path');
const { pathToFileURL } = require('node:url');
(async () => {
const modUrl = pathToFileURL(
path.join(__dirname, '..', 'client', 'lib', 'filterQueryParams.js'),
).href;
const {
parseListParam,
resolveUsernamesToIds,
resolveLabelNamesToIds,
parseBoardFilterQueryParams,
} = await import(modUrl);
// parseListParam: splits, trims, drops empties, de-duplicates.
assert.deepEqual(parseListParam('johndoe'), ['johndoe']);
assert.deepEqual(parseListParam(' johndoe , janedoe ,,johndoe'), [
'johndoe',
'janedoe',
]);
assert.deepEqual(parseListParam(''), []);
assert.deepEqual(parseListParam(undefined), []);
assert.deepEqual(parseListParam(null), []);
// resolveUsernamesToIds: matches by username, silently drops unknowns.
const users = [
{ _id: 'u1', username: 'johndoe' },
{ _id: 'u2', username: 'janedoe' },
];
assert.deepEqual(resolveUsernamesToIds(['johndoe', 'janedoe'], users), [
'u1',
'u2',
]);
assert.deepEqual(resolveUsernamesToIds(['johndoe', 'nobody'], users), [
'u1',
]);
assert.deepEqual(resolveUsernamesToIds([], users), []);
assert.deepEqual(resolveUsernamesToIds(['johndoe'], []), []);
// resolveLabelNamesToIds: matches by name, case-insensitively.
const labels = [
{ _id: 'l1', name: 'Urgent', color: 'red' },
{ _id: 'l2', name: 'Later', color: 'blue' },
];
assert.deepEqual(resolveLabelNamesToIds(['urgent'], labels), ['l1']);
assert.deepEqual(resolveLabelNamesToIds(['URGENT', 'later'], labels), [
'l1',
'l2',
]);
assert.deepEqual(resolveLabelNamesToIds(['missing'], labels), []);
// parseBoardFilterQueryParams: the end-to-end shape Filter needs.
const result = parseBoardFilterQueryParams(
{ assignee: 'johndoe', member: 'janedoe,johndoe', label: 'Urgent' },
users,
labels,
);
assert.deepEqual(result.assigneeIds, ['u1']);
assert.deepEqual(result.memberIds, ['u2', 'u1']);
assert.deepEqual(result.labelIds, ['l1']);
// Missing/empty query params resolve to empty arrays, never throw.
const empty = parseBoardFilterQueryParams({}, users, labels);
assert.deepEqual(empty, { assigneeIds: [], memberIds: [], labelIds: [] });
const emptyNoArgs = parseBoardFilterQueryParams(undefined, undefined, undefined);
assert.deepEqual(emptyNoArgs, {
assigneeIds: [],
memberIds: [],
labelIds: [],
});
// boardBody.js must actually wire this into the existing Filter object
// rather than a new filtering engine, and must apply it once the board's
// subscription is ready (not eagerly in the route, before members/labels
// are loaded).
const fs = require('node:fs');
const boardBody = fs.readFileSync(
path.join(__dirname, '..', 'client', 'components', 'boards', 'boardBody.js'),
'utf8',
);
assert.match(
boardBody,
/parseBoardFilterQueryParams,?\s*[\s\S]{0,80}from '\/client\/lib\/filterQueryParams';/,
'boardBody.js must import the pure param-parsing helper',
);
assert.match(
boardBody,
/applyQueryParamFilters = \(boardId\) => \{/,
'boardBody.js must define applyQueryParamFilters',
);
assert.match(
boardBody,
/Filter\.assignees\.add\(id\)/,
'resolved assignee ids must be applied through the existing Filter.assignees API',
);
assert.match(
boardBody,
/Filter\.members\.add\(id\)/,
'resolved member ids must be applied through the existing Filter.members API',
);
assert.match(
boardBody,
/Filter\.labelIds\.add\(id\)/,
'resolved label ids must be applied through the existing Filter.labelIds API',
);
assert.match(
boardBody,
/Tracker\.nonreactive\(\(\) => this\.applyQueryParamFilters\(currentBoardId\)\);/,
'the filters must be applied once the board subscription is ready, guarded like the other once-per-board hooks',
);
console.log('filterQueryParams4540: URL query param filter parsing passed');
})().catch(err => {
console.error(err);
process.exit(1);
});