From f51e8859eeb32c944126d692cfe03a0320c8b568 Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Tue, 1 Sep 2020 21:18:16 +0100 Subject: [PATCH] add reversibility on category level --- src/application/State/ApplicationState.ts | 3 +- .../State/Selection/IUserSelection.ts | 2 +- .../State/Selection/UserSelection.ts | 23 +- src/infrastructure/Repository/IRepository.ts | 1 + .../Repository/InMemoryRepository.ts | 8 + .../Scripts/ScriptsTree/ScriptNodeParser.ts | 2 +- .../Scripts/ScriptsTree/ScriptsTree.vue | 41 ++-- .../SelectableTree/LiquorTree/LiquorTree.d.ts | 4 +- .../ScriptsTree/SelectableTree/Node/Node.vue | 2 +- .../SelectableTree/Node/RevertToggle.vue | 206 +++++++++--------- .../Node/Reverter/CategoryReverter.ts | 30 +++ .../SelectableTree/Node/Reverter/IReverter.ts | 7 + .../Node/Reverter/ReverterFactory.ts | 16 ++ .../Node/Reverter/ScriptReverter.ts | 21 ++ .../SelectableTree/SelectableTree.vue | 2 +- .../State/Code/ApplicationCode.spec.ts | 8 +- .../State/Selection/UserSelection.spec.ts | 120 ++++++++-- .../infrastructure/InMemoryRepository.spec.ts | 22 ++ .../ScriptsTree/ScriptNodeParser.spec.ts | 120 ++++++++++ .../Node/Reverter/CategoryReverter.spec.ts | 106 +++++++++ .../Node/Reverter/ReverterFactory.spec.ts | 45 ++++ .../Node/Reverter/ScriptReverter.spec.ts | 88 ++++++++ tests/unit/stubs/ApplicationStub.ts | 2 +- 23 files changed, 717 insertions(+), 162 deletions(-) create mode 100644 src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter.ts create mode 100644 src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/IReverter.ts create mode 100644 src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ReverterFactory.ts create mode 100644 src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter.ts create mode 100644 tests/unit/presentation/Scripts/ScriptsTree/ScriptNodeParser.spec.ts create mode 100644 tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter.spec.ts create mode 100644 tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ReverterFactory.spec.ts create mode 100644 tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter.spec.ts diff --git a/src/application/State/ApplicationState.ts b/src/application/State/ApplicationState.ts index 1e82b496..0d09e824 100644 --- a/src/application/State/ApplicationState.ts +++ b/src/application/State/ApplicationState.ts @@ -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); } diff --git a/src/application/State/Selection/IUserSelection.ts b/src/application/State/Selection/IUserSelection.ts index c7bb4a59..a97aada9 100644 --- a/src/application/State/Selection/IUserSelection.ts +++ b/src/application/State/Selection/IUserSelection.ts @@ -7,7 +7,7 @@ export interface IUserSelection { readonly selectedScripts: ReadonlyArray; 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; diff --git a/src/application/State/Selection/UserSelection.ts b/src/application/State/Selection/UserSelection.ts index e798ce10..bb750524 100644 --- a/src/application/State/Selection/UserSelection.ts +++ b/src/application/State/Selection/UserSelection.ts @@ -12,13 +12,11 @@ export class UserSelection implements IUserSelection { constructor( private readonly app: IApplication, - /** Initially selected scripts */ - selectedScripts: ReadonlyArray) { + selectedScripts: ReadonlyArray) { this.scripts = new InMemoryRepository(); 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()); } diff --git a/src/infrastructure/Repository/IRepository.ts b/src/infrastructure/Repository/IRepository.ts index a53b9d85..53f68dba 100644 --- a/src/infrastructure/Repository/IRepository.ts +++ b/src/infrastructure/Repository/IRepository.ts @@ -3,6 +3,7 @@ import { IEntity } from '../Entity/IEntity'; export interface IRepository> { 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; diff --git a/src/infrastructure/Repository/InMemoryRepository.ts b/src/infrastructure/Repository/InMemoryRepository.ts index 0ead08ce..261e8735 100644 --- a/src/infrastructure/Repository/InMemoryRepository.ts +++ b/src/infrastructure/Repository/InMemoryRepository.ts @@ -16,6 +16,14 @@ export class InMemoryRepository> 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'); diff --git a/src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts b/src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts index d05a0b86..7e8742c8 100644 --- a/src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts +++ b/src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts @@ -74,7 +74,7 @@ function convertCategoryToNode( text: category.name, children, documentationUrls: category.documentationUrls, - isReversible: false, + isReversible: children && children.every((child) => child.isReversible), }; } diff --git a/src/presentation/Scripts/ScriptsTree/ScriptsTree.vue b/src/presentation/Scripts/ScriptsTree/ScriptsTree.vue index 96fa3664..edfcab64 100644 --- a/src/presentation/Scripts/ScriptsTree/ScriptsTree.vue +++ b/src/presentation/Scripts/ScriptsTree/ScriptsTree.vue @@ -7,7 +7,6 @@ :filterPredicate="filterPredicate" :filterText="filterText" v-on:nodeSelected="toggleNodeSelectionAsync($event)" - v-on:nodeRevertToggled="handleNodeRevertToggleAsync($event)" > @@ -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); + } + } \ No newline at end of file diff --git a/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter.ts b/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter.ts new file mode 100644 index 00000000..abf6cc33 --- /dev/null +++ b/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter.ts @@ -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; + constructor(nodeId: string, app: IApplication) { + this.categoryId = getCategoryId(nodeId); + this.scriptReverters = getAllSubScriptReverters(this.categoryId, app); + } + public getState(selectedScripts: ReadonlyArray): 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)); +} diff --git a/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/IReverter.ts b/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/IReverter.ts new file mode 100644 index 00000000..8df201c6 --- /dev/null +++ b/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/IReverter.ts @@ -0,0 +1,7 @@ +import { SelectedScript } from '@/application/State/Selection/SelectedScript'; +import { IUserSelection } from '@/application/State/IApplicationState'; + +export interface IReverter { + getState(selectedScripts: ReadonlyArray): boolean; + selectWithRevertState(newState: boolean, selection: IUserSelection): void; +} diff --git a/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ReverterFactory.ts b/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ReverterFactory.ts new file mode 100644 index 00000000..dfed5e5a --- /dev/null +++ b/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ReverterFactory.ts @@ -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'); + } +} diff --git a/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter.ts b/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter.ts new file mode 100644 index 00000000..4c2f9075 --- /dev/null +++ b/src/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter.ts @@ -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): 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); + } +} diff --git a/src/presentation/Scripts/ScriptsTree/SelectableTree/SelectableTree.vue b/src/presentation/Scripts/ScriptsTree/SelectableTree/SelectableTree.vue index 19b827b0..6b9a4b4d 100644 --- a/src/presentation/Scripts/ScriptsTree/SelectableTree/SelectableTree.vue +++ b/src/presentation/Scripts/ScriptsTree/SelectableTree/SelectableTree.vue @@ -7,7 +7,7 @@ v-on:node:unchecked="nodeSelected($event)" ref="treeElement" > - + diff --git a/tests/unit/application/State/Code/ApplicationCode.spec.ts b/tests/unit/application/State/Code/ApplicationCode.spec.ts index aab7703d..7d817429 100644 --- a/tests/unit/application/State/Code/ApplicationCode.spec.ts +++ b/tests/unit/application/State/Code/ApplicationCode.spec.ts @@ -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; diff --git a/tests/unit/application/State/Selection/UserSelection.spec.ts b/tests/unit/application/State/Selection/UserSelection.spec.ts index 4ce41e86..420f0ab7 100644 --- a/tests/unit/application/State/Selection/UserSelection.spec.ts +++ b/tests/unit/application/State/Selection/UserSelection.spec.ts @@ -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 = []; 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 = []; @@ -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 diff --git a/tests/unit/infrastructure/InMemoryRepository.spec.ts b/tests/unit/infrastructure/InMemoryRepository.spec.ts index e01e26b0..334f9532 100644 --- a/tests/unit/infrastructure/InMemoryRepository.spec.ts +++ b/tests/unit/infrastructure/InMemoryRepository.spec.ts @@ -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([ + 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([]); + // act + const actual = sut.getById(31); + // assert + expect(actual).to.equal(undefined); + }); + }); }); diff --git a/tests/unit/presentation/Scripts/ScriptsTree/ScriptNodeParser.spec.ts b/tests/unit/presentation/Scripts/ScriptsTree/ScriptNodeParser.spec.ts new file mode 100644 index 00000000..ee566184 --- /dev/null +++ b/tests/unit/presentation/Scripts/ScriptsTree/ScriptNodeParser.spec.ts @@ -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)}`; + } +} diff --git a/tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter.spec.ts b/tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter.spec.ts new file mode 100644 index 00000000..fe47af32 --- /dev/null +++ b/tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/CategoryReverter.spec.ts @@ -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); + }); + } + }); +}); diff --git a/tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ReverterFactory.spec.ts b/tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ReverterFactory.spec.ts new file mode 100644 index 00000000..415dcd2f --- /dev/null +++ b/tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ReverterFactory.spec.ts @@ -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, + }; + } +}); diff --git a/tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter.spec.ts b/tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter.spec.ts new file mode 100644 index 00000000..fb05b916 --- /dev/null +++ b/tests/unit/presentation/Scripts/ScriptsTree/SelectableTree/Node/Reverter/ScriptReverter.spec.ts @@ -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); + }); + } + }); +}); diff --git a/tests/unit/stubs/ApplicationStub.ts b/tests/unit/stubs/ApplicationStub.ts index adc507ef..7631edba 100644 --- a/tests/unit/stubs/ApplicationStub.ts +++ b/tests/unit/stubs/ApplicationStub.ts @@ -8,7 +8,7 @@ export class ApplicationStub implements IApplication { public readonly version = '0.1.0'; public readonly actions = new Array(); - public withAction(category: ICategory): IApplication { + public withAction(category: ICategory): ApplicationStub { this.actions.push(category); return this; }