Skip to content

Instantly share code, notes, and snippets.

@joebutler2
Created June 14, 2017 18:35
Show Gist options
  • Save joebutler2/b2b2bca09ffc491463f9953d3695e492 to your computer and use it in GitHub Desktop.
Save joebutler2/b2b2bca09ffc491463f9953d3695e492 to your computer and use it in GitHub Desktop.
First attempt at RPS kata
/**
* Created by pivotal on 6/14/17.
* new Rps().play(...)
*/
function Rps() {
this.p1WinConditions = {
scissors: "paper",
paper: "rock",
rock: "scissors"
};
this.validThrows = Object.keys(this.p1WinConditions);
this.play = function (p1, p2, ui) {
if (this.anyInvalidThrows(p1, p2))
return ui.renderInvalid();
if (p1 === p2) return ui.renderTie();
this.p1WinConditions[p1] == p2
? ui.renderP1Wins()
: ui.renderP2Wins();
};
this.anyInvalidThrows = function (p1, p2) {
return !(this.validThrows.includes(p2) &&
this.validThrows.includes(p1));
}
};
describe("play", function () {
var ui = null;
beforeEach(function () {
ui = jasmine.createSpyObj("ui", ["renderP1Wins", "renderP2Wins",
"renderTie", "renderInvalid"]);
});
it("rock v scissors", function () {
const rps = new Rps().play("rock", "scissors", ui);
expect(ui.renderP1Wins).toHaveBeenCalled();
expect(ui.renderP2Wins).not.toHaveBeenCalled();
expect(ui.renderTie).not.toHaveBeenCalled();
expect(ui.renderInvalid).not.toHaveBeenCalled();
});
it("rock v paper", function () {
const rps = new Rps().play("rock", "paper", ui);
expect(ui.renderP2Wins).toHaveBeenCalled();
expect(ui.renderP1Wins).not.toHaveBeenCalled();
expect(ui.renderTie).not.toHaveBeenCalled();
expect(ui.renderInvalid).not.toHaveBeenCalled();
});
it("scissors v paper", function () {
const rps = new Rps().play("scissors", "paper", ui);
expect(ui.renderP1Wins).toHaveBeenCalled();
expect(ui.renderP2Wins).not.toHaveBeenCalled();
expect(ui.renderTie).not.toHaveBeenCalled();
expect(ui.renderInvalid).not.toHaveBeenCalled();
});
it("scissors v rock", function () {
const rps = new Rps().play("scissors", "rock", ui);
expect(ui.renderP2Wins).toHaveBeenCalled();
expect(ui.renderP1Wins).not.toHaveBeenCalled();
expect(ui.renderTie).not.toHaveBeenCalled();
expect(ui.renderInvalid).not.toHaveBeenCalled();
});
it("paper v scissors", function () {
const rps = new Rps().play("paper", "scissors", ui);
expect(ui.renderP2Wins).toHaveBeenCalled();
expect(ui.renderP1Wins).not.toHaveBeenCalled();
expect(ui.renderTie).not.toHaveBeenCalled();
expect(ui.renderInvalid).not.toHaveBeenCalled();
});
it("paper v rock", function () {
const rps = new Rps().play("paper", "rock", ui);
expect(ui.renderP1Wins).toHaveBeenCalled();
expect(ui.renderP2Wins).not.toHaveBeenCalled();
expect(ui.renderTie).not.toHaveBeenCalled();
expect(ui.renderInvalid).not.toHaveBeenCalled();
});
it("rock v rock", function () {
const rps = new Rps().play("rock", "rock", ui);
expect(ui.renderTie).toHaveBeenCalled();
expect(ui.renderP1Wins).not.toHaveBeenCalled();
expect(ui.renderP2Wins).not.toHaveBeenCalled();
expect(ui.renderInvalid).not.toHaveBeenCalled();
});
it("paper v paper", function () {
const rps = new Rps().play("paper", "paper", ui);
expect(ui.renderTie).toHaveBeenCalled();
expect(ui.renderP1Wins).not.toHaveBeenCalled();
expect(ui.renderP2Wins).not.toHaveBeenCalled();
expect(ui.renderInvalid).not.toHaveBeenCalled();
});
it("scissors v scissors", function () {
const rps = new Rps().play("scissors", "scissors", ui);
expect(ui.renderTie).toHaveBeenCalled();
expect(ui.renderP1Wins).not.toHaveBeenCalled();
expect(ui.renderP2Wins).not.toHaveBeenCalled();
expect(ui.renderInvalid).not.toHaveBeenCalled();
});
it("scissors v sailboat", function () {
const rps = new Rps().play("scissors", "sailboat", ui);
expect(ui.renderInvalid).toHaveBeenCalled();
expect(ui.renderP1Wins).not.toHaveBeenCalled();
expect(ui.renderP2Wins).not.toHaveBeenCalled();
expect(ui.renderTie).not.toHaveBeenCalled();
});
it("sailboat v paper", function () {
const rps = new Rps().play("sailboat", "paper", ui);
expect(ui.renderInvalid).toHaveBeenCalled();
expect(ui.renderP1Wins).not.toHaveBeenCalled();
expect(ui.renderP2Wins).not.toHaveBeenCalled();
expect(ui.renderTie).not.toHaveBeenCalled();
});
it("sailboat v sailboat", function () {
const rps = new Rps().play("sailboat", "sailboat", ui);
expect(ui.renderInvalid).toHaveBeenCalled();
expect(ui.renderP1Wins).not.toHaveBeenCalled();
expect(ui.renderP2Wins).not.toHaveBeenCalled();
expect(ui.renderTie).not.toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment