Files
privacy.sexy/tests/unit/shared/Stubs/UseCollectionStateStub.ts
undergroundwires 6142f3a297 Extend search by including documentation content
This commit broadens the search functionality within privacy.sexy by
including documentation text in the search scope. Users can now find
scripts and categories not only by their names but also by content in
their documentation. This improvement aims to make the discovery of
relevant scripts and information more intuitive and comprehensive.

Key changes:

- Documentation text is now searchable, enhancing the ability to
  discover scripts and categories based on content details.

Other supporting changes:

- Remove interface prefixes (`I`) from related interfaces to adhere to
  naming conventions, enhancing code readability.
- Refactor filtering to separate actual filtering logic from filter
  state management, improving the structure for easier maintenance.
- Improve test coverage to ensure relability of existing and new search
  capabilities.
- Test coverage expanded to ensure the reliability of the new search
  capabilities.
2024-02-14 12:10:49 +01:00

133 lines
4.3 KiB
TypeScript

import { shallowRef } from 'vue';
import {
ContextModifier, IStateCallbackSettings, NewStateEventHandler,
StateModifier, useCollectionState,
} from '@/presentation/components/Shared/Hooks/UseCollectionState';
import { ICategoryCollectionState } from '@/application/Context/State/ICategoryCollectionState';
import { FilterContext } from '@/application/Context/State/Filter/FilterContext';
import { IApplicationContext } from '@/application/Context/IApplicationContext';
import { FilterResult } from '@/application/Context/State/Filter/Result/FilterResult';
import { CategoryCollectionStateStub } from './CategoryCollectionStateStub';
import { ApplicationContextStub } from './ApplicationContextStub';
import { FilterContextStub } from './FilterContextStub';
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
export class UseCollectionStateStub
extends StubWithObservableMethodCalls<ReturnType<typeof useCollectionState>> {
private currentContext: IApplicationContext = new ApplicationContextStub();
private readonly currentState = shallowRef<ICategoryCollectionState>(
new CategoryCollectionStateStub(),
);
public withFilter(filter: FilterContext) {
const state = new CategoryCollectionStateStub()
.withFilter(filter);
const context = new ApplicationContextStub()
.withState(state);
return new UseCollectionStateStub()
.withState(state)
.withContext(context);
}
public withFilterResult(filterResult: FilterResult | undefined) {
const filter = new FilterContextStub()
.withCurrentFilter(filterResult);
return this.withFilter(filter);
}
public withContext(context: IApplicationContext) {
this.currentContext = context;
return this;
}
public withState(state: ICategoryCollectionState) {
this.currentState.value = state;
return this;
}
public get state(): ICategoryCollectionState {
return this.currentState.value;
}
public isStateModified(): boolean {
const call = this.callHistory.find((c) => c.methodName === 'modifyCurrentState');
return call !== undefined;
}
public triggerImmediateStateChange(): void {
this.triggerOnStateChange({
newState: this.currentState.value,
immediateOnly: true,
});
}
public triggerOnStateChange(scenario: {
readonly newState: ICategoryCollectionState,
readonly immediateOnly: boolean,
}): void {
this.currentState.value = scenario.newState;
let handlers = this.getRegisteredHandlers();
if (scenario.immediateOnly) {
handlers = handlers.filter((args) => args[1]?.immediate === true);
}
const callbacks = handlers.map((args) => args[0]);
if (!callbacks.length) {
throw new Error('No handler callbacks are registered to handle state change');
}
callbacks.forEach(
(handler) => handler(scenario.newState, undefined),
);
}
public get(): ReturnType<typeof useCollectionState> {
return {
modifyCurrentState: this.modifyCurrentState.bind(this),
modifyCurrentContext: this.modifyCurrentContext.bind(this),
onStateChange: this.onStateChange.bind(this),
currentContext: this.currentContext,
currentState: this.currentState,
};
}
private onStateChange(
handler: NewStateEventHandler,
settings?: Partial<IStateCallbackSettings>,
) {
if (settings?.immediate) {
handler(this.currentState.value, undefined);
}
this.registerMethodCall({
methodName: 'onStateChange',
args: [handler, settings],
});
}
private modifyCurrentState(mutator: StateModifier) {
mutator(this.currentState.value);
this.registerMethodCall({
methodName: 'modifyCurrentState',
args: [mutator],
});
}
private modifyCurrentContext(mutator: ContextModifier) {
mutator(this.currentContext);
this.registerMethodCall({
methodName: 'modifyCurrentContext',
args: [mutator],
});
}
private getRegisteredHandlers(): readonly Parameters<ReturnType<typeof useCollectionState>['onStateChange']>[] {
const calls = this.callHistory.filter((call) => call.methodName === 'onStateChange');
return calls.map((handler) => {
const [callback, settings] = handler.args;
return [
callback as NewStateEventHandler,
settings as Partial<IStateCallbackSettings>,
];
});
}
}