Files
privacy.sexy/tests/unit/shared/Stubs/TreeNodeStateAccessStub.ts
undergroundwires 8f188acd3c Fix loss of tree node state when switching views
This commit fixes an issue where the check state of categories was lost
when toggling between card and tree views. This is solved by immediately
emitting node state changes for all nodes. This ensures consistent view
transitions without any loss of node state information.

Furthermore, this commit includes added unit tests for the modified code
sections.
2023-09-24 20:34:47 +02:00

72 lines
2.3 KiB
TypeScript

import { NodeStateChangedEvent, TreeNodeStateAccess, TreeNodeStateTransaction } from '@/presentation/components/Scripts/View/Tree/TreeView/Node/State/StateAccess';
import { TreeNodeStateDescriptor } from '@/presentation/components/Scripts/View/Tree/TreeView/Node/State/StateDescriptor';
import { TreeNodeCheckState } from '@/presentation/components/Scripts/View/Tree/TreeView/Node/State/CheckState';
import { TreeNodeStateDescriptorStub } from './TreeNodeStateDescriptorStub';
import { EventSourceStub } from './EventSourceStub';
import { TreeNodeStateTransactionStub } from './TreeNodeStateTransactionStub';
export class TreeNodeStateAccessStub implements TreeNodeStateAccess {
public isStateModificationRequested = false;
public current: TreeNodeStateDescriptor = new TreeNodeStateDescriptorStub();
public changed: EventSourceStub<NodeStateChangedEvent> = new EventSourceStub();
public toggleCheck(): void {
throw new Error('Method not implemented.');
}
public toggleExpand(): void {
throw new Error('Method not implemented.');
}
public beginTransaction(): TreeNodeStateTransaction {
return new TreeNodeStateTransactionStub();
}
public commitTransaction(transaction: TreeNodeStateTransaction): void {
const oldState = this.current;
const newState = {
...oldState,
...transaction.updatedState,
};
this.current = newState;
this.changed.notify({
oldState,
newState,
});
this.isStateModificationRequested = true;
}
public triggerStateChangedEvent(event: NodeStateChangedEvent) {
this.changed.notify(event);
}
public withCurrent(state: TreeNodeStateDescriptor): this {
this.current = state;
return this;
}
public withCurrentCheckState(checkState: TreeNodeCheckState): this {
return this.withCurrent(
new TreeNodeStateDescriptorStub()
.withCheckState(checkState),
);
}
public withCurrentVisibility(isVisible: boolean): this {
return this.withCurrent(
new TreeNodeStateDescriptorStub()
.withVisibility(isVisible),
);
}
}
export function createAccessStubsFromCheckStates(
states: readonly TreeNodeCheckState[],
): TreeNodeStateAccessStub[] {
return states.map(
(checkState) => new TreeNodeStateAccessStub().withCurrentCheckState(checkState),
);
}