Files
wekan/tests/labelFilterThreeState.test.cjs
T
Lauri Ojansivu b13a5af330 Add a three-state label filter: unfiltered -> include -> exclude -> clear.
#2886: clicking a label filter chip only ever toggled between unfiltered
and filtering FOR that label. Filter.labelIds now has a companion
Filter.excludedLabelIds SetFilter (same API/shape), and a new
Filter.toggleLabelFilter(labelId) cycles a label through three states:
not in either set -> added to labelIds (include) -> moved to
excludedLabelIds (exclude) -> removed (back to unfiltered).
Filter._getMongoSelector() merges the exclusion into the existing
labelIds selector ({$in, $nin}) rather than a separate ignored key, so a
card carrying an excluded label is filtered out even when it also
matches an included label - exclusion wins over inclusion. An
exclude-only filter (no included label) is also handled: isActive() and
the filterSelector-push condition both check excludedLabelIds, and
reset() clears it too.

sidebarFilters.js's 'click .js-toggle-label-filter' handler now calls
Filter.toggleLabelFilter() for a real label chip, keeping the existing
plain two-state Filter.labelIds.toggle() only for the special "no label"
pseudo-entry (data-filter-id="__none__"), which is out of scope for this
three-state cycle. sidebarFilters.jade gives an excluded chip a distinct
"excluded" class (struck-through text via sidebar.css) plus a fa-ban icon
tooltipped with the new filter-label-excluded i18n key, alongside the
existing fa-check used for an included chip.

Scoped to labels only, matching the issue's exact wording - members, due
dates and the other filter types keep their existing two-state toggle in
this pass; the same three-state treatment could be added to them later if
wanted.

tests/labelFilterThreeState.test.cjs drives the real client/lib/filter.js
under plain `node` (via tests/helpers/meteorStubLoader.mjs, an ESM loader
hook stubbing only the Meteor packages/modules filter.js imports) rather
than reimplementing the logic, and pins: the exact three-click cycle in
order (unfiltered -> included -> excluded -> unfiltered), that the cycle
repeats and that two labels cycle independently, and the card-matching
predicate - an excluded-only card is filtered out, an excluded label
wins over an included one on the same card, and an exclude-only filter
(nothing included) still filters correctly.

Thanks to bennyandresen and xet7 !

Closes #2886,
2026-09-10 20:35:40 +03:00

194 lines
6.8 KiB
JavaScript

'use strict';
// #2886: clicking a label filter chip cycles three states — unfiltered ->
// filter FOR this label (included) -> filter to EXCLUDE this label
// (inverted) -> unfiltered again — instead of the old two-state
// filter/unfilter toggle. This drives the REAL client/lib/filter.js
// (Filter.toggleLabelFilter / Filter.labelIds / Filter.excludedLabelIds /
// Filter._getMongoSelector) under plain `node`, stubbing only the Meteor
// packages it imports (see tests/helpers/meteorStubLoader.mjs), so the
// production cycling and card-matching logic is pinned directly rather than
// reimplemented in the test.
//
// Run: node tests/labelFilterThreeState.test.cjs
const path = require('node:path');
const { pathToFileURL } = require('node:url');
const assert = require('node:assert/strict');
const { register } = require('node:module');
const loaderUrl = pathToFileURL(
path.join(__dirname, 'helpers', 'meteorStubLoader.mjs'),
).href;
register(loaderUrl, pathToFileURL(__filename).href);
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
(async () => {
console.log('labelFilterThreeState:');
const filterUrl = pathToFileURL(
path.join(__dirname, '..', 'client', 'lib', 'filter.js'),
).href;
const { Filter } = await import(filterUrl);
test('starts unfiltered for a label neither set has seen', () => {
Filter.reset();
assert.equal(Filter.labelIds.isSelected('l1'), false);
assert.equal(Filter.excludedLabelIds.isSelected('l1'), false);
assert.equal(Filter.isActive(), false);
});
test('1st click: unfiltered -> included (filter FOR the label)', () => {
Filter.reset();
Filter.toggleLabelFilter('l1');
assert.equal(Filter.labelIds.isSelected('l1'), true);
assert.equal(Filter.excludedLabelIds.isSelected('l1'), false);
assert.equal(Filter.isActive(), true);
});
test('2nd click: included -> excluded (inverted, filter AGAINST the label)', () => {
Filter.toggleLabelFilter('l1');
assert.equal(Filter.labelIds.isSelected('l1'), false);
assert.equal(Filter.excludedLabelIds.isSelected('l1'), true);
assert.equal(Filter.isActive(), true);
});
test('3rd click: excluded -> unfiltered again (back to the start)', () => {
Filter.toggleLabelFilter('l1');
assert.equal(Filter.labelIds.isSelected('l1'), false);
assert.equal(Filter.excludedLabelIds.isSelected('l1'), false);
assert.equal(Filter.isActive(), false);
});
test('the cycle repeats identically a second time around', () => {
Filter.toggleLabelFilter('l1');
assert.equal(Filter.labelIds.isSelected('l1'), true);
Filter.toggleLabelFilter('l1');
assert.equal(Filter.excludedLabelIds.isSelected('l1'), true);
Filter.toggleLabelFilter('l1');
assert.equal(Filter.isActive(), false);
});
test('two different labels cycle independently of each other', () => {
Filter.reset();
Filter.toggleLabelFilter('l1'); // l1 included
Filter.toggleLabelFilter('l2'); // l2 included
Filter.toggleLabelFilter('l2'); // l2 excluded
assert.equal(Filter.labelIds.isSelected('l1'), true);
assert.equal(Filter.excludedLabelIds.isSelected('l1'), false);
assert.equal(Filter.labelIds.isSelected('l2'), false);
assert.equal(Filter.excludedLabelIds.isSelected('l2'), true);
});
// ---- the card-matching predicate (Filter._getMongoSelector) ----
function selectorMatches(selector, card) {
// A tiny in-memory evaluator for the shape of selector this suite
// produces: a top-level $or of plain-field selectors, each field either
// a literal, {$in: [...]}, {$nin: [...]}, or {$in, $nin} combined (an
// array field matched by intersection / non-intersection).
const matchesFieldSelector = (value, fieldSel) => {
if (fieldSel === null || typeof fieldSel !== 'object') {
return value === fieldSel;
}
const arr = Array.isArray(value) ? value : [value];
if ('$in' in fieldSel && !arr.some(v => fieldSel.$in.includes(v))) {
return false;
}
if ('$nin' in fieldSel && arr.some(v => fieldSel.$nin.includes(v))) {
return false;
}
return true;
};
const matchesClause = clause =>
Object.entries(clause).every(([field, fieldSel]) => {
if (field === '_id') {
return fieldSel.$in.includes(card._id);
}
return matchesFieldSelector(card[field], fieldSel);
});
if (selector.$or) {
return selector.$or.some(matchesClause);
}
return matchesClause(selector);
}
test('excluding a label filters out a card that carries only that label', () => {
Filter.reset();
Filter.toggleLabelFilter('l1');
Filter.toggleLabelFilter('l1'); // -> excluded
const selector = Filter._getMongoSelector();
assert.equal(
selectorMatches(selector, { _id: 'c1', labelIds: ['l1'] }),
false,
);
assert.equal(
selectorMatches(selector, { _id: 'c2', labelIds: ['l2'] }),
true,
);
});
test('exclusion wins over inclusion: a card matching an included label ' +
'is still filtered out if it ALSO carries an excluded label', () => {
Filter.reset();
Filter.labelIds.add('l1'); // include l1
Filter.excludedLabelIds.add('l2'); // exclude l2
const selector = Filter._getMongoSelector();
// Has the included label AND the excluded one: exclusion wins.
assert.equal(
selectorMatches(selector, { _id: 'c1', labelIds: ['l1', 'l2'] }),
false,
);
// Has only the included label: passes.
assert.equal(
selectorMatches(selector, { _id: 'c2', labelIds: ['l1'] }),
true,
);
// Has neither: fails (doesn't match the required inclusion).
assert.equal(
selectorMatches(selector, { _id: 'c3', labelIds: [] }),
false,
);
});
test('an exclude-only filter (no included label) still filters cards ' +
'carrying the excluded label, and passes every other card', () => {
Filter.reset();
Filter.excludedLabelIds.add('l2');
assert.equal(Filter.isActive(), true);
const selector = Filter._getMongoSelector();
assert.equal(
selectorMatches(selector, { _id: 'c1', labelIds: ['l2'] }),
false,
);
assert.equal(
selectorMatches(selector, { _id: 'c2', labelIds: ['l1'] }),
true,
);
assert.equal(
selectorMatches(selector, { _id: 'c3', labelIds: [] }),
true,
);
});
test('Filter.reset() clears both the included and excluded label sets', () => {
Filter.labelIds.add('l1');
Filter.excludedLabelIds.add('l2');
Filter.reset();
assert.equal(Filter.labelIds.isSelected('l1'), false);
assert.equal(Filter.excludedLabelIds.isSelected('l2'), false);
assert.equal(Filter.isActive(), false);
});
console.log(`\nlabelFilterThreeState: ${passed} tests passed`);
})().catch(err => {
console.error(err);
process.exit(1);
});