diff --git a/src/application/Parser/ApplicationParser.ts b/src/application/Parser/ApplicationParser.ts index 564f01b3..bf76996f 100644 --- a/src/application/Parser/ApplicationParser.ts +++ b/src/application/Parser/ApplicationParser.ts @@ -4,26 +4,17 @@ import { Script } from '@/domain/Script'; import applicationFile from 'js-yaml-loader!./../application.yaml'; import { parseCategory } from './CategoryParser'; -interface ApplicationResult { - readonly application: Application; - readonly selectedScripts: Script[]; -} - -export function buildApplication(): ApplicationResult { +export function parseApplication(): Application { const name = applicationFile.name as string; const version = applicationFile.version as number; const categories = new Array(); - const selectedScripts = new Array diff --git a/tests/unit/application/UserSelection.spec.ts b/tests/unit/application/UserSelection.spec.ts index aae97c99..11dce56d 100644 --- a/tests/unit/application/UserSelection.spec.ts +++ b/tests/unit/application/UserSelection.spec.ts @@ -11,7 +11,7 @@ describe('UserSelection', () => { // arrange const app = new ApplicationStub() .withCategory(new CategoryStub(1) - .withScripts('s1', 's2', 's3', 's4')); + .withScriptIds('s1', 's2', 's3', 's4')); const selectedScripts = [new ScriptStub('s1'), new ScriptStub('s2'), new ScriptStub('s3')]; const sut = new UserSelection(app, selectedScripts); @@ -26,7 +26,7 @@ describe('UserSelection', () => { // arrange const app = new ApplicationStub() .withCategory(new CategoryStub(1) - .withScripts('s1', 's2', 's3', 's4')); + .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')]; diff --git a/tests/unit/domain/Application.spec.ts b/tests/unit/domain/Application.spec.ts new file mode 100644 index 00000000..ba4d9dac --- /dev/null +++ b/tests/unit/domain/Application.spec.ts @@ -0,0 +1,62 @@ +import { ScriptStub } from './../stubs/ScriptStub'; +import { CategoryStub } from './../stubs/CategoryStub'; +import { Application } from './../../../src/domain/Application'; +import 'mocha'; +import { expect } from 'chai'; + +describe('Application', () => { + it('getRecommendedScripts returns as expected', () => { + // arrange + const expected = [ + new ScriptStub('S3').withIsRecommended(true), + new ScriptStub('S4').withIsRecommended(true), + ]; + const sut = new Application('name', 2, [ + new CategoryStub(3).withScripts(expected[0], new ScriptStub('S1').withIsRecommended(false)), + new CategoryStub(2).withScripts(expected[1], new ScriptStub('S2').withIsRecommended(false)), + ]); + + // act + const actual = sut.getRecommendedScripts(); + + // assert + expect(expected[0]).to.deep.equal(actual[0]); + expect(expected[1]).to.deep.equal(actual[1]); + }); + it('cannot construct without categories', () => { + // arrange + const categories = []; + + // act + function construct() { return new Application('name', 2, categories); } + + // assert + expect(construct).to.throw('Application must consist of at least one category'); + }); + it('cannot construct without scripts', () => { + // arrange + const categories = [ + new CategoryStub(3), + new CategoryStub(2), + ]; + + // act + function construct() { return new Application('name', 2, categories); } + + // assert + expect(construct).to.throw('Application must consist of at least one script'); + }); + it('cannot construct without any recommended scripts', () => { + // arrange + const categories = [ + new CategoryStub(3).withScripts(new ScriptStub('S1').withIsRecommended(false)), + new CategoryStub(2).withScripts(new ScriptStub('S2').withIsRecommended(false)), + ]; + + // act + function construct() { return new Application('name', 2, categories); } + + // assert + expect(construct).to.throw('Application must consist of at least one recommended script'); + }); +}); diff --git a/tests/unit/domain/Script.spec.ts b/tests/unit/domain/Script.spec.ts index 56bfb999..0a2d77c6 100644 --- a/tests/unit/domain/Script.spec.ts +++ b/tests/unit/domain/Script.spec.ts @@ -4,12 +4,12 @@ import { Script } from '@/domain/Script'; describe('Script', () => { - it('cannot construct with duplicate lines', async () => { + it('cannot construct with duplicate lines', () => { // arrange const code = 'duplicate\nduplicate\ntest\nduplicate'; // act - function construct() { return new Script('ScriptName', code, []); } + function construct() { return new Script('ScriptName', code, [], true); } // assert expect(construct).to.throw(); diff --git a/tests/unit/stubs/ApplicationStub.ts b/tests/unit/stubs/ApplicationStub.ts index 85d91392..c45b3b88 100644 --- a/tests/unit/stubs/ApplicationStub.ts +++ b/tests/unit/stubs/ApplicationStub.ts @@ -1,6 +1,10 @@ import { IApplication, ICategory, IScript } from '@/domain/IApplication'; export class ApplicationStub implements IApplication { + public readonly totalScripts = 0; + public readonly totalCategories = 0; + public readonly name = 'StubApplication'; + public readonly version = 1; public readonly categories = new Array(); public withCategory(category: ICategory): IApplication { @@ -10,11 +14,12 @@ export class ApplicationStub implements IApplication { public findCategory(categoryId: number): ICategory { throw new Error('Method not implemented.'); } - + public getRecommendedScripts(): readonly IScript[] { + throw new Error('Method not implemented.'); + } public findScript(scriptId: string): IScript { throw new Error('Method not implemented.'); } - public getAllScripts(): ReadonlyArray { throw new Error('Method not implemented.'); } diff --git a/tests/unit/stubs/CategoryStub.ts b/tests/unit/stubs/CategoryStub.ts index 39534583..358e2c64 100644 --- a/tests/unit/stubs/CategoryStub.ts +++ b/tests/unit/stubs/CategoryStub.ts @@ -11,10 +11,16 @@ export class CategoryStub extends BaseEntity implements ICategory { constructor(id: number) { super(id); } - public withScripts(...scriptIds: string[]): CategoryStub { + public withScriptIds(...scriptIds: string[]): CategoryStub { for (const scriptId of scriptIds) { this.scripts.push(new ScriptStub(scriptId)); } return this; } + public withScripts(...scripts: IScript[]): CategoryStub { + for (const script of scripts) { + this.scripts.push(script); + } + return this; + } } diff --git a/tests/unit/stubs/ScriptStub.ts b/tests/unit/stubs/ScriptStub.ts index 52666224..d8d1c418 100644 --- a/tests/unit/stubs/ScriptStub.ts +++ b/tests/unit/stubs/ScriptStub.ts @@ -5,8 +5,14 @@ export class ScriptStub extends BaseEntity implements IScript { public readonly name = `name${this.id}`; public readonly code = `name${this.id}`; public readonly documentationUrls = new Array(); + public isRecommended = false; constructor(public readonly id: string) { super(id); } + + public withIsRecommended(value: boolean): ScriptStub { + this.isRecommended = value; + return this; + } }