added ability to revert (#21)
This commit is contained in:
@@ -1,12 +1,115 @@
|
||||
import applicationFile from 'js-yaml-loader!@/application/application.yaml';
|
||||
import { IEntity } from '@/infrastructure/Entity/IEntity';
|
||||
import applicationFile, { YamlCategory, YamlScript, ApplicationYaml } from 'js-yaml-loader!@/application/application.yaml';
|
||||
import { parseApplication } from '@/application/Parser/ApplicationParser';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { parseCategory } from '@/application/Parser/CategoryParser';
|
||||
|
||||
declare var process;
|
||||
|
||||
describe('ApplicationParser', () => {
|
||||
describe('parseApplication', () => {
|
||||
it('can parse current application file', () => {
|
||||
expect(() => parseApplication(applicationFile)).to.not.throw();
|
||||
});
|
||||
it('throws when undefined', () => {
|
||||
expect(() => parseApplication(undefined)).to.throw('application is null or undefined');
|
||||
});
|
||||
it('throws when undefined actions', () => {
|
||||
const sut: ApplicationYaml = {
|
||||
name: 'test',
|
||||
repositoryUrl: 'https://privacy.sexy',
|
||||
actions: undefined,
|
||||
};
|
||||
expect(() => parseApplication(sut)).to.throw('application does not define any action');
|
||||
});
|
||||
it('throws when has no actions', () => {
|
||||
const sut: ApplicationYaml = {
|
||||
name: 'test',
|
||||
repositoryUrl: 'https://privacy.sexy',
|
||||
actions: [],
|
||||
};
|
||||
expect(() => parseApplication(sut)).to.throw('application does not define any action');
|
||||
});
|
||||
it('returns expected name', () => {
|
||||
// arrange
|
||||
const expected = 'test-app-name';
|
||||
const sut: ApplicationYaml = {
|
||||
name: expected,
|
||||
repositoryUrl: 'https://privacy.sexy',
|
||||
actions: [ getTestCategory() ],
|
||||
};
|
||||
// act
|
||||
const actual = parseApplication(sut).name;
|
||||
// assert
|
||||
expect(actual).to.be.equal(actual);
|
||||
});
|
||||
it('returns expected repository url', () => {
|
||||
// arrange
|
||||
const expected = 'https://privacy.sexy';
|
||||
const sut: ApplicationYaml = {
|
||||
name: 'name',
|
||||
repositoryUrl: expected,
|
||||
actions: [ getTestCategory() ],
|
||||
};
|
||||
// act
|
||||
const actual = parseApplication(sut).repositoryUrl;
|
||||
// assert
|
||||
expect(actual).to.be.equal(actual);
|
||||
});
|
||||
it('returns expected repository version', () => {
|
||||
// arrange
|
||||
const expected = '1.0.0';
|
||||
process = {
|
||||
env: {
|
||||
VUE_APP_VERSION: expected,
|
||||
},
|
||||
};
|
||||
const sut: ApplicationYaml = {
|
||||
name: 'name',
|
||||
repositoryUrl: 'https://privacy.sexy',
|
||||
actions: [ getTestCategory() ],
|
||||
};
|
||||
// act
|
||||
const actual = parseApplication(sut).version;
|
||||
// assert
|
||||
expect(actual).to.be.equal(actual);
|
||||
});
|
||||
it('parses actions', () => {
|
||||
// arrange
|
||||
const actions = [ getTestCategory('test1'), getTestCategory('test2') ];
|
||||
const expected = [ parseCategory(actions[0]), parseCategory(actions[1]) ];
|
||||
const sut: ApplicationYaml = {
|
||||
name: 'name',
|
||||
repositoryUrl: 'https://privacy.sexy',
|
||||
actions,
|
||||
};
|
||||
// act
|
||||
const actual = parseApplication(sut).actions;
|
||||
// assert
|
||||
expect(excludingId(actual)).to.be.deep.equal(excludingId(expected));
|
||||
function excludingId<TId>(array: ReadonlyArray<IEntity<TId>>) {
|
||||
return array.map((obj) => {
|
||||
const { ['id']: omitted, ...rest } = obj;
|
||||
return rest;
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function getTestCategory(scriptName = 'testScript'): YamlCategory {
|
||||
return {
|
||||
category: 'category name',
|
||||
children: [ getTestScript(scriptName) ],
|
||||
};
|
||||
}
|
||||
|
||||
function getTestScript(scriptName: string): YamlScript {
|
||||
return {
|
||||
name: scriptName,
|
||||
code: 'script code',
|
||||
revertCode: 'revert code',
|
||||
recommend: true,
|
||||
};
|
||||
}
|
||||
|
||||
109
tests/unit/application/Parser/CategoryParser.spec.ts
Normal file
109
tests/unit/application/Parser/CategoryParser.spec.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { parseCategory } from '@/application/Parser/CategoryParser';
|
||||
import { YamlCategory, CategoryOrScript, YamlScript } from 'js-yaml-loader!./application.yaml';
|
||||
import { parseScript } from '@/application/Parser/ScriptParser';
|
||||
import { parseDocUrls } from '@/application/Parser/DocumentationParser';
|
||||
|
||||
describe('CategoryParser', () => {
|
||||
describe('parseCategory', () => {
|
||||
|
||||
it('throws when undefined', () => {
|
||||
expect(() => parseCategory(undefined)).to.throw('category is null or undefined');
|
||||
});
|
||||
|
||||
it('throws when children is empty', () => {
|
||||
const category: YamlCategory = {
|
||||
category: 'test',
|
||||
children: [],
|
||||
};
|
||||
expect(() => parseCategory(category)).to.throw('category has no children');
|
||||
});
|
||||
|
||||
it('throws when children is undefined', () => {
|
||||
const category: YamlCategory = {
|
||||
category: 'test',
|
||||
children: undefined,
|
||||
};
|
||||
expect(() => parseCategory(category)).to.throw('category has no children');
|
||||
});
|
||||
|
||||
it('throws when name is empty', () => {
|
||||
const category: YamlCategory = {
|
||||
category: '',
|
||||
children: getTestChildren(),
|
||||
};
|
||||
expect(() => parseCategory(category)).to.throw('category has no name');
|
||||
});
|
||||
|
||||
it('throws when name is undefined', () => {
|
||||
const category: YamlCategory = {
|
||||
category: undefined,
|
||||
children: getTestChildren(),
|
||||
};
|
||||
expect(() => parseCategory(category)).to.throw('category has no name');
|
||||
});
|
||||
|
||||
it('returns expected docs', () => {
|
||||
// arrange
|
||||
const url = 'https://privacy.sexy';
|
||||
const expected = parseDocUrls({ docs: url });
|
||||
const category: YamlCategory = {
|
||||
category: 'category name',
|
||||
children: getTestChildren(),
|
||||
docs: url,
|
||||
};
|
||||
// act
|
||||
const actual = parseCategory(category).documentationUrls;
|
||||
// assert
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
|
||||
it('returns expected scripts', () => {
|
||||
// arrange
|
||||
const script = getTestScript();
|
||||
const expected = [ parseScript(script) ];
|
||||
const category: YamlCategory = {
|
||||
category: 'category name',
|
||||
children: [ script ],
|
||||
};
|
||||
// act
|
||||
const actual = parseCategory(category).scripts;
|
||||
// assert
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
|
||||
it('returns expected subcategories', () => {
|
||||
// arrange
|
||||
const expected: YamlCategory[] = [ {
|
||||
category: 'test category',
|
||||
children: [ getTestScript() ],
|
||||
}];
|
||||
const category: YamlCategory = {
|
||||
category: 'category name',
|
||||
children: expected,
|
||||
};
|
||||
// act
|
||||
const actual = parseCategory(category).subCategories;
|
||||
// assert
|
||||
expect(actual).to.have.lengthOf(1);
|
||||
expect(actual[0].name).to.equal(expected[0].category);
|
||||
expect(actual[0].scripts.length).to.equal(expected[0].children.length);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function getTestChildren(): ReadonlyArray<CategoryOrScript> {
|
||||
return [
|
||||
getTestScript(),
|
||||
];
|
||||
}
|
||||
|
||||
function getTestScript(): YamlScript {
|
||||
return {
|
||||
name: 'script name',
|
||||
code: 'script code',
|
||||
revertCode: 'revert code',
|
||||
recommend: true,
|
||||
};
|
||||
}
|
||||
39
tests/unit/application/Parser/DocumentationParser.spec.ts
Normal file
39
tests/unit/application/Parser/DocumentationParser.spec.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { YamlDocumentable } from 'js-yaml-loader!./application.yaml';
|
||||
import { parseDocUrls } from '@/application/Parser/DocumentationParser';
|
||||
|
||||
describe('DocumentationParser', () => {
|
||||
describe('parseDocUrls', () => {
|
||||
it('throws when undefined', () => {
|
||||
expect(() => parseDocUrls(undefined)).to.throw('documentable is null or undefined');
|
||||
});
|
||||
it('returns empty when empty', () => {
|
||||
// arrange
|
||||
const empty: YamlDocumentable = { };
|
||||
// act
|
||||
const actual = parseDocUrls(empty);
|
||||
// assert
|
||||
expect(actual).to.have.lengthOf(0);
|
||||
});
|
||||
it('returns single item when string', () => {
|
||||
// arrange
|
||||
const url = 'https://privacy.sexy';
|
||||
const expected = [ url ];
|
||||
const sut: YamlDocumentable = { docs: url };
|
||||
// act
|
||||
const actual = parseDocUrls(sut);
|
||||
// assert
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
it('returns all when array', () => {
|
||||
// arrange
|
||||
const expected = [ 'https://privacy.sexy', 'https://github.com/undergroundwires/privacy.sexy' ];
|
||||
const sut: YamlDocumentable = { docs: expected };
|
||||
// act
|
||||
const actual = parseDocUrls(sut);
|
||||
// assert
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
28
tests/unit/application/Parser/ScriptParser.spec.ts
Normal file
28
tests/unit/application/Parser/ScriptParser.spec.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { YamlScript } from 'js-yaml-loader!./application.yaml';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { parseScript } from '@/application/Parser/ScriptParser';
|
||||
import { parseDocUrls } from '@/application/Parser/DocumentationParser';
|
||||
|
||||
describe('ScriptParser', () => {
|
||||
describe('parseScript', () => {
|
||||
it('parseScript parses as expected', () => {
|
||||
// arrange
|
||||
const expected: YamlScript = {
|
||||
name: 'expected name',
|
||||
code: 'expected code',
|
||||
revertCode: 'expected revert code',
|
||||
docs: ['hello.com'],
|
||||
recommend: true,
|
||||
};
|
||||
// act
|
||||
const actual = parseScript(expected);
|
||||
// assert
|
||||
expect(actual.name).to.equal(expected.name);
|
||||
expect(actual.code).to.equal(expected.code);
|
||||
expect(actual.revertCode).to.equal(expected.revertCode);
|
||||
expect(actual.documentationUrls).to.deep.equal(parseDocUrls(expected));
|
||||
expect(actual.isRecommended).to.equal(expected.recommend);
|
||||
});
|
||||
});
|
||||
});
|
||||
65
tests/unit/application/State/Code/ApplicationCode.spec.ts
Normal file
65
tests/unit/application/State/Code/ApplicationCode.spec.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { CategoryStub } from './../../../stubs/CategoryStub';
|
||||
import { ScriptStub } from './../../../stubs/ScriptStub';
|
||||
import { ApplicationStub } from './../../../stubs/ApplicationStub';
|
||||
import { UserSelection } from '@/application/State/Selection/UserSelection';
|
||||
import { ApplicationCode } from '@/application/State/Code/ApplicationCode';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
|
||||
|
||||
describe('ApplicationCode', () => {
|
||||
describe('ctor', () => {
|
||||
it('empty when selection is empty', () => {
|
||||
// arrange
|
||||
const selection = new UserSelection(new ApplicationStub(), []);
|
||||
const sut = new ApplicationCode(selection, 'version');
|
||||
// act
|
||||
const actual = sut.current;
|
||||
// assert
|
||||
expect(actual).to.have.lengthOf(0);
|
||||
});
|
||||
it('has code when selection is not empty', () => {
|
||||
// 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 version = 'version-string';
|
||||
const sut = new ApplicationCode(selection, version);
|
||||
// act
|
||||
const actual = sut.current;
|
||||
// assert
|
||||
expect(actual).to.have.length.greaterThan(0).and.include(version);
|
||||
});
|
||||
});
|
||||
describe('user selection changes', () => {
|
||||
it('empty when selection is empty', () => {
|
||||
// arrange
|
||||
let signaled: string;
|
||||
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 sut = new ApplicationCode(selection, 'version');
|
||||
sut.changed.on((code) => signaled = code);
|
||||
// act
|
||||
selection.changed.notify([]);
|
||||
// assert
|
||||
expect(signaled).to.have.lengthOf(0);
|
||||
expect(signaled).to.equal(sut.current);
|
||||
});
|
||||
it('has code when selection is not empty', () => {
|
||||
// arrange
|
||||
let signaled: string;
|
||||
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 version = 'version-string';
|
||||
const sut = new ApplicationCode(selection, version);
|
||||
sut.changed.on((code) => signaled = code);
|
||||
// act
|
||||
selection.changed.notify(scripts.map((s) => new SelectedScript(s, false)));
|
||||
// assert
|
||||
expect(signaled).to.have.length.greaterThan(0).and.include(version);
|
||||
expect(signaled).to.equal(sut.current);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
import { ScriptStub } from './../../../stubs/ScriptStub';
|
||||
import { UserScriptGenerator, adminRightsScript } from '@/application/State/Code/UserScriptGenerator';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
|
||||
|
||||
describe('UserScriptGenerator', () => {
|
||||
it('adds version', () => {
|
||||
const sut = new UserScriptGenerator();
|
||||
// arrange
|
||||
const version = '1.5.0';
|
||||
const selectedScripts = [ new SelectedScript(new ScriptStub('id'), false)];
|
||||
// act
|
||||
const actual = sut.buildCode(selectedScripts, version);
|
||||
// assert
|
||||
expect(actual).to.include(version);
|
||||
});
|
||||
it('adds admin rights function', () => {
|
||||
const sut = new UserScriptGenerator();
|
||||
// arrange
|
||||
const selectedScripts = [ new SelectedScript(new ScriptStub('id'), false)];
|
||||
// act
|
||||
const actual = sut.buildCode(selectedScripts, 'non-important-version');
|
||||
// assert
|
||||
expect(actual).to.include(adminRightsScript.code);
|
||||
expect(actual).to.include(adminRightsScript.name);
|
||||
});
|
||||
it('appends revert script', () => {
|
||||
const sut = new UserScriptGenerator();
|
||||
// arrange
|
||||
const scriptName = 'test non-revert script';
|
||||
const scriptCode = 'REM nop';
|
||||
const script = new ScriptStub('id').withName(scriptName).withRevertCode(scriptCode);
|
||||
const selectedScripts = [ new SelectedScript(script, true)];
|
||||
// act
|
||||
const actual = sut.buildCode(selectedScripts, 'non-important-version');
|
||||
// assert
|
||||
expect(actual).to.include(`${scriptName} (revert)`);
|
||||
expect(actual).to.include(scriptCode);
|
||||
});
|
||||
it('appends non-revert script', () => {
|
||||
const sut = new UserScriptGenerator();
|
||||
// arrange
|
||||
const scriptName = 'test non-revert script';
|
||||
const scriptCode = 'REM nop';
|
||||
const script = new ScriptStub('id').withName(scriptName).withCode(scriptCode);
|
||||
const selectedScripts = [ new SelectedScript(script, false)];
|
||||
// act
|
||||
const actual = sut.buildCode(selectedScripts, 'non-important-version');
|
||||
// assert
|
||||
expect(actual).to.include(scriptName);
|
||||
expect(actual).to.include(scriptCode);
|
||||
});
|
||||
});
|
||||
46
tests/unit/application/State/Filter/FilterResult.spec.ts
Normal file
46
tests/unit/application/State/Filter/FilterResult.spec.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { CategoryStub } from './../../../stubs/CategoryStub';
|
||||
import { ScriptStub } from './../../../stubs/ScriptStub';
|
||||
import { FilterResult } from '@/application/State/Filter/FilterResult';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('FilterResult', () => {
|
||||
describe('hasAnyMatches', () => {
|
||||
it('false when no matches', () => {
|
||||
const sut = new FilterResult(
|
||||
/* scriptMatches */ [],
|
||||
/* categoryMatches */ [],
|
||||
'query',
|
||||
);
|
||||
const actual = sut.hasAnyMatches();
|
||||
expect(actual).to.equal(false);
|
||||
});
|
||||
it('true when script matches', () => {
|
||||
const sut = new FilterResult(
|
||||
/* scriptMatches */ [ new ScriptStub('id') ],
|
||||
/* categoryMatches */ [],
|
||||
'query',
|
||||
);
|
||||
const actual = sut.hasAnyMatches();
|
||||
expect(actual).to.equal(true);
|
||||
});
|
||||
it('true when category matches', () => {
|
||||
const sut = new FilterResult(
|
||||
/* scriptMatches */ [ ],
|
||||
/* categoryMatches */ [ new CategoryStub(5) ],
|
||||
'query',
|
||||
);
|
||||
const actual = sut.hasAnyMatches();
|
||||
expect(actual).to.equal(true);
|
||||
});
|
||||
it('true when script + category matches', () => {
|
||||
const sut = new FilterResult(
|
||||
/* scriptMatches */ [ new ScriptStub('id') ],
|
||||
/* categoryMatches */ [ new CategoryStub(5) ],
|
||||
'query',
|
||||
);
|
||||
const actual = sut.hasAnyMatches();
|
||||
expect(actual).to.equal(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
135
tests/unit/application/State/Filter/UserFilter.spec.ts
Normal file
135
tests/unit/application/State/Filter/UserFilter.spec.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import { CategoryStub } from './../../../stubs/CategoryStub';
|
||||
import { ScriptStub } from './../../../stubs/ScriptStub';
|
||||
import { IFilterResult } from '@/application/State/Filter/IFilterResult';
|
||||
import { ApplicationStub } from './../../../stubs/ApplicationStub';
|
||||
import { UserFilter } from '@/application/State/Filter/UserFilter';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('UserFilter', () => {
|
||||
it('signals when removing filter', () => {
|
||||
// arrange
|
||||
let isCalled = false;
|
||||
const sut = new UserFilter(new ApplicationStub());
|
||||
sut.filterRemoved.on(() => isCalled = true);
|
||||
// act
|
||||
sut.removeFilter();
|
||||
// assert
|
||||
expect(isCalled).to.be.equal(true);
|
||||
});
|
||||
it('signals when no matches', () => {
|
||||
// arrange
|
||||
let actual: IFilterResult;
|
||||
const nonMatchingFilter = 'non matching filter';
|
||||
const sut = new UserFilter(new ApplicationStub());
|
||||
sut.filtered.on((filterResult) => actual = filterResult);
|
||||
// act
|
||||
sut.setFilter(nonMatchingFilter);
|
||||
// assert
|
||||
expect(actual.hasAnyMatches()).be.equal(false);
|
||||
expect(actual.categoryMatches).to.have.lengthOf(0);
|
||||
expect(actual.scriptMatches).to.have.lengthOf(0);
|
||||
expect(actual.query).to.equal(nonMatchingFilter);
|
||||
});
|
||||
describe('signals when script matches', () => {
|
||||
it('code matches', () => {
|
||||
// arrange
|
||||
const code = 'HELLO world';
|
||||
const filter = 'Hello WoRLD';
|
||||
let actual: IFilterResult;
|
||||
const script = new ScriptStub('id').withCode(code);
|
||||
const category = new CategoryStub(33).withScript(script);
|
||||
const sut = new UserFilter(new ApplicationStub()
|
||||
.withAction(category));
|
||||
sut.filtered.on((filterResult) => actual = filterResult);
|
||||
// act
|
||||
sut.setFilter(filter);
|
||||
// assert
|
||||
expect(actual.hasAnyMatches()).be.equal(true);
|
||||
expect(actual.categoryMatches).to.have.lengthOf(0);
|
||||
expect(actual.scriptMatches).to.have.lengthOf(1);
|
||||
expect(actual.scriptMatches[0]).to.deep.equal(script);
|
||||
expect(actual.query).to.equal(filter);
|
||||
});
|
||||
it('revertCode matches', () => {
|
||||
// arrange
|
||||
const revertCode = 'HELLO world';
|
||||
const filter = 'Hello WoRLD';
|
||||
let actual: IFilterResult;
|
||||
const script = new ScriptStub('id').withRevertCode(revertCode);
|
||||
const category = new CategoryStub(33).withScript(script);
|
||||
const sut = new UserFilter(new ApplicationStub()
|
||||
.withAction(category));
|
||||
sut.filtered.on((filterResult) => actual = filterResult);
|
||||
// act
|
||||
sut.setFilter(filter);
|
||||
// assert
|
||||
expect(actual.hasAnyMatches()).be.equal(true);
|
||||
expect(actual.categoryMatches).to.have.lengthOf(0);
|
||||
expect(actual.scriptMatches).to.have.lengthOf(1);
|
||||
expect(actual.scriptMatches[0]).to.deep.equal(script);
|
||||
expect(actual.query).to.equal(filter);
|
||||
});
|
||||
it('name matches', () => {
|
||||
// arrange
|
||||
const name = 'HELLO world';
|
||||
const filter = 'Hello WoRLD';
|
||||
let actual: IFilterResult;
|
||||
const script = new ScriptStub('id').withName(name);
|
||||
const category = new CategoryStub(33).withScript(script);
|
||||
const sut = new UserFilter(new ApplicationStub()
|
||||
.withAction(category));
|
||||
sut.filtered.on((filterResult) => actual = filterResult);
|
||||
// act
|
||||
sut.setFilter(filter);
|
||||
// assert
|
||||
expect(actual.hasAnyMatches()).be.equal(true);
|
||||
expect(actual.categoryMatches).to.have.lengthOf(0);
|
||||
expect(actual.scriptMatches).to.have.lengthOf(1);
|
||||
expect(actual.scriptMatches[0]).to.deep.equal(script);
|
||||
expect(actual.query).to.equal(filter);
|
||||
});
|
||||
});
|
||||
it('signals when category matches', () => {
|
||||
// arrange
|
||||
const categoryName = 'HELLO world';
|
||||
const filter = 'Hello WoRLD';
|
||||
let actual: IFilterResult;
|
||||
const category = new CategoryStub(55).withName(categoryName);
|
||||
const sut = new UserFilter(new ApplicationStub()
|
||||
.withAction(category));
|
||||
sut.filtered.on((filterResult) => actual = filterResult);
|
||||
// act
|
||||
sut.setFilter(filter);
|
||||
// assert
|
||||
expect(actual.hasAnyMatches()).be.equal(true);
|
||||
expect(actual.categoryMatches).to.have.lengthOf(1);
|
||||
expect(actual.categoryMatches[0]).to.deep.equal(category);
|
||||
expect(actual.scriptMatches).to.have.lengthOf(0);
|
||||
expect(actual.query).to.equal(filter);
|
||||
});
|
||||
it('signals when category and script matches', () => {
|
||||
// arrange
|
||||
const matchingText = 'HELLO world';
|
||||
const filter = 'Hello WoRLD';
|
||||
let actual: IFilterResult;
|
||||
const script = new ScriptStub('script')
|
||||
.withName(matchingText);
|
||||
const category = new CategoryStub(55)
|
||||
.withName(matchingText)
|
||||
.withScript(script);
|
||||
const app = new ApplicationStub()
|
||||
.withAction(category);
|
||||
const sut = new UserFilter(app);
|
||||
sut.filtered.on((filterResult) => actual = filterResult);
|
||||
// act
|
||||
sut.setFilter(filter);
|
||||
// assert
|
||||
expect(actual.hasAnyMatches()).be.equal(true);
|
||||
expect(actual.categoryMatches).to.have.lengthOf(1);
|
||||
expect(actual.categoryMatches[0]).to.deep.equal(category);
|
||||
expect(actual.scriptMatches).to.have.lengthOf(1);
|
||||
expect(actual.scriptMatches[0]).to.deep.equal(script);
|
||||
expect(actual.query).to.equal(filter);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import { ScriptStub } from './../../../stubs/ScriptStub';
|
||||
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('SelectedScript', () => {
|
||||
it('id is same as script id', () => {
|
||||
// arrange
|
||||
const expectedId = 'scriptId';
|
||||
const script = new ScriptStub(expectedId);
|
||||
const sut = new SelectedScript(script, false);
|
||||
// act
|
||||
const actualId = sut.id;
|
||||
// assert
|
||||
expect(actualId).to.equal(expectedId);
|
||||
});
|
||||
it('throws when revert is true for irreversible script', () => {
|
||||
// arrange
|
||||
const expectedId = 'scriptId';
|
||||
const script = new ScriptStub(expectedId)
|
||||
.withRevertCode(undefined);
|
||||
// act
|
||||
function construct() { new SelectedScript(script, true); } // tslint:disable-line:no-unused-expression
|
||||
// assert
|
||||
expect(construct).to.throw('cannot revert an irreversible script');
|
||||
});
|
||||
|
||||
});
|
||||
96
tests/unit/application/State/Selection/UserSelection.spec.ts
Normal file
96
tests/unit/application/State/Selection/UserSelection.spec.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
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', () => {
|
||||
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 sut = new UserSelection(app, selectedScripts);
|
||||
sut.changed.on((newScripts) => events.push(newScripts));
|
||||
// act
|
||||
sut.deselectAll();
|
||||
// assert
|
||||
expect(sut.selectedScripts).to.have.length(0);
|
||||
expect(events).to.have.lengthOf(1);
|
||||
expect(events[0]).to.have.length(0);
|
||||
});
|
||||
it('selectOnly selects expected', () => {
|
||||
// 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 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));
|
||||
// act
|
||||
sut.selectOnly(scripts);
|
||||
// assert
|
||||
expect(sut.selectedScripts).to.deep.equal(expected);
|
||||
expect(events).to.have.lengthOf(1);
|
||||
expect(events[0]).to.deep.equal(expected);
|
||||
});
|
||||
it('selectAll selects as expected', () => {
|
||||
// arrange
|
||||
const events: Array<readonly SelectedScript[]> = [];
|
||||
const scripts: IScript[] = [new ScriptStub('s1'), new ScriptStub('s2'), new ScriptStub('s3'), new ScriptStub('s4')];
|
||||
const app = new ApplicationStub()
|
||||
.withAction(new CategoryStub(1)
|
||||
.withScripts(...scripts));
|
||||
const sut = new UserSelection(app, []);
|
||||
sut.changed.on((newScripts) => events.push(newScripts));
|
||||
const expected = scripts.map((script) => new SelectedScript(script, false));
|
||||
// act
|
||||
sut.selectAll();
|
||||
// assert
|
||||
expect(sut.selectedScripts).to.deep.equal(expected);
|
||||
expect(events).to.have.lengthOf(1);
|
||||
expect(events[0]).to.deep.equal(expected);
|
||||
});
|
||||
describe('addOrUpdateSelectedScript', () => {
|
||||
it('adds when item does not exist', () => {
|
||||
// arrange
|
||||
const events: Array<readonly SelectedScript[]> = [];
|
||||
const app = new ApplicationStub()
|
||||
.withAction(new CategoryStub(1)
|
||||
.withScripts(new ScriptStub('s1'), new ScriptStub('s2')));
|
||||
const sut = new UserSelection(app, []);
|
||||
sut.changed.on((scripts) => events.push(scripts));
|
||||
const expected = [ new SelectedScript(new ScriptStub('s1'), false) ];
|
||||
// act
|
||||
sut.addOrUpdateSelectedScript('s1', false);
|
||||
// assert
|
||||
expect(sut.selectedScripts).to.deep.equal(expected);
|
||||
expect(events).to.have.lengthOf(1);
|
||||
expect(events[0]).to.deep.equal(expected);
|
||||
});
|
||||
it('updates when item exists', () => {
|
||||
// arrange
|
||||
const events: Array<readonly SelectedScript[]> = [];
|
||||
const app = new ApplicationStub()
|
||||
.withAction(new CategoryStub(1)
|
||||
.withScripts(new ScriptStub('s1'), new ScriptStub('s2')));
|
||||
const sut = new UserSelection(app, []);
|
||||
sut.changed.on((scripts) => events.push(scripts));
|
||||
const expected = [ new SelectedScript(new ScriptStub('s1'), true) ];
|
||||
// act
|
||||
sut.addOrUpdateSelectedScript('s1', true);
|
||||
// assert
|
||||
expect(sut.selectedScripts).to.deep.equal(expected);
|
||||
expect(events).to.have.lengthOf(1);
|
||||
expect(events[0]).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,41 +0,0 @@
|
||||
import { CategoryStub } from './../stubs/CategoryStub';
|
||||
import { ApplicationStub } from './../stubs/ApplicationStub';
|
||||
import { ScriptStub } from './../stubs/ScriptStub';
|
||||
import { UserSelection } from '@/application/State/Selection/UserSelection';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
|
||||
|
||||
describe('UserSelection', () => {
|
||||
it('deselectAll removes all items', async () => {
|
||||
// arrange
|
||||
const app = new ApplicationStub()
|
||||
.withCategory(new CategoryStub(1)
|
||||
.withScriptIds('s1', 's2', 's3', 's4'));
|
||||
const selectedScripts = [new ScriptStub('s1'), new ScriptStub('s2'), new ScriptStub('s3')];
|
||||
const sut = new UserSelection(app, selectedScripts);
|
||||
|
||||
// act
|
||||
sut.deselectAll();
|
||||
const actual = sut.selectedScripts;
|
||||
|
||||
// assert
|
||||
expect(actual, JSON.stringify(sut.selectedScripts)).to.have.length(0);
|
||||
});
|
||||
it('selectOnly selects expected', async () => {
|
||||
// arrange
|
||||
const app = new ApplicationStub()
|
||||
.withCategory(new CategoryStub(1)
|
||||
.withScriptIds('s1', 's2', 's3', 's4'));
|
||||
const selectedScripts = [new ScriptStub('s1'), new ScriptStub('s2'), new ScriptStub('s3')];
|
||||
const sut = new UserSelection(app, selectedScripts);
|
||||
const expected = [new ScriptStub('s2'), new ScriptStub('s3'), new ScriptStub('s4')];
|
||||
|
||||
// act
|
||||
sut.selectOnly(expected);
|
||||
const actual = sut.selectedScripts;
|
||||
|
||||
// assert
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
@@ -3,15 +3,44 @@ import { expect } from 'chai';
|
||||
import { Script } from '@/domain/Script';
|
||||
|
||||
describe('Script', () => {
|
||||
|
||||
it('cannot construct with duplicate lines', () => {
|
||||
// arrange
|
||||
const code = 'duplicate\nduplicate\ntest\nduplicate';
|
||||
|
||||
// act
|
||||
function construct() { return new Script('ScriptName', code, [], true); }
|
||||
|
||||
// assert
|
||||
expect(construct).to.throw();
|
||||
describe('ctor', () => {
|
||||
describe('code', () => {
|
||||
it('cannot construct with duplicate lines', () => {
|
||||
const code = 'duplicate\nduplicate\ntest\nduplicate';
|
||||
expect(() => createWithCode(code)).to.throw();
|
||||
});
|
||||
it('cannot construct with empty lines', () => {
|
||||
const code = 'duplicate\n\n\ntest\nduplicate';
|
||||
expect(() => createWithCode(code)).to.throw();
|
||||
});
|
||||
});
|
||||
describe('revertCode', () => {
|
||||
it('cannot construct with duplicate lines', () => {
|
||||
const code = 'duplicate\nduplicate\ntest\nduplicate';
|
||||
expect(() => createWithCode('REM', code)).to.throw();
|
||||
});
|
||||
it('cannot construct with empty lines', () => {
|
||||
const code = 'duplicate\n\n\ntest\nduplicate';
|
||||
expect(() => createWithCode('REM', code)).to.throw();
|
||||
});
|
||||
it('cannot construct with when same as code', () => {
|
||||
const code = 'REM';
|
||||
expect(() => createWithCode(code, code)).to.throw();
|
||||
});
|
||||
});
|
||||
describe('canRevert', () => {
|
||||
it('returns false without revert code', () => {
|
||||
const sut = createWithCode('code');
|
||||
expect(sut.canRevert()).to.equal(false);
|
||||
});
|
||||
it('returns true with revert code', () => {
|
||||
const sut = createWithCode('code', 'non empty revert code');
|
||||
expect(sut.canRevert()).to.equal(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function createWithCode(code: string, revertCode?: string): Script {
|
||||
return new Script('name', code, revertCode, [], false);
|
||||
}
|
||||
|
||||
@@ -8,15 +8,15 @@ describe('InMemoryRepository', () => {
|
||||
[new NumericEntityStub(1), new NumericEntityStub(2), new NumericEntityStub(3)]);
|
||||
|
||||
describe('item exists', () => {
|
||||
const actual = sut.exists(new NumericEntityStub(1));
|
||||
const actual = sut.exists(1);
|
||||
it('returns true', () => expect(actual).to.be.true);
|
||||
});
|
||||
describe('item does not exist', () => {
|
||||
const actual = sut.exists(new NumericEntityStub(99));
|
||||
const actual = sut.exists(99);
|
||||
it('returns false', () => expect(actual).to.be.false);
|
||||
});
|
||||
});
|
||||
it('can get', () => {
|
||||
it('getItems gets initial items', () => {
|
||||
// arrange
|
||||
const expected = [
|
||||
new NumericEntityStub(1), new NumericEntityStub(2), new NumericEntityStub(3), new NumericEntityStub(4)];
|
||||
@@ -28,7 +28,7 @@ describe('InMemoryRepository', () => {
|
||||
// assert
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
it('can add', () => {
|
||||
it('addItem adds', () => {
|
||||
// arrange
|
||||
const sut = new InMemoryRepository<number, NumericEntityStub>();
|
||||
const expected = {
|
||||
@@ -47,7 +47,7 @@ describe('InMemoryRepository', () => {
|
||||
expect(actual.length).to.equal(expected.length);
|
||||
expect(actual.item).to.deep.equal(expected.item);
|
||||
});
|
||||
it('can remove', () => {
|
||||
it('removeItem removes', () => {
|
||||
// arrange
|
||||
const initialItems = [
|
||||
new NumericEntityStub(1), new NumericEntityStub(2), new NumericEntityStub(3), new NumericEntityStub(4)];
|
||||
@@ -69,4 +69,30 @@ describe('InMemoryRepository', () => {
|
||||
expect(actual.length).to.equal(expected.length);
|
||||
expect(actual.items).to.deep.equal(expected.items);
|
||||
});
|
||||
describe('addOrUpdateItem', () => {
|
||||
it('adds when item does not exist', () => {
|
||||
// arrange
|
||||
const initialItems = [ new NumericEntityStub(1), new NumericEntityStub(2) ];
|
||||
const newItem = new NumericEntityStub(3);
|
||||
const expected = [ ...initialItems, newItem ];
|
||||
const sut = new InMemoryRepository<number, NumericEntityStub>(initialItems);
|
||||
// act
|
||||
sut.addOrUpdateItem(newItem);
|
||||
// assert
|
||||
const actual = sut.getItems();
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
it('updates when item exists', () => {
|
||||
// arrange
|
||||
const initialItems = [ new NumericEntityStub(1).withCustomProperty('bca') ];
|
||||
const updatedItem = new NumericEntityStub(1).withCustomProperty('abc');
|
||||
const expected = [ updatedItem ];
|
||||
const sut = new InMemoryRepository<number, NumericEntityStub>(initialItems);
|
||||
// act
|
||||
sut.addOrUpdateItem(updatedItem);
|
||||
// assert
|
||||
const actual = sut.getItems();
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,16 +35,20 @@ describe('Signal Tests', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
receivers = [
|
||||
new ReceiverMock(), new ReceiverMock(),
|
||||
new ReceiverMock(), new ReceiverMock()];
|
||||
for (const receiver of receivers) {
|
||||
new ReceiverMock(), new ReceiverMock(),
|
||||
new ReceiverMock(), new ReceiverMock()];
|
||||
function subscribeReceiver(receiver: ReceiverMock) {
|
||||
signal.on((arg) => receiver.onReceive(arg));
|
||||
}});
|
||||
}
|
||||
for (const receiver of receivers) {
|
||||
subscribeReceiver(receiver);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
it('notify() should execute all callbacks', () => {
|
||||
signal.notify(5);
|
||||
receivers.every((receiver) => {
|
||||
receivers.forEach((receiver) => {
|
||||
expect(receiver.onRecieveCalls).to.have.length(1);
|
||||
});
|
||||
});
|
||||
@@ -52,7 +56,7 @@ describe('Signal Tests', () => {
|
||||
it('notify() should execute all callbacks with payload', () => {
|
||||
const expected = 5;
|
||||
signal.notify(expected);
|
||||
receivers.every((receiver) => {
|
||||
receivers.forEach((receiver) => {
|
||||
expect(receiver.onRecieveCalls).to.deep.equal([expected]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { IApplication, ICategory, IScript } from '@/domain/IApplication';
|
||||
|
||||
export class ApplicationStub implements IApplication {
|
||||
public readonly totalScripts = 0;
|
||||
public readonly totalCategories = 0;
|
||||
public totalScripts = 0;
|
||||
public totalCategories = 0;
|
||||
public readonly name = 'StubApplication';
|
||||
public readonly repositoryUrl = 'https://privacy.sexy';
|
||||
public readonly version = '0.1.0';
|
||||
public readonly categories = new Array<ICategory>();
|
||||
public readonly actions = new Array<ICategory>();
|
||||
|
||||
public withCategory(category: ICategory): IApplication {
|
||||
this.categories.push(category);
|
||||
public withAction(category: ICategory): IApplication {
|
||||
this.actions.push(category);
|
||||
return this;
|
||||
}
|
||||
public findCategory(categoryId: number): ICategory {
|
||||
@@ -19,12 +19,51 @@ export class ApplicationStub implements IApplication {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
public findScript(scriptId: string): IScript {
|
||||
throw new Error('Method not implemented.');
|
||||
return this.getAllScripts().find((script) => scriptId === script.id);
|
||||
}
|
||||
public getAllScripts(): ReadonlyArray<IScript> {
|
||||
throw new Error('Method not implemented.');
|
||||
const scripts = [];
|
||||
for (const category of this.actions) {
|
||||
const categoryScripts = getScriptsRecursively(category);
|
||||
scripts.push(...categoryScripts);
|
||||
}
|
||||
return scripts;
|
||||
}
|
||||
public getAllCategories(): ReadonlyArray<ICategory> {
|
||||
throw new Error('Method not implemented.');
|
||||
const categories = [];
|
||||
categories.push(...this.actions);
|
||||
for (const category of this.actions) {
|
||||
const subCategories = getSubCategoriesRecursively(category);
|
||||
categories.push(...subCategories);
|
||||
}
|
||||
return categories;
|
||||
}
|
||||
}
|
||||
|
||||
function getSubCategoriesRecursively(category: ICategory): ReadonlyArray<ICategory> {
|
||||
const subCategories = [];
|
||||
if (category.subCategories) {
|
||||
for (const subCategory of category.subCategories) {
|
||||
subCategories.push(subCategory);
|
||||
subCategories.push(...getSubCategoriesRecursively(subCategory));
|
||||
}
|
||||
}
|
||||
return subCategories;
|
||||
}
|
||||
|
||||
|
||||
function getScriptsRecursively(category: ICategory): ReadonlyArray<IScript> {
|
||||
const categoryScripts = [];
|
||||
if (category.scripts) {
|
||||
for (const script of category.scripts) {
|
||||
categoryScripts.push(script);
|
||||
}
|
||||
}
|
||||
if (category.subCategories) {
|
||||
for (const subCategory of category.subCategories) {
|
||||
const subCategoryScripts = getScriptsRecursively(subCategory);
|
||||
categoryScripts.push(...subCategoryScripts);
|
||||
}
|
||||
}
|
||||
return categoryScripts;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { ScriptStub } from './ScriptStub';
|
||||
import { BaseEntity } from '@/infrastructure/Entity/BaseEntity';
|
||||
import { ICategory, IScript } from '@/domain/ICategory';
|
||||
import { ScriptStub } from './ScriptStub';
|
||||
|
||||
export class CategoryStub extends BaseEntity<number> implements ICategory {
|
||||
public readonly name = `category-with-id-${this.id}`;
|
||||
public name = `category-with-id-${this.id}`;
|
||||
public readonly subCategories = new Array<ICategory>();
|
||||
public readonly scripts = new Array<IScript>();
|
||||
public readonly documentationUrls = new Array<string>();
|
||||
@@ -13,14 +13,22 @@ export class CategoryStub extends BaseEntity<number> implements ICategory {
|
||||
}
|
||||
public withScriptIds(...scriptIds: string[]): CategoryStub {
|
||||
for (const scriptId of scriptIds) {
|
||||
this.scripts.push(new ScriptStub(scriptId));
|
||||
this.withScript(new ScriptStub(scriptId));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public withScripts(...scripts: IScript[]): CategoryStub {
|
||||
for (const script of scripts) {
|
||||
this.scripts.push(script);
|
||||
this.withScript(script);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public withScript(script: IScript): CategoryStub {
|
||||
this.scripts.push(script);
|
||||
return this;
|
||||
}
|
||||
public withName(categoryName: string) {
|
||||
this.name = categoryName;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { BaseEntity } from '@/infrastructure/Entity/BaseEntity';
|
||||
|
||||
export class NumericEntityStub extends BaseEntity<number> {
|
||||
public customProperty = 'customProperty';
|
||||
constructor(id: number) {
|
||||
super(id);
|
||||
}
|
||||
public withCustomProperty(value: string): NumericEntityStub {
|
||||
this.customProperty = value;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,34 @@
|
||||
import { BaseEntity } from '@/infrastructure/Entity/BaseEntity';
|
||||
import { IScript } from './../../../src/domain/IScript';
|
||||
import { IScript } from '@/domain/IScript';
|
||||
|
||||
export class ScriptStub extends BaseEntity<string> implements IScript {
|
||||
public readonly name = `name${this.id}`;
|
||||
public readonly code = `name${this.id}`;
|
||||
public name = `name${this.id}`;
|
||||
public code = `REM code${this.id}`;
|
||||
public revertCode = `REM revertCode${this.id}`;
|
||||
public readonly documentationUrls = new Array<string>();
|
||||
public isRecommended = false;
|
||||
|
||||
constructor(public readonly id: string) {
|
||||
super(id);
|
||||
}
|
||||
public canRevert(): boolean {
|
||||
return Boolean(this.revertCode);
|
||||
}
|
||||
|
||||
public withIsRecommended(value: boolean): ScriptStub {
|
||||
this.isRecommended = value;
|
||||
return this;
|
||||
}
|
||||
public withCode(value: string): ScriptStub {
|
||||
this.code = value;
|
||||
return this;
|
||||
}
|
||||
public withName(name: string): ScriptStub {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
public withRevertCode(revertCode: string): ScriptStub {
|
||||
this.revertCode = revertCode;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user