Files
privacy.sexy/tests/unit/shared/Stubs/ScriptSelectionStub.ts
undergroundwires 55fa7eae71 Add 'Revert All Selection' feature #68
This commit introduces 'Revert: None - Selected' toggle, enabling users
to revert all reversible scripts with a single action, improving user
safety and control over script effects.

This feature addresses user-reported concerns about the ease of
reverting script changes. This feature should enhance the user experience
by streamlining the revert process along with providing essential
information about script reversibility.

Key changes:

- Add buttons to revert all selected scripts or setting all selected
  scripts to non-revert state.
- Add tooltips with detailed explanations about consequences of
  modifying revert states, includinginformation about irreversible
  script changes.

Supporting changes:

- Align items on top menu vertically for better visual consistency.
- Rename `SelectionType` to `RecommendationStatusType` for more clarity.
- Rename `IReverter` to `Reverter` to move away from `I` prefix
  convention.
- The `.script` CSS class was duplicated in `TheScriptsView.vue` and
  `TheScriptsArea.vue`, leading to style collisions in the development
  environment. The class has been renamed to component-specific classes
  to avoid such issues in the future.
2024-02-11 22:47:34 +01:00

131 lines
4.5 KiB
TypeScript

import { expect } from 'vitest';
import { ScriptSelection } from '@/application/Context/State/Selection/Script/ScriptSelection';
import { SelectedScript } from '@/application/Context/State/Selection/Script/SelectedScript';
import { IScript } from '@/domain/IScript';
import { ScriptSelectionChange, ScriptSelectionChangeCommand } from '@/application/Context/State/Selection/Script/ScriptSelectionChange';
import { formatAssertionMessage } from '@tests/shared/FormatAssertionMessage';
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
import { EventSourceStub } from './EventSourceStub';
import { SelectedScriptStub } from './SelectedScriptStub';
export class ScriptSelectionStub
extends StubWithObservableMethodCalls<ScriptSelection>
implements ScriptSelection {
public readonly changed = new EventSourceStub<readonly SelectedScript[]>();
public selectedScripts: readonly SelectedScript[] = [];
public isSelectedResult: boolean | undefined;
public withSelectedScripts(selectedScripts: readonly SelectedScript[]): this {
this.selectedScripts = selectedScripts;
return this;
}
public triggerSelectionChangedEvent(scripts: readonly SelectedScript[]): this {
this.changed.notify(scripts);
return this;
}
public withIsSelectedResult(isSelected: boolean): this {
this.isSelectedResult = isSelected;
return this;
}
public isScriptSelected(scriptId: string, revert: boolean): boolean {
return this.isScriptChanged({
scriptId,
newStatus: {
isSelected: true,
isReverted: revert,
},
});
}
public isScriptDeselected(scriptId: string): boolean {
return this.isScriptChanged({
scriptId,
newStatus: {
isSelected: false,
},
});
}
public processChanges(action: ScriptSelectionChangeCommand): void {
this.registerMethodCall({
methodName: 'processChanges',
args: [action],
});
}
public selectOnly(scripts: ReadonlyArray<IScript>): void {
this.registerMethodCall({
methodName: 'selectOnly',
args: [scripts],
});
this.selectedScripts = scripts.map((s) => new SelectedScriptStub(s));
}
public selectAll(): void {
this.registerMethodCall({
methodName: 'selectAll',
args: [],
});
}
public deselectAll(): void {
this.registerMethodCall({
methodName: 'deselectAll',
args: [],
});
}
public isSelected(): boolean {
if (this.isSelectedResult === undefined) {
throw new Error('Method not configured.');
}
return this.isSelectedResult;
}
public assertSelectionChanges(expectedChanges: readonly ScriptSelectionChange[]): void {
const actualChanges = this.getAllChanges();
expect(actualChanges).to.have.lengthOf(expectedChanges.length, formatAssertionMessage([
`Expected number of changes to be ${expectedChanges.length}, but found ${actualChanges.length}`,
`Expected changes (${expectedChanges.length}):`, toNumberedPrettyJson(expectedChanges),
`Actual changes (${actualChanges.length}):`, toNumberedPrettyJson(actualChanges),
]));
const unexpectedChanges = actualChanges.filter(
(actual) => !expectedChanges.some((expected) => isSameChange(actual, expected)),
);
expect(unexpectedChanges).to.have.lengthOf(0, formatAssertionMessage([
`Found ${unexpectedChanges.length} unexpected changes.`,
'Unexpected changes:', toNumberedPrettyJson(unexpectedChanges),
'Expected changes:', toNumberedPrettyJson(expectedChanges),
'Actual changes:', toNumberedPrettyJson(actualChanges),
]));
}
private isScriptChanged(expectedChange: ScriptSelectionChange): boolean {
return this.getAllChanges().some((change) => isSameChange(change, expectedChange));
}
private getAllChanges(): ScriptSelectionChange[] {
const processChangesCalls = this.callHistory.filter((c) => c.methodName === 'processChanges');
const changeCommands = processChangesCalls.map(
(call) => call.args[0] as ScriptSelectionChangeCommand,
);
const changes = changeCommands.flatMap((command) => command.changes);
return changes;
}
}
function isSameChange(change: ScriptSelectionChange, otherChange: ScriptSelectionChange): boolean {
return change.newStatus.isSelected === otherChange.newStatus.isSelected
&& change.newStatus.isReverted === otherChange.newStatus.isReverted
&& change.scriptId === otherChange.scriptId;
}
function toNumberedPrettyJson<T>(array: readonly T[]): string {
return array.map((item, index) => `${index + 1}: ${JSON.stringify(item, undefined, 2)}`).join('\n');
}