From 4e021415c6c0b84d02f75859d5c6a02477afc9d2 Mon Sep 17 00:00:00 2001 From: Iku-turso Date: Mon, 13 Mar 2023 12:29:43 +0200 Subject: [PATCH] Introduce first stages of the kata Co-authored-by: Janne Savolainen Signed-off-by: Iku-turso --- .../src/monster-beatdown.test.ts | 74 +++++++++++++++++++ .../kata-for-gabriel/src/monster-beatdown.ts | 21 ++++++ 2 files changed, 95 insertions(+) create mode 100644 packages/kata-for-gabriel/src/monster-beatdown.test.ts create mode 100644 packages/kata-for-gabriel/src/monster-beatdown.ts diff --git a/packages/kata-for-gabriel/src/monster-beatdown.test.ts b/packages/kata-for-gabriel/src/monster-beatdown.test.ts new file mode 100644 index 0000000000..2024ccd3cb --- /dev/null +++ b/packages/kata-for-gabriel/src/monster-beatdown.test.ts @@ -0,0 +1,74 @@ +import { createGame, Dependencies } from "./monster-beatdown"; +import asyncFn, { AsyncFnMock } from "@async-fn/jest"; + +describe("monster-beatdown", () => { + let game: { start: () => void }; + let messageToPlayerMock: jest.Mocked; + let questionToPlayerMock: AsyncFnMock; + + beforeEach(() => { + messageToPlayerMock = jest.fn(); + questionToPlayerMock = asyncFn(); + + game = createGame({ + messageToPlayer: messageToPlayerMock, + questionToPlayer: questionToPlayerMock, + }); + }); + + describe("when game is not started", () => { + it("does not message player about anything", () => { + expect(messageToPlayerMock).not.toHaveBeenCalled(); + }); + + it("does not question player about anything", () => { + expect(questionToPlayerMock).not.toHaveBeenCalled(); + }); + }); + + describe("when game is started", () => { + beforeEach(() => { + game.start(); + }); + + it("player encounters a monster", () => { + expect(messageToPlayerMock).toHaveBeenCalledWith("You encounter a monster"); + }); + + it("player is asked if they wants to attack the monster", () => { + expect(questionToPlayerMock).toHaveBeenCalledWith("Attack the monster?"); + }); + + describe("when the player chooses to not attack the monster", () => { + beforeEach(async () => { + await questionToPlayerMock.resolve(false); + }); + + it("the player gets eaten", () => { + expect(messageToPlayerMock).toHaveBeenCalledWith( + "You chose not to attack the monster, and the monster eats you dead, in disappointment.", + ); + }); + + it("the player does not attack", () => { + expect(messageToPlayerMock).not.toHaveBeenCalledWith("You attack the monster."); + }); + }); + + describe("when the player chooses to attack the monster", () => { + beforeEach(async () => { + await questionToPlayerMock.resolve(true); + }); + + it("the player attacks the monster", () => { + expect(messageToPlayerMock).toHaveBeenCalledWith("You attack the monster."); + }); + + it("the player does not get eaten yet", () => { + expect(messageToPlayerMock).not.toHaveBeenCalledWith( + "You chose not to attack the monster, and the monster eats you dead, in disappointment.", + ); + }); + }); + }); +}); diff --git a/packages/kata-for-gabriel/src/monster-beatdown.ts b/packages/kata-for-gabriel/src/monster-beatdown.ts new file mode 100644 index 0000000000..a31ff9030c --- /dev/null +++ b/packages/kata-for-gabriel/src/monster-beatdown.ts @@ -0,0 +1,21 @@ +export type Dependencies = { + messageToPlayer: (message: string) => void; + questionToPlayer: (message: string) => Promise; +}; + +const createGame = ({ messageToPlayer, questionToPlayer }: Dependencies) => ({ + start: async () => { + messageToPlayer("You encounter a monster"); + + const playerWantsToAttack = await questionToPlayer("Attack the monster?"); + if (playerWantsToAttack) { + messageToPlayer("You attack the monster."); + } else { + messageToPlayer( + "You chose not to attack the monster, and the monster eats you dead, in disappointment.", + ); + } + }, +}); + +export { createGame };