From 288b6ed220239afa5268b7dc197aa07abd70f80b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ckelmann?= Date: Sat, 27 Jan 2024 13:19:46 +0100 Subject: [PATCH] Ensure that not two modelcomponents with the same name can be created --- .../scriptAccounts/ScriptAccountTest.spec.ts | 40 +++++++++---------- src/app/app.component.ts | 4 +- src/app/game-model/GameModel.ts | 7 +++- .../script-account-overview.component.spec.ts | 5 +++ .../script-account-overview.component.ts | 7 ++-- 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/e2e/game-model/scriptAccounts/ScriptAccountTest.spec.ts b/e2e/game-model/scriptAccounts/ScriptAccountTest.spec.ts index a9048e3..c7eb65b 100644 --- a/e2e/game-model/scriptAccounts/ScriptAccountTest.spec.ts +++ b/e2e/game-model/scriptAccounts/ScriptAccountTest.spec.ts @@ -21,38 +21,36 @@ test.describe('Test ScriptAccounts', () => { test("Test Adding ScriptAccounts", async () => { const gameModel: GameModel = new GameModel("GameModel"); - const scriptAccount = new ScriptAccount("ScriptAccount", "Description"); - gameModel.addScriptAccount(scriptAccount); - + let scriptAccount =gameModel.addScriptAccount("ScriptAccount"); + expect(scriptAccount).toBeDefined(); expect(gameModel.scriptAccounts.length).toEqual(1); expect(gameModel.scriptAccounts.includes(scriptAccount)).toBeTruthy(); - //Test for duplicates - gameModel.addScriptAccount(scriptAccount); - expect(gameModel.scriptAccounts.length).toEqual(1); - - //Test for adding null value - gameModel.addScriptAccount(null); - expect(gameModel.scriptAccounts.length).toEqual(1); - gameModel.addScriptAccount(undefined); - expect(gameModel.scriptAccounts.length).toEqual(1); - //Test adding scriptAccount with already existing name - const scriptAccount2 = new ScriptAccount("ScriptAccount", ""); - gameModel.addScriptAccount(scriptAccount2); + const scriptAccount2 = gameModel.addScriptAccount("ScriptAccount") + expect(scriptAccount2).toBeUndefined(); + expect(gameModel.scriptAccounts.length).toEqual(1); + + //Test for adding invalid names as scriptaccount names (null/undefined/empty) + let result = gameModel.addScriptAccount(null); + expect(result).toBeUndefined(); + expect(gameModel.scriptAccounts.length).toEqual(1); + result = gameModel.addScriptAccount(undefined); + expect(result).toBeUndefined(); + expect(gameModel.scriptAccounts.length).toEqual(1); + result = gameModel.addScriptAccount(""); + expect(result).toBeUndefined(); expect(gameModel.scriptAccounts.length).toEqual(1); - expect(gameModel.scriptAccounts.includes(scriptAccount2)).toBeFalsy(); }) test("test Removing ScriptAccounts", async () => { const gameModel: GameModel = new GameModel("GameModel"); - const scriptAccount = new ScriptAccount("ScriptAccount", "Description"); - const scriptAccount2 = new ScriptAccount("ScriptAccount 2", "Description"); + let scriptAccount = new ScriptAccount("test", "") gameModel.removeScriptAccount(scriptAccount); expect(gameModel.scriptAccounts.length).toEqual(0); - gameModel.addScriptAccount(scriptAccount); + scriptAccount = gameModel.addScriptAccount("ScriptAccount"); gameModel.removeScriptAccount(scriptAccount); expect(gameModel.scriptAccounts.length).toEqual(0); @@ -62,8 +60,8 @@ test.describe('Test ScriptAccounts', () => { gameModel.removeScriptAccount(null); expect(gameModel.scriptAccounts.length).toEqual(0); - gameModel.addScriptAccount(scriptAccount); - gameModel.addScriptAccount(scriptAccount2); + scriptAccount = gameModel.addScriptAccount(scriptAccount); + let scriptAccount2 = gameModel.addScriptAccount("ScriptAccount 2"); gameModel.removeScriptAccount(scriptAccount); expect(gameModel.scriptAccounts.length).toEqual(1); diff --git a/src/app/app.component.ts b/src/app/app.component.ts index e2f110d..85775ad 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -39,8 +39,8 @@ export class AppComponent implements OnInit{ ngOnInit() { this.gameModel = new GameModel("No More"); - this.gameModel.addScriptAccount(new ScriptAccount("Temperature", "")); - this.gameModel.addScriptAccount(new ScriptAccount("Luftfeuchtigkeit", "")); + this.gameModel.addScriptAccount("Temperature"); + this.gameModel.addScriptAccount("Luftfeuchtigkeit"); } openScriptAccountsOverview() { diff --git a/src/app/game-model/GameModel.ts b/src/app/game-model/GameModel.ts index b27a223..a061db3 100644 --- a/src/app/game-model/GameModel.ts +++ b/src/app/game-model/GameModel.ts @@ -32,13 +32,16 @@ export class GameModel { this._gamesystems = this._gamesystems.filter(g => g !== gamesystem); } - addScriptAccount(scriptAccount: ScriptAccount) { - if(scriptAccount != undefined && !this.scriptAccounts.includes(scriptAccount)) { + addScriptAccount(scriptAccountName: string) { + if(scriptAccountName != undefined && scriptAccountName.length > 0) { + const scriptAccount = new ScriptAccount(scriptAccountName, ""); const searchedScriptAccount = this.scriptAccounts.find(s => s.componentName === scriptAccount.componentName); if(searchedScriptAccount == undefined) { this.scriptAccounts.push(scriptAccount); + return scriptAccount; } } + return undefined; } removeScriptAccount(scriptAccount: ScriptAccount) { diff --git a/src/app/side-overviews/script-account-overview/script-account-overview.component.spec.ts b/src/app/side-overviews/script-account-overview/script-account-overview.component.spec.ts index fce1a6e..09f3d6f 100644 --- a/src/app/side-overviews/script-account-overview/script-account-overview.component.spec.ts +++ b/src/app/side-overviews/script-account-overview/script-account-overview.component.spec.ts @@ -37,6 +37,11 @@ describe('ScriptAccountOverview', () => { expect(component.openScriptAccountEmitter.emit).toHaveBeenCalledWith(component.gameModel!.scriptAccounts[0]); + component.onCreateNewScriptAccount(); + fixture.detectChanges() + expect(component.openScriptAccountEmitter.emit).toBeCalledTimes(1); + + })) diff --git a/src/app/side-overviews/script-account-overview/script-account-overview.component.ts b/src/app/side-overviews/script-account-overview/script-account-overview.component.ts index 496be51..e57d45a 100644 --- a/src/app/side-overviews/script-account-overview/script-account-overview.component.ts +++ b/src/app/side-overviews/script-account-overview/script-account-overview.component.ts @@ -31,8 +31,9 @@ export class ScriptAccountOverviewComponent { } onCreateNewScriptAccount() { - const scriptAccount = new ScriptAccount("New ScriptAccount", ""); - this.gameModel!.addScriptAccount(scriptAccount); - this.openScriptAccountEmitter.emit(scriptAccount); + const scriptAccount = this.gameModel!.addScriptAccount("New ScriptAccount"); + if(scriptAccount != undefined) { + this.openScriptAccountEmitter.emit(scriptAccount); + } } }