Skip to content

Commit

Permalink
Issue #6 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
Katreque committed Dec 4, 2020
1 parent a2ac918 commit 914954c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/classes/Player.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

const fgAction = require('./Action');
const { throwMissingParameters } = require('../errors');

class Player {
declare id: string | number;
Expand All @@ -10,6 +11,36 @@ class Player {
this.id = id;
this.Actions = Actions || [];
}

addAction(Actions: Array<fgAction>) {
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;
54 changes: 54 additions & 0 deletions tests/classes/Player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, []);
});

0 comments on commit 914954c

Please sign in to comment.