From 914954ca9d53abd32a6314609458be9c08ba7f06 Mon Sep 17 00:00:00 2001 From: Renan Katreque Date: Fri, 4 Dec 2020 17:17:21 -0300 Subject: [PATCH] Issue #6 solved. --- src/classes/Player.js | 31 +++++++++++++++++++++ tests/classes/Player.test.js | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/classes/Player.js b/src/classes/Player.js index ce7315c..7ed2a6e 100644 --- a/src/classes/Player.js +++ b/src/classes/Player.js @@ -1,6 +1,7 @@ // @flow const fgAction = require('./Action'); +const { throwMissingParameters } = require('../errors'); class Player { declare id: string | number; @@ -10,6 +11,36 @@ class Player { this.id = id; this.Actions = Actions || []; } + + addAction(Actions: Array) { + if (!Actions) { + throw new Error(throwMissingParameters('Actions')); + } + + if (!(Actions instanceof Array)) { + throw new Error(`The parameter Actions must be an Array.`); + } + + Actions.forEach((Action) => { + this.Actions.push(Action); + }); + } + + removeAction(id: any) { + if (!id) { + throw new Error(throwMissingParameters('id')); + } + + this.Actions.forEach((Action, i) => { + if (Action.id === id) { + this.Actions.splice(i, 1); + } + }); + } + + removeAllActions() { + this.Actions = []; + } } module.exports = Player; diff --git a/tests/classes/Player.test.js b/tests/classes/Player.test.js index 6b9fb97..5147eed 100644 --- a/tests/classes/Player.test.js +++ b/tests/classes/Player.test.js @@ -33,3 +33,57 @@ test('Checking props on the child class.', (t) => { t.deepEqual(Class.Actions, [new Action()]); t.is(Class.name, 'Player1'); }); + +test('addAction should work.', (t) => { + const Class = new MPlayer(1, [], 'Player1'); + + Class.addAction([new Action(1), new Action(2)]); + t.deepEqual(Class.Actions, [new Action(1), new Action(2)]); + + Class.addAction([new Action(3)]); + t.deepEqual(Class.Actions, [new Action(1), new Action(2), new Action(3)]); +}); + +test('addAction without Actions should throw.', (t) => { + const Class = new MPlayer(1, [], 'Player1'); + const error = t.throws(() => { + Class.addAction(); + }); + + t.is(error.message, 'A required parameter (Actions) is missing.'); +}); + +test('addAction with Actions not being an Array should throw.', (t) => { + const Class = new MPlayer(1, [], 'Player1'); + const error = t.throws(() => { + Class.addAction('notArray'); + }); + + t.is(error.message, 'The parameter Actions must be an Array.'); +}); + +test('removeAction should work.', (t) => { + const Class = new MPlayer(1, [new Action(1), new Action(2), new Action(3)], 'Player1'); + + Class.removeAction(1); + t.deepEqual(Class.Actions, [new Action(2), new Action(3)]); + + Class.removeAction(3); + t.deepEqual(Class.Actions, [new Action(2)]); +}); + +test('removeAction without id should throw', (t) => { + const Class = new MPlayer(1, [new Action(1)], 'Player1'); + const error = t.throws(() => { + Class.removeAction(); + }); + + t.is(error.message, 'A required parameter (id) is missing.'); +}); + +test('removeAllActions should work.', (t) => { + const Class = new MPlayer(1, [new Action(1), new Action(2), new Action(3)], 'Player1'); + + Class.removeAllActions(); + t.deepEqual(Class.Actions, []); +});