Skip to content

Practicing Unit testing in Javascript by building a battleship game with BDD

Notifications You must be signed in to change notification settings

Moonminky/JS-unit-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

This is a simple battleship game engine, written in Javascript using BDD (Behaviour Driven Development). It is part of a course on Unit Testing and BDD. LINK

Prerequisites

This project uses Mocha and Chai for unit testing.

  • Chai

    npm install chai --save-dev
    
  • Mocha

    npm install --save-dev mocha
    

TOC

Mocha

Should run our tests using npm.

expect(true).to.be.ok;

PLAYER METHODS

validateLocation

should confirm valid for unoccupied locations in range.

let location = [0, 0];
let actual = validateLocation(player, location);
expect(actual).to.be.ok;

should confirm INvalid for occupied locations in range.

let location = [9, 9];
let actual = validateLocation(player, location);
expect(actual).to.be.false;

should confirm INvalid for UNoccupied locations OUT of range.

let locationHigh = [10, 10];
let locationLow = [-1, -1];
expect(validateLocation(player, locationHigh)).to.be.false;
expect(validateLocation(player, locationLow)).to.be.false;

validateLocations

should correctly report a list of unoccupied locations is valid.

let locations = [
  [1, 1],
  [1, 2],
  [1, 3],
  [1, 4]
];
expect(validateLocations(player, locations)).to.be.ok;

should correctly report a problem if any location in the list is invalid.

let locations = [
  [1, 1],
  [1, 2],
  [1, 3],
  [10, 10]
];
expect(validateLocations(player, locations)).to.be.false;
locations = [
  [1, 1],
  [1, 2],
  [1, 3],
  [0, 0]
];
expect(validateLocations(player, locations)).to.be.false;

placeShip

should update a ship with a valid starting location.

let ship = player.ships[0];
let coordinates = [0, 1];
placeShip(player, ship, coordinates, "horizontal");
let actual = ship.locations;
expect(actual).to.be.ok;
expect(actual).to.have.length(1);
expect(actual[0]).to.deep.equal([0, 1]);

should throw an error if no direction is specified.

let ship = player.ships[0];
let coordinates = [0, 1];
let handler = function() {
  placeShip(player, ship, coordinates);
};
expect(handler).to.throw(Error);
expect(handler).to.throw("You left ouf the direction ! I need that for math!");

GAME INSTANCE FUNCTIONS

checkGameStatus

should tell me when the game is over.

let players = [
    {
        ships: [
            {
                locations: [[0,0]],
                damage: [[0,0]]
            }
        ]
    }
];
let actual = checkGameStatus(players);
expect(actual).to.be.false;

takeTurn

should return false if the game ends.

let actual = takeTurn(player, guess);
expect(actual).to.be.false;

saveGame

should update save status.

let status = "game not saved...";
saveGame(function() {
  status = "game saved!";
  expect(status).to.equal("game saved!");
  done();
});

SHIP METHODS

checkForShip

should correctly report no ship at a given players coordinate.

expect(checkForShip(player, [9, 9])).to.be.false;

should correctly report a ship at a given players coordinate.

expect(checkForShip(player, [0, 0])).to.deep.equal(player.ships[0]);

should handle ships located at more than one coordinate.

expect(checkForShip(player, [0, 1])).to.deep.equal(player.ships[0]);
expect(checkForShip(player, [0, 0])).to.deep.equal(player.ships[0]);
expect(checkForShip(player, [9, 9])).to.be.false;

should handle checking multiple ships.

expect(checkForShip(player, [0, 1])).to.deep.equal(player.ships[0]);
expect(checkForShip(player, [0, 0])).to.deep.equal(player.ships[0]);
expect(checkForShip(player, [1, 1])).to.deep.equal(player.ships[1]);
expect(checkForShip(player, [1, 0])).to.deep.equal(player.ships[1]);
expect(checkForShip(player, [2, 3])).to.deep.equal(player.ships[2]);
expect(checkForShip(player, [2, 2])).to.deep.equal(player.ships[2]);
expect(checkForShip(player, [9, 9])).to.be.false;

damageShip

should register damage on a given ship at a given location.

let ship = {
  locations: [[0, 0]],
  damage: []
};
damageShip(ship, [0, 0]);
expect(ship.damage).to.not.be.empty;
expect(ship.damage[0]).to.deep.equal([0, 0]);

fire

should record damage on the given players ship at a given coordinate.

fire(player, [0, 0]);
expect(player.ships[0].damage[0]).to.deep.equal([0, 0]);

should NOT record damage if there's no ship at given coordinate.

fire(player, [9, 9]);
expect(player.ships[0].damage).to.be.empty;

About

Practicing Unit testing in Javascript by building a battleship game with BDD

Topics

Resources

Stars

Watchers

Forks