add reversibility on category level

This commit is contained in:
undergroundwires
2020-09-01 21:18:16 +01:00
parent d235dee955
commit f51e8859ee
23 changed files with 717 additions and 162 deletions

View File

@@ -11,6 +11,7 @@ import { Script } from '@/domain/Script';
import { IApplication } from '@/domain/IApplication';
import { IApplicationCode } from './Code/IApplicationCode';
import applicationFile from 'js-yaml-loader!@/application/application.yaml';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
/** Mutatable singleton application state that's the single source of truth throughout the application */
export class ApplicationState implements IApplicationState {
@@ -37,7 +38,7 @@ export class ApplicationState implements IApplicationState {
public readonly app: IApplication,
/** Initially selected scripts */
public readonly defaultScripts: Script[]) {
this.selection = new UserSelection(app, defaultScripts);
this.selection = new UserSelection(app, defaultScripts.map((script) => new SelectedScript(script, false)));
this.code = new ApplicationCode(this.selection, app.version);
this.filter = new UserFilter(app);
}

View File

@@ -7,7 +7,7 @@ export interface IUserSelection {
readonly selectedScripts: ReadonlyArray<SelectedScript>;
readonly totalSelected: number;
removeAllInCategory(categoryId: number): void;
addAllInCategory(categoryId: number): void;
addOrUpdateAllInCategory(categoryId: number, revert: boolean): void;
addSelectedScript(scriptId: string, revert: boolean): void;
addOrUpdateSelectedScript(scriptId: string, revert: boolean): void;
removeSelectedScript(scriptId: string): void;

View File

@@ -12,13 +12,11 @@ export class UserSelection implements IUserSelection {
constructor(
private readonly app: IApplication,
/** Initially selected scripts */
selectedScripts: ReadonlyArray<IScript>) {
selectedScripts: ReadonlyArray<SelectedScript>) {
this.scripts = new InMemoryRepository<string, SelectedScript>();
if (selectedScripts && selectedScripts.length > 0) {
for (const script of selectedScripts) {
const selected = new SelectedScript(script, false);
this.scripts.addItem(selected);
this.scripts.addItem(script);
}
}
}
@@ -36,16 +34,19 @@ export class UserSelection implements IUserSelection {
this.changed.notify(this.scripts.getItems());
}
public addAllInCategory(categoryId: number): void {
public addOrUpdateAllInCategory(categoryId: number, revert: boolean = false): void {
const category = this.app.findCategory(categoryId);
const scriptsToAdd = category.getAllScriptsRecursively()
.filter((script) => !this.scripts.exists(script.id));
if (!scriptsToAdd.length) {
const scriptsToAddOrUpdate = category.getAllScriptsRecursively()
.filter((script) =>
!this.scripts.exists(script.id)
|| this.scripts.getById(script.id).revert !== revert,
);
if (!scriptsToAddOrUpdate.length) {
return;
}
for (const script of scriptsToAdd) {
const selectedScript = new SelectedScript(script, false);
this.scripts.addItem(selectedScript);
for (const script of scriptsToAddOrUpdate) {
const selectedScript = new SelectedScript(script, revert);
this.scripts.addOrUpdateItem(selectedScript);
}
this.changed.notify(this.scripts.getItems());
}

View File

@@ -3,6 +3,7 @@ import { IEntity } from '../Entity/IEntity';
export interface IRepository<TKey, TEntity extends IEntity<TKey>> {
readonly length: number;
getItems(predicate?: (entity: TEntity) => boolean): TEntity[];
getById(id: TKey): TEntity | undefined;
addItem(item: TEntity): void;
addOrUpdateItem(item: TEntity): void;
removeItem(id: TKey): void;

View File

@@ -16,6 +16,14 @@ export class InMemoryRepository<TKey, TEntity extends IEntity<TKey>> implements
return predicate ? this.items.filter(predicate) : this.items;
}
public getById(id: TKey): TEntity | undefined {
const items = this.getItems((entity) => entity.id === id);
if (!items.length) {
return undefined;
}
return items[0];
}
public addItem(item: TEntity): void {
if (!item) {
throw new Error('item is null or undefined');

View File

@@ -74,7 +74,7 @@ function convertCategoryToNode(
text: category.name,
children,
documentationUrls: category.documentationUrls,
isReversible: false,
isReversible: children && children.every((child) => child.isReversible),
};
}

View File

@@ -7,7 +7,6 @@
:filterPredicate="filterPredicate"
:filterText="filterText"
v-on:nodeSelected="toggleNodeSelectionAsync($event)"
v-on:nodeRevertToggled="handleNodeRevertToggleAsync($event)"
>
</SelectableTree>
</span>
@@ -58,10 +57,10 @@
const state = await this.getCurrentStateAsync();
switch (event.node.type) {
case NodeType.Category:
this.toggleCategoryNodeSelection(event, state);
toggleCategoryNodeSelection(event, state);
break;
case NodeType.Script:
this.toggleScriptNodeSelection(event, state);
toggleScriptNodeSelection(event, state);
break;
default:
throw new Error(`Unknown node type: ${event.node.id}`);
@@ -100,26 +99,26 @@
this.filterText = result.query;
this.filtered = result;
}
private toggleCategoryNodeSelection(event: INodeSelectedEvent, state: IApplicationState): void {
const categoryId = getCategoryId(event.node.id);
if (event.isSelected) {
state.selection.addAllInCategory(categoryId);
} else {
state.selection.removeAllInCategory(categoryId);
}
}
private toggleScriptNodeSelection(event: INodeSelectedEvent, state: IApplicationState): void {
const scriptId = getScriptId(event.node.id);
const actualToggleState = state.selection.isSelected(scriptId);
const targetToggleState = event.isSelected;
if (targetToggleState && !actualToggleState) {
state.selection.addSelectedScript(scriptId, false);
} else if (!targetToggleState && actualToggleState) {
state.selection.removeSelectedScript(scriptId);
}
}
}
function toggleCategoryNodeSelection(event: INodeSelectedEvent, state: IApplicationState): void {
const categoryId = getCategoryId(event.node.id);
if (event.isSelected) {
state.selection.addOrUpdateAllInCategory(categoryId, false);
} else {
state.selection.removeAllInCategory(categoryId);
}
}
function toggleScriptNodeSelection(event: INodeSelectedEvent, state: IApplicationState): void {
const scriptId = getScriptId(event.node.id);
const actualToggleState = state.selection.isSelected(scriptId);
const targetToggleState = event.isSelected;
if (targetToggleState && !actualToggleState) {
state.selection.addSelectedScript(scriptId, false);
} else if (!targetToggleState && actualToggleState) {
state.selection.removeSelectedScript(scriptId);
}
}
</script>
<style scoped lang="scss">

View File

@@ -1,4 +1,3 @@
// Two ways of typing other libraries: https://stackoverflow.com/a/53070501
declare module 'liquor-tree' {
import { PluginObject } from 'vue';
@@ -10,6 +9,8 @@ declare module 'liquor-tree' {
filter(query: string): void;
clearFilter(): void;
setModel(nodes: ReadonlyArray<ILiquorTreeNewNode>): void;
// getNodeById(id: string): ILiquorTreeExistingNode;
// recurseDown(fn: (node: ILiquorTreeExistingNode) => void): void;
}
interface ICustomLiquorTreeData {
type: number;
@@ -34,6 +35,7 @@ declare module 'liquor-tree' {
data: ILiquorTreeNodeData;
states: ILiquorTreeNodeState | undefined;
children: ReadonlyArray<ILiquorTreeExistingNode> | undefined;
expand(): void;
}
/**

View File

@@ -4,7 +4,7 @@
<RevertToggle
class="item"
v-if="data.isReversible"
:scriptId="data.id" />
:node="data" />
<DocumentationUrls
class="item"
v-if="data.documentationUrls && data.documentationUrls.length > 0"

View File

@@ -2,7 +2,8 @@
<div class="checkbox-switch" >
<input type="checkbox" class="input-checkbox"
v-model="isReverted"
@change="onRevertToggledAsync()" >
@change="onRevertToggledAsync()"
v-on:click.stop>
<div class="checkbox-animate">
<span class="checkbox-off">revert</span>
<span class="checkbox-on">revert</span>
@@ -12,130 +13,141 @@
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
import { IReverter } from './Reverter/IReverter';
import { StatefulVue } from '@/presentation/StatefulVue';
import { INode } from './INode';
import { INode, NodeType } from './INode';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { IApplicationState } from '@/application/State/IApplicationState';
import { getCategoryId, getScriptId } from './../../ScriptNodeParser';
import { getReverter } from './Reverter/ReverterFactory';
@Component
export default class RevertToggle extends StatefulVue {
@Prop() public scriptId: string;
@Prop() public node: INode;
public isReverted = false;
private handler: IReverter;
public async mounted() {
await this.onNodeChangedAsync(this.node);
const state = await this.getCurrentStateAsync();
state.selection.changed.on(this.handleSelectionChanged);
this.updateState(state.selection.selectedScripts);
state.selection.changed.on((scripts) => this.updateState(scripts));
}
@Watch('node') public async onNodeChangedAsync(node: INode) {
const state = await this.getCurrentStateAsync();
this.handler = getReverter(node, state.app);
}
public async onRevertToggledAsync() {
const state = await this.getCurrentStateAsync();
state.selection.addOrUpdateSelectedScript(this.scriptId, this.isReverted);
this.handler.selectWithRevertState(this.isReverted, state.selection);
}
private handleSelectionChanged(selectedScripts: ReadonlyArray<SelectedScript>): void {
const selectedScript = selectedScripts.find((script) => script.id === this.scriptId);
if (!selectedScript) {
this.isReverted = false;
} else {
this.isReverted = selectedScript.revert;
}
private updateState(scripts: ReadonlyArray<SelectedScript>) {
this.isReverted = this.handler.getState(scripts);
}
}
</script>
<style scoped lang="scss">
@import "@/presentation/styles/colors.scss";
$width: 85px;
$height: 30px;
// https://www.designlabthemes.com/css-toggle-switch/
.checkbox-switch {
cursor: pointer;
display: inline-block;
overflow: hidden;
position: relative;
width: $width;
height: $height;
-webkit-border-radius: $height;
border-radius: $height;
line-height: $height;
font-size: $height / 2;
display: inline-block;
input.input-checkbox {
position: absolute;
left: 0;
top: 0;
width: $width;
height: $height;
padding: 0;
margin: 0;
opacity: 0;
z-index: 2;
@import "@/presentation/styles/colors.scss";
$width: 85px;
$height: 30px;
// https://www.designlabthemes.com/css-toggle-switch/
.checkbox-switch {
cursor: pointer;
}
.checkbox-animate {
display: inline-block;
overflow: hidden;
position: relative;
width: $width;
height: $height;
background-color: $gray;
-webkit-transition: background-color 0.25s ease-out 0s;
transition: background-color 0.25s ease-out 0s;
-webkit-border-radius: $height;
border-radius: $height;
line-height: $height;
font-size: $height / 2;
display: inline-block;
// Circle
&:before {
$circle-size: $height * 0.66;
content: "";
display: block;
input.input-checkbox {
position: absolute;
width: $circle-size;
height: $circle-size;
border-radius: $circle-size * 2;
-webkit-border-radius: $circle-size * 2;
background-color: $slate;
top: $height * 0.16;
left: $width * 0.05;
-webkit-transition: left 0.3s ease-out 0s;
transition: left 0.3s ease-out 0s;
z-index: 10;
}
}
input.input-checkbox:checked {
+ .checkbox-animate {
background-color: $accent;
}
+ .checkbox-animate:before {
left: ($width - $width/3.5);
background-color: $light-gray;
}
+ .checkbox-animate .checkbox-off {
display: none;
left: 0;
top: 0;
width: $width;
height: $height;
padding: 0;
margin: 0;
opacity: 0;
z-index: 2;
cursor: pointer;
}
+ .checkbox-animate .checkbox-on {
display: block;
.checkbox-animate {
position: relative;
width: $width;
height: $height;
background-color: $gray;
-webkit-transition: background-color 0.25s ease-out 0s;
transition: background-color 0.25s ease-out 0s;
// Circle
&:before {
$circle-size: $height * 0.66;
content: "";
display: block;
position: absolute;
width: $circle-size;
height: $circle-size;
border-radius: $circle-size * 2;
-webkit-border-radius: $circle-size * 2;
background-color: $slate;
top: $height * 0.16;
left: $width * 0.05;
-webkit-transition: left 0.3s ease-out 0s;
transition: left 0.3s ease-out 0s;
z-index: 10;
}
}
input.input-checkbox:checked {
+ .checkbox-animate {
background-color: $accent;
}
+ .checkbox-animate:before {
left: ($width - $width/3.5);
background-color: $light-gray;
}
+ .checkbox-animate .checkbox-off {
display: none;
opacity: 0;
}
+ .checkbox-animate .checkbox-on {
display: block;
opacity: 1;
}
}
.checkbox-off, .checkbox-on {
float: left;
color: $white;
font-weight: 700;
-webkit-transition: all 0.3s ease-out 0s;
transition: all 0.3s ease-out 0s;
}
.checkbox-off {
margin-left: $width / 3;
opacity: 1;
}
}
.checkbox-off, .checkbox-on {
float: left;
color: $white;
font-weight: 700;
-webkit-transition: all 0.3s ease-out 0s;
transition: all 0.3s ease-out 0s;
.checkbox-on {
display: none;
float: right;
margin-right: $width / 3;
opacity: 0;
}
}
.checkbox-off {
margin-left: $width / 3;
opacity: 1;
}
.checkbox-on {
display: none;
float: right;
margin-right: $width / 3;
opacity: 0;
}
}
</style>

View File

@@ -0,0 +1,30 @@
import { IReverter } from './IReverter';
import { getCategoryId } from '../../../ScriptNodeParser';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { IApplication } from '@/domain/IApplication';
import { ScriptReverter } from './ScriptReverter';
import { IUserSelection } from '@/application/State/Selection/IUserSelection';
export class CategoryReverter implements IReverter {
private readonly categoryId: number;
private readonly scriptReverters: ReadonlyArray<ScriptReverter>;
constructor(nodeId: string, app: IApplication) {
this.categoryId = getCategoryId(nodeId);
this.scriptReverters = getAllSubScriptReverters(this.categoryId, app);
}
public getState(selectedScripts: ReadonlyArray<SelectedScript>): boolean {
return this.scriptReverters.every((script) => script.getState(selectedScripts));
}
public selectWithRevertState(newState: boolean, selection: IUserSelection): void {
selection.addOrUpdateAllInCategory(this.categoryId, newState);
}
}
function getAllSubScriptReverters(categoryId: number, app: IApplication) {
const category = app.findCategory(categoryId);
if (!category) {
throw new Error(`Category with id "${categoryId}" does not exist`);
}
const scripts = category.getAllScriptsRecursively();
return scripts.map((script) => new ScriptReverter(script.id));
}

View File

@@ -0,0 +1,7 @@
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { IUserSelection } from '@/application/State/IApplicationState';
export interface IReverter {
getState(selectedScripts: ReadonlyArray<SelectedScript>): boolean;
selectWithRevertState(newState: boolean, selection: IUserSelection): void;
}

View File

@@ -0,0 +1,16 @@
import { INode, NodeType } from '../INode';
import { IReverter } from './IReverter';
import { ScriptReverter } from './ScriptReverter';
import { IApplication } from '@/domain/IApplication';
import { CategoryReverter } from './CategoryReverter';
export function getReverter(node: INode, app: IApplication): IReverter {
switch (node.type) {
case NodeType.Category:
return new CategoryReverter(node.id, app);
case NodeType.Script:
return new ScriptReverter(node.id);
default:
throw new Error('Unknown script type');
}
}

View File

@@ -0,0 +1,21 @@
import { IReverter } from './IReverter';
import { getScriptId } from '../../../ScriptNodeParser';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { IUserSelection } from '@/application/State/IApplicationState';
export class ScriptReverter implements IReverter {
private readonly scriptId: string;
constructor(nodeId: string) {
this.scriptId = getScriptId(nodeId);
}
public getState(selectedScripts: ReadonlyArray<SelectedScript>): boolean {
const selectedScript = selectedScripts.find((selected) => selected.id === this.scriptId);
if (!selectedScript) {
return false;
}
return selectedScript.revert;
}
public selectWithRevertState(newState: boolean, selection: IUserSelection): void {
selection.addOrUpdateSelectedScript(this.scriptId, newState);
}
}

View File

@@ -7,7 +7,7 @@
v-on:node:unchecked="nodeSelected($event)"
ref="treeElement"
>
<span class="tree-text" slot-scope="{ node }">
<span class="tree-text" slot-scope="{ node }" >
<Node :data="convertExistingToNode(node)" />
</span>
</tree>

View File

@@ -26,7 +26,7 @@ describe('ApplicationCode', () => {
// arrange
const scripts = [new ScriptStub('first'), new ScriptStub('second')];
const app = new ApplicationStub().withAction(new CategoryStub(1).withScripts(...scripts));
const selection = new UserSelection(app, scripts);
const selection = new UserSelection(app, scripts.map((script) => new SelectedScript(script, false)));
const version = 'version-string';
const sut = new ApplicationCode(selection, version);
// act
@@ -42,7 +42,7 @@ describe('ApplicationCode', () => {
let signaled: ICodeChangedEvent;
const scripts = [new ScriptStub('first'), new ScriptStub('second')];
const app = new ApplicationStub().withAction(new CategoryStub(1).withScripts(...scripts));
const selection = new UserSelection(app, scripts);
const selection = new UserSelection(app, scripts.map((script) => new SelectedScript(script, false)));
const sut = new ApplicationCode(selection, 'version');
sut.changed.on((code) => signaled = code);
// act
@@ -56,7 +56,7 @@ describe('ApplicationCode', () => {
let signaled: ICodeChangedEvent;
const scripts = [new ScriptStub('first'), new ScriptStub('second')];
const app = new ApplicationStub().withAction(new CategoryStub(1).withScripts(...scripts));
const selection = new UserSelection(app, scripts);
const selection = new UserSelection(app, scripts.map((script) => new SelectedScript(script, false)));
const version = 'version-string';
const sut = new ApplicationCode(selection, version);
sut.changed.on((code) => signaled = code);
@@ -72,7 +72,7 @@ describe('ApplicationCode', () => {
let signaled: ICodeChangedEvent;
const scripts = [new ScriptStub('first'), new ScriptStub('second')];
const app = new ApplicationStub().withAction(new CategoryStub(1).withScripts(...scripts));
const selection = new UserSelection(app, scripts);
const selection = new UserSelection(app, scripts.map((script) => new SelectedScript(script, false)));
const expectedVersion = 'version-string';
const scriptsToSelect = scripts.map((s) => new SelectedScript(s, false));
const totalLines = 20;

View File

@@ -1,20 +1,47 @@
import { ScriptStub } from './../../../stubs/ScriptStub';
import 'mocha';
import { expect } from 'chai';
import { IScript } from '@/domain/IScript';
import { SelectedScriptStub } from '../../../stubs/SelectedScriptStub';
import { ScriptStub } from '../../../stubs/ScriptStub';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { CategoryStub } from '../../../stubs/CategoryStub';
import { ApplicationStub } from '../../../stubs/ApplicationStub';
import { UserSelection } from '@/application/State/Selection/UserSelection';
import 'mocha';
import { expect } from 'chai';
import { IScript } from '@/domain/IScript';
describe('UserSelection', () => {
describe('ctor', () => {
it('has nothing with no initial selection', () => {
// arrange
const app = new ApplicationStub().withAction(new CategoryStub(1).withScriptIds('s1'));
const selection = [];
// act
const sut = new UserSelection(app, selection);
// assert
expect(sut.selectedScripts).to.have.lengthOf(0);
});
it('has initial selection', () => {
// arrange
const firstScript = new ScriptStub('1');
const secondScript = new ScriptStub('2');
const app = new ApplicationStub().withAction(
new CategoryStub(1).withScript(firstScript).withScripts(secondScript));
const expected = [ new SelectedScript(firstScript, false), new SelectedScript(secondScript, true) ];
// act
const sut = new UserSelection(app, expected);
// assert
expect(sut.selectedScripts).to.deep.include(expected[0]);
expect(sut.selectedScripts).to.deep.include(expected[1]);
});
});
it('deselectAll removes all items', () => {
// arrange
const events: Array<readonly SelectedScript[]> = [];
const app = new ApplicationStub()
.withAction(new CategoryStub(1)
.withScriptIds('s1', 's2', 's3', 's4'));
const selectedScripts = [new ScriptStub('s1'), new ScriptStub('s2'), new ScriptStub('s3')];
const selectedScripts = [
new SelectedScriptStub('s1'), new SelectedScriptStub('s2'), new SelectedScriptStub('s3'),
];
const sut = new UserSelection(app, selectedScripts);
sut.changed.on((newScripts) => events.push(newScripts));
// act
@@ -30,15 +57,20 @@ describe('UserSelection', () => {
const app = new ApplicationStub()
.withAction(new CategoryStub(1)
.withScriptIds('s1', 's2', 's3', 's4'));
const selectedScripts = [new ScriptStub('s1'), new ScriptStub('s2'), new ScriptStub('s3')];
const selectedScripts = [
new SelectedScriptStub('s1'), new SelectedScriptStub('s2'), new SelectedScriptStub('s3'),
];
const sut = new UserSelection(app, selectedScripts);
sut.changed.on((newScripts) => events.push(newScripts));
const scripts = [new ScriptStub('s2'), new ScriptStub('s3'), new ScriptStub('s4')];
const expected = scripts.map((script) => new SelectedScript(script, false));
const expected = [ new SelectedScriptStub('s2'), new SelectedScriptStub('s3'),
new SelectedScript(scripts[2], false)];
// act
sut.selectOnly(scripts);
// assert
expect(sut.selectedScripts).to.deep.equal(expected);
expect(sut.selectedScripts).to.have.deep.members(expected,
`Expected: ${JSON.stringify(sut.selectedScripts)}\n` +
`Actual: ${JSON.stringify(expected)}`);
expect(events).to.have.lengthOf(1);
expect(events[0]).to.deep.equal(expected);
});
@@ -112,10 +144,10 @@ describe('UserSelection', () => {
it('removes all when all exists', () => {
// arrange
const categoryId = 1;
const scripts = [new ScriptStub('s1'), new ScriptStub('s2')];
const scripts = [new SelectedScriptStub('s1'), new SelectedScriptStub('s2')];
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...scripts));
.withScripts(...scripts.map((script) => script.script)));
const sut = new UserSelection(app, scripts);
// act
sut.removeAllInCategory(categoryId);
@@ -131,7 +163,7 @@ describe('UserSelection', () => {
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...existing, ...notExisting));
const sut = new UserSelection(app, existing);
const sut = new UserSelection(app, existing.map((script) => new SelectedScript(script, false)));
// act
sut.removeAllInCategory(categoryId);
// assert
@@ -139,7 +171,7 @@ describe('UserSelection', () => {
expect(sut.selectedScripts.length).to.equal(0);
});
});
describe('addAllInCategory', () => {
describe('addOrUpdateAllInCategory', () => {
it('does nothing when all already exists', () => {
// arrange
const events: Array<readonly SelectedScript[]> = [];
@@ -148,10 +180,10 @@ describe('UserSelection', () => {
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...scripts));
const sut = new UserSelection(app, scripts);
const sut = new UserSelection(app, scripts.map((script) => new SelectedScript(script, false)));
sut.changed.on((s) => events.push(s));
// act
sut.addAllInCategory(categoryId);
sut.addOrUpdateAllInCategory(categoryId);
// assert
expect(events).to.have.lengthOf(0);
expect(sut.selectedScripts.map((script) => script.id))
@@ -166,12 +198,26 @@ describe('UserSelection', () => {
.withScripts(...expected));
const sut = new UserSelection(app, []);
// act
sut.addAllInCategory(categoryId);
sut.addOrUpdateAllInCategory(categoryId);
// assert
expect(sut.selectedScripts.map((script) => script.id))
.to.have.deep.members(expected.map((script) => script.id));
});
it('adds not existing some exists', () => {
it('adds all with given revert status when nothing exists', () => {
// arrange
const categoryId = 1;
const expected = [new ScriptStub('s1'), new ScriptStub('s2')];
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...expected));
const sut = new UserSelection(app, []);
// act
sut.addOrUpdateAllInCategory(categoryId, true);
// assert
expect(sut.selectedScripts.every((script) => script.revert))
.to.equal(true);
});
it('changes revert status of all when some exists', () => {
// arrange
const categoryId = 1;
const notExisting = [ new ScriptStub('notExisting1'), new ScriptStub('notExisting2') ];
@@ -180,12 +226,42 @@ describe('UserSelection', () => {
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...allScripts));
const sut = new UserSelection(app, existing);
const sut = new UserSelection(app, existing.map((script) => new SelectedScript(script, false)));
// act
sut.addAllInCategory(categoryId);
sut.addOrUpdateAllInCategory(categoryId, true);
// assert
expect(sut.selectedScripts.map((script) => script.id))
.to.have.deep.members(allScripts.map((script) => script.id));
expect(sut.selectedScripts.every((script) => script.revert))
.to.equal(true);
});
it('changes revert status of all when some exists', () => {
// arrange
const categoryId = 1;
const notExisting = [ new ScriptStub('notExisting1'), new ScriptStub('notExisting2') ];
const existing = [ new ScriptStub('existing1'), new ScriptStub('existing2') ];
const allScripts = [ ...existing, ...notExisting ];
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...allScripts));
const sut = new UserSelection(app, existing.map((script) => new SelectedScript(script, false)));
// act
sut.addOrUpdateAllInCategory(categoryId, true);
// assert
expect(sut.selectedScripts.every((script) => script.revert))
.to.equal(true);
});
it('changes revert status of all when all already exists', () => {
// arrange
const categoryId = 1;
const scripts = [ new ScriptStub('existing1'), new ScriptStub('existing2') ];
const app = new ApplicationStub()
.withAction(new CategoryStub(categoryId)
.withScripts(...scripts));
const sut = new UserSelection(app, scripts.map((script) => new SelectedScript(script, false)));
// act
sut.addOrUpdateAllInCategory(categoryId, true);
// assert
expect(sut.selectedScripts.every((script) => script.revert))
.to.equal(true);
});
});
describe('isSelected', () => {
@@ -196,7 +272,7 @@ describe('UserSelection', () => {
const app = new ApplicationStub()
.withAction(new CategoryStub(1)
.withScripts(selectedScript, notSelectedScript));
const sut = new UserSelection(app, [ selectedScript ]);
const sut = new UserSelection(app, [ new SelectedScript(selectedScript, false) ]);
// act
const actual = sut.isSelected(notSelectedScript.id);
// assert
@@ -209,7 +285,7 @@ describe('UserSelection', () => {
const app = new ApplicationStub()
.withAction(new CategoryStub(1)
.withScripts(selectedScript, notSelectedScript));
const sut = new UserSelection(app, [ selectedScript ]);
const sut = new UserSelection(app, [ new SelectedScript(selectedScript, false) ]);
// act
const actual = sut.isSelected(selectedScript.id);
// assert

View File

@@ -95,4 +95,26 @@ describe('InMemoryRepository', () => {
expect(actual).to.deep.equal(expected);
});
});
describe('getById', () => {
it('gets entity if it exists', () => {
// arrange
const expected = new NumericEntityStub(1).withCustomProperty('bca');
const sut = new InMemoryRepository<number, NumericEntityStub>([
expected, new NumericEntityStub(2).withCustomProperty('bca'),
new NumericEntityStub(3).withCustomProperty('bca'), new NumericEntityStub(4).withCustomProperty('bca'),
]);
// act
const actual = sut.getById(expected.id);
// assert
expect(actual).to.deep.equal(expected);
});
it('gets undefined if it does not exist', () => {
// arrange
const sut = new InMemoryRepository<number, NumericEntityStub>([]);
// act
const actual = sut.getById(31);
// assert
expect(actual).to.equal(undefined);
});
});
});

View File

@@ -0,0 +1,120 @@
import 'mocha';
import { expect } from 'chai';
import { getScriptNodeId, getScriptId, getCategoryNodeId, getCategoryId } from '@/presentation/Scripts/ScriptsTree/ScriptNodeParser';
import { CategoryStub } from '../../../stubs/CategoryStub';
import { ScriptStub } from '../../../stubs/ScriptStub';
import { parseSingleCategory, parseAllCategories } from '../../../../../src/presentation/Scripts/ScriptsTree/ScriptNodeParser';
import { ApplicationStub } from '../../../stubs/ApplicationStub';
import { INode, NodeType } from '../../../../../src/presentation/Scripts/ScriptsTree/SelectableTree/Node/INode';
import { IScript } from '../../../../../src/domain/IScript';
import { ICategory } from '../../../../../src/domain/ICategory';
describe('ScriptNodeParser', () => {
it('can convert script id and back', () => {
// arrange
const script = new ScriptStub('test');
// act
const nodeId = getScriptNodeId(script);
const scriptId = getScriptId(nodeId);
// assert
expect(scriptId).to.equal(script.id);
});
it('can convert category id and back', () => {
// arrange
const category = new CategoryStub(55);
// act
const nodeId = getCategoryNodeId(category);
const scriptId = getCategoryId(nodeId);
// assert
expect(scriptId).to.equal(category.id);
});
describe('parseSingleCategory', () => {
it('can parse when category has sub categories', () => {
// arrange
const categoryId = 31;
const firstSubCategory = new CategoryStub(11).withScriptIds('111', '112');
const secondSubCategory = new CategoryStub(categoryId)
.withCategory(new CategoryStub(33).withScriptIds('331', '331'))
.withCategory(new CategoryStub(44).withScriptIds('44'));
const app = new ApplicationStub().withAction(new CategoryStub(categoryId)
.withCategory(firstSubCategory)
.withCategory(secondSubCategory));
// act
const nodes = parseSingleCategory(categoryId, app);
// assert
expect(nodes).to.have.lengthOf(2);
expectSameCategory(nodes[0], firstSubCategory);
expectSameCategory(nodes[1], secondSubCategory);
});
it('can parse when category has sub scripts', () => {
// arrange
const categoryId = 31;
const scripts = [ new ScriptStub('script1'), new ScriptStub('script2'), new ScriptStub('script3') ];
const app = new ApplicationStub().withAction(new CategoryStub(categoryId).withScripts(...scripts));
// act
const nodes = parseSingleCategory(categoryId, app);
// assert
expect(nodes).to.have.lengthOf(3);
expectSameScript(nodes[0], scripts[0]);
expectSameScript(nodes[1], scripts[1]);
expectSameScript(nodes[2], scripts[2]);
});
});
it('parseAllCategories parses as expected', () => {
// arrange
const app = new ApplicationStub()
.withAction(new CategoryStub(0).withScriptIds('1, 2'))
.withAction(new CategoryStub(1).withCategories(
new CategoryStub(3).withScriptIds('3', '4'),
new CategoryStub(4).withCategory(new CategoryStub(5).withScriptIds('6')),
));
// act
const nodes = parseAllCategories(app);
// assert
expect(nodes).to.have.lengthOf(2);
expectSameCategory(nodes[0], app.actions[0]);
expectSameCategory(nodes[1], app.actions[1]);
});
});
function isReversible(category: ICategory): boolean {
if (category.scripts) {
return category.scripts.every((s) => s.revertCode);
}
return category.subCategories.every((c) => isReversible(c));
}
function expectSameCategory(node: INode, category: ICategory): void {
expect(node.type).to.equal(NodeType.Category, getErrorMessage('type'));
expect(node.id).to.equal(getCategoryNodeId(category), getErrorMessage('id'));
expect(node.documentationUrls).to.equal(category.documentationUrls, getErrorMessage('documentationUrls'));
expect(node.text).to.equal(category.name, getErrorMessage('name'));
expect(node.isReversible).to.equal(isReversible(category), getErrorMessage('isReversible'));
expect(node.children).to.have.lengthOf(category.scripts.length || category.subCategories.length, getErrorMessage('name'));
for (let i = 0; i < category.subCategories.length; i++) {
expectSameCategory(node.children[i], category.subCategories[i]);
}
for (let i = 0; i < category.scripts.length; i++) {
expectSameScript(node.children[i], category.scripts[i]);
}
function getErrorMessage(field: string) {
return `Unexpected node field: ${field}.\n` +
`\nActual node:\n${JSON.stringify(node, null, 2)}` +
`\nExpected category:\n${JSON.stringify(category, null, 2)}`;
}
}
function expectSameScript(node: INode, script: IScript): void {
expect(node.type).to.equal(NodeType.Script, getErrorMessage('type'));
expect(node.id).to.equal(getScriptNodeId(script), getErrorMessage('id'));
expect(node.documentationUrls).to.equal(script.documentationUrls, getErrorMessage('documentationUrls'));
expect(node.text).to.equal(script.name, getErrorMessage('name'));
expect(node.isReversible).to.equal(!!script.revertCode, getErrorMessage('revertCode'));
expect(node.children).to.equal(undefined);
function getErrorMessage(field: string) {
return `Unexpected node field: ${field}.` +
`\nActual node:\n${JSON.stringify(node, null, 2)}\n` +
`\nExpected script:\n${JSON.stringify(script, null, 2)}`;
}
}

View File

@@ -0,0 +1,106 @@
import 'mocha';
import { expect } from 'chai';
import { ScriptStub } from '../../../../../../stubs/ScriptStub';
import { CategoryReverter } from '@/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter';
import { getCategoryNodeId } from '@/presentation/Scripts/ScriptsTree/ScriptNodeParser';
import { CategoryStub } from '../../../../../../stubs/CategoryStub';
import { Script } from '@/domain/Script';
import { ApplicationStub } from '../../../../../../stubs/ApplicationStub';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { UserSelection } from '@/application/State/Selection/UserSelection';
describe('CategoryReverter', () => {
describe('getState', () => {
// arrange
const scripts = [
new ScriptStub('revertable').withRevertCode('REM revert me'),
new ScriptStub('revertable2').withRevertCode('REM revert me 2'),
];
const category = new CategoryStub(1).withScripts(...scripts);
const nodeId = getCategoryNodeId(category);
const app = new ApplicationStub().withAction(category);
const sut = new CategoryReverter(nodeId, app);
const testCases = [
{
name: 'false when subscripts are not reverted',
state: scripts.map((script) => new SelectedScript(script, false)),
expected: false,
},
{
name: 'false when some subscripts are reverted',
state: [new SelectedScript(scripts[0], false), new SelectedScript(scripts[0], true)],
expected: false,
},
{
name: 'false when subscripts are not reverted',
state: scripts.map((script) => new SelectedScript(script, true)),
expected: true,
},
];
for (const testCase of testCases) {
it(testCase.name, () => {
// act
const actual = sut.getState(testCase.state);
// assert
expect(actual).to.equal(testCase.expected);
});
}
});
describe('selectWithRevertState', () => {
// arrange
const scripts = [
new ScriptStub('revertable').withRevertCode('REM revert me'),
new ScriptStub('revertable2').withRevertCode('REM revert me 2'),
];
const category = new CategoryStub(1).withScripts(...scripts);
const app = new ApplicationStub().withAction(category);
const testCases = [
{
name: 'selects with revert state when not selected',
selection: [],
revert: true, expectRevert: true,
},
{
name: 'selects with non-revert state when not selected',
selection: [],
revert: false, expectRevert: false,
},
{
name: 'switches when already selected with revert state',
selection: scripts.map((script) => new SelectedScript(script, true)),
revert: false, expectRevert: false,
},
{
name: 'switches when already selected with not revert state',
selection: scripts.map((script) => new SelectedScript(script, false)),
revert: true, expectRevert: true,
},
{
name: 'keeps revert state when already selected with revert state',
selection: scripts.map((script) => new SelectedScript(script, true)),
revert: true, expectRevert: true,
},
{
name: 'keeps revert state deselected when already selected wtih non revert state',
selection: scripts.map((script) => new SelectedScript(script, false)),
revert: false, expectRevert: false,
},
];
const nodeId = getCategoryNodeId(category);
for (const testCase of testCases) {
it(testCase.name, () => {
const selection = new UserSelection(app, testCase.selection);
const sut = new CategoryReverter(nodeId, app);
// act
sut.selectWithRevertState(testCase.revert, selection);
// assert
expect(sut.getState(selection.selectedScripts)).to.equal(testCase.expectRevert);
expect(selection.selectedScripts).has.lengthOf(2);
expect(selection.selectedScripts[0].id).equal(scripts[0].id);
expect(selection.selectedScripts[1].id).equal(scripts[1].id);
expect(selection.selectedScripts[0].revert).equal(testCase.expectRevert);
expect(selection.selectedScripts[1].revert).equal(testCase.expectRevert);
});
}
});
});

View File

@@ -0,0 +1,45 @@
import 'mocha';
import { expect } from 'chai';
import { INode, NodeType } from '@/presentation/Scripts/ScriptsTree/SelectableTree/Node/INode';
import { getReverter } from '@/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ReverterFactory';
import { ScriptReverter } from '@/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter';
import { CategoryReverter } from '@/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter';
import { ApplicationStub } from '../../../../../../stubs/ApplicationStub';
import { CategoryStub } from '../../../../../../stubs/CategoryStub';
import { getScriptNodeId, getCategoryNodeId } from '@/presentation/Scripts/ScriptsTree/ScriptNodeParser';
import { ScriptStub } from '../../../../../../stubs/ScriptStub';
describe('ReverterFactory', () => {
describe('getReverter', () => {
it('gets CategoryReverter for category node', () => {
// arrange
const category = new CategoryStub(0).withScriptIds('55');
const node = getNodeStub(getCategoryNodeId(category), NodeType.Category);
const app = new ApplicationStub().withAction(category);
// act
const result = getReverter(node, app);
// assert
expect(result instanceof CategoryReverter).to.equal(true);
});
it('gets ScriptReverter for script node', () => {
// arrange
const script = new ScriptStub('test');
const node = getNodeStub(getScriptNodeId(script), NodeType.Script);
const app = new ApplicationStub().withAction(new CategoryStub(0).withScript(script));
// act
const result = getReverter(node, app);
// assert
expect(result instanceof ScriptReverter).to.equal(true);
});
});
function getNodeStub(nodeId: string, type: NodeType): INode {
return {
id: nodeId,
text: 'text',
isReversible: false,
documentationUrls: [],
children: [],
type,
};
}
});

View File

@@ -0,0 +1,88 @@
import 'mocha';
import { expect } from 'chai';
import { ScriptReverter } from '@/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter';
import { SelectedScriptStub } from '../../../../../../stubs/SelectedScriptStub';
import { getScriptNodeId } from '@/presentation/Scripts/ScriptsTree/ScriptNodeParser';
import { ScriptStub } from '../../../../../../stubs/ScriptStub';
import { UserSelection } from '../../../../../../../../src/application/State/Selection/UserSelection';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { ApplicationStub } from '../../../../../../stubs/ApplicationStub';
import { CategoryStub } from '../../../../../../stubs/CategoryStub';
describe('ScriptReverter', () => {
describe('getState', () => {
it('false when script is not selected', () => {
// arrange
const script = new ScriptStub('id');
const nodeId = getScriptNodeId(script);
const sut = new ScriptReverter(nodeId);
// act
const actual = sut.getState([]);
// assert
expect(actual).to.equal(false);
});
it('false when script is selected but not reverted', () => {
// arrange
const scripts = [ new SelectedScriptStub('id'), new SelectedScriptStub('dummy') ];
const nodeId = getScriptNodeId(scripts[0].script);
const sut = new ScriptReverter(nodeId);
// act
const actual = sut.getState(scripts);
// assert
expect(actual).to.equal(false);
});
it('true when script is selected and reverted', () => {
// arrange
const scripts = [ new SelectedScriptStub('id', true), new SelectedScriptStub('dummy') ];
const nodeId = getScriptNodeId(scripts[0].script);
const sut = new ScriptReverter(nodeId);
// act
const actual = sut.getState(scripts);
// assert
expect(actual).to.equal(true);
});
});
describe('selectWithRevertState', () => {
// arrange
const script = new ScriptStub('id');
const app = new ApplicationStub().withAction(new CategoryStub(5).withScript(script));
const testCases = [
{
name: 'selects with revert state when not selected',
selection: [], revert: true, expectRevert: true,
},
{
name: 'selects with non-revert state when not selected',
selection: [], revert: false, expectRevert: false,
},
{
name: 'switches when already selected with revert state',
selection: [ new SelectedScript(script, true)], revert: false, expectRevert: false,
},
{
name: 'switches when already selected with not revert state',
selection: [ new SelectedScript(script, false)], revert: true, expectRevert: true,
},
{
name: 'keeps revert state when already selected with revert state',
selection: [ new SelectedScript(script, true)], revert: true, expectRevert: true,
},
{
name: 'keeps revert state deselected when already selected wtih non revert state',
selection: [ new SelectedScript(script, false)], revert: false, expectRevert: false,
},
];
const nodeId = getScriptNodeId(script);
for (const testCase of testCases) {
it(testCase.name, () => {
const selection = new UserSelection(app, testCase.selection);
const sut = new ScriptReverter(nodeId);
// act
sut.selectWithRevertState(testCase.revert, selection);
// assert
expect(selection.isSelected(script.id)).to.equal(true);
expect(selection.selectedScripts[0].revert).equal(testCase.expectRevert);
});
}
});
});

View File

@@ -8,7 +8,7 @@ export class ApplicationStub implements IApplication {
public readonly version = '0.1.0';
public readonly actions = new Array<ICategory>();
public withAction(category: ICategory): IApplication {
public withAction(category: ICategory): ApplicationStub {
this.actions.push(category);
return this;
}