Fix searching/filtering bugs #235

- Fix a bug (introduced in 1b9be8fe) preventing the tree view from being
  visible during a search.
- Fix a minor bug where the scripts view does not render based on the
  initial filter.
- Add Vue component tests for `TheScriptView` to prevent regressions.
- Refactor `isSearching` in `TheScriptView` to simplify its logic.
This commit is contained in:
undergroundwires
2023-08-25 00:32:01 +02:00
parent 75c9b51bf2
commit 62f8bfac2f
11 changed files with 613 additions and 31 deletions

View File

@@ -17,7 +17,7 @@ export class CategoryCollectionStateStub implements ICategoryCollectionState {
public readonly code: IApplicationCode = new ApplicationCodeStub();
public readonly filter: IUserFilter = new UserFilterStub();
public filter: IUserFilter = new UserFilterStub();
public get os(): OperatingSystem {
return this.collectionStub.os;
@@ -42,6 +42,11 @@ export class CategoryCollectionStateStub implements ICategoryCollectionState {
return this;
}
public withFilter(filter: IUserFilter) {
this.filter = filter;
return this;
}
public withSelectedScripts(initialScripts: readonly SelectedScript[]) {
this.selection.withSelectedScripts(initialScripts);
return this;

View File

@@ -0,0 +1,14 @@
import { IEventSubscription } from '@/infrastructure/Events/IEventSource';
import { IEventSubscriptionCollection } from '@/infrastructure/Events/IEventSubscriptionCollection';
export class EventSubscriptionCollectionStub implements IEventSubscriptionCollection {
private readonly subscriptions = new Array<IEventSubscription>();
public register(...subscriptions: IEventSubscription[]) {
this.subscriptions.push(...subscriptions);
}
public unsubscribeAll() {
this.subscriptions.length = 0;
}
}

View File

@@ -33,6 +33,12 @@ export class FilterResultStub implements IFilterResult {
return this;
}
public withQueryAndSomeMatches() {
return this
.withQuery('non-empty query')
.withSomeMatches();
}
public withQuery(query: string) {
this.query = query;
return this;

View File

@@ -0,0 +1,19 @@
import { useApplication } from '@/presentation/components/Shared/Hooks/UseApplication';
import { IApplication } from '@/domain/IApplication';
import { ApplicationStub } from './ApplicationStub';
export class UseApplicationStub {
private application: IApplication = new ApplicationStub();
public withState(application: IApplication) {
this.application = application;
return this;
}
public get(): ReturnType<typeof useApplication> {
return {
application: this.application,
info: this.application.info,
};
}
}

View File

@@ -0,0 +1,87 @@
import { ref } from 'vue';
import {
ContextModifier, IStateCallbackSettings, NewStateEventHandler,
StateModifier, useCollectionState,
} from '@/presentation/components/Shared/Hooks/UseCollectionState';
import { ICategoryCollectionState } from '@/application/Context/State/ICategoryCollectionState';
import { IUserFilter } from '@/application/Context/State/Filter/IUserFilter';
import { IApplicationContext } from '@/application/Context/IApplicationContext';
import { IFilterResult } from '@/application/Context/State/Filter/IFilterResult';
import { CategoryCollectionStateStub } from './CategoryCollectionStateStub';
import { EventSubscriptionCollectionStub } from './EventSubscriptionCollectionStub';
import { ApplicationContextStub } from './ApplicationContextStub';
import { UserFilterStub } from './UserFilterStub';
export class UseCollectionStateStub {
private currentContext: IApplicationContext = new ApplicationContextStub();
private readonly currentState = ref<ICategoryCollectionState>(new CategoryCollectionStateStub());
private readonly onStateChangeHandlers = new Array<NewStateEventHandler>();
public withFilter(filter: IUserFilter) {
const state = new CategoryCollectionStateStub()
.withFilter(filter);
const context = new ApplicationContextStub()
.withState(state);
return new UseCollectionStateStub()
.withState(state)
.withContext(context);
}
public withFilterResult(filterResult: IFilterResult | undefined) {
const filter = new UserFilterStub()
.withCurrentFilterResult(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 triggerOnStateChange(newState: ICategoryCollectionState): void {
this.currentState.value = newState;
this.onStateChangeHandlers.forEach(
(handler) => handler(newState, undefined),
);
}
private onStateChange(
handler: NewStateEventHandler,
settings?: Partial<IStateCallbackSettings>,
) {
if (settings?.immediate) {
handler(this.currentState.value, undefined);
}
this.onStateChangeHandlers.push(handler);
}
private modifyCurrentState(mutator: StateModifier) {
mutator(this.currentState.value);
}
private modifyCurrentContext(mutator: ContextModifier) {
mutator(this.currentContext);
}
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,
events: new EventSubscriptionCollectionStub(),
};
}
}

View File

@@ -5,9 +5,16 @@ import { IFilterChangeDetails } from '@/application/Context/State/Filter/Event/I
import { FilterResultStub } from './FilterResultStub';
import { EventSourceStub } from './EventSourceStub';
export enum UserFilterMethod {
ApplyFilter,
ClearFilter,
}
export class UserFilterStub implements IUserFilter {
private readonly filterChangedSource = new EventSourceStub<IFilterChangeDetails>();
public readonly callHistory = new Array<UserFilterMethod>();
public currentFilter: IFilterResult | undefined = new FilterResultStub();
public filterChanged: IEventSource<IFilterChangeDetails> = this.filterChangedSource;
@@ -26,7 +33,11 @@ export class UserFilterStub implements IUserFilter {
return this;
}
public applyFilter(): void { /* NO OP */ }
public applyFilter(): void {
this.callHistory.push(UserFilterMethod.ApplyFilter);
}
public clearFilter(): void { /* NO OP */ }
public clearFilter(): void {
this.callHistory.push(UserFilterMethod.ClearFilter);
}
}