Tests unitaires
src/services/gameService.test.js
import * as gameService from "./gameService"
describe("Game service", () => {
test("should init a deck", () => {
const defaultDeck = gameService.initDeck()
expect(defaultDeck.length).toBe(52)
expect(defaultDeck.filter((card) => card === "diamonds").length).toBe(6)
// etc
})
test("should draw cards", () => {
const deck = ["camel", "diamonds", "gold"]
const drawnCard = gameService.drawCards(deck, 1)
expect(deck.length).toBe(2)
expect(drawnCard).toStrictEqual(["camel"])
})
test("should put camels from hand to herd", () => {
const game = {
_players: [
{ hand: ["camel", "gold"], camelsCount: 0 },
{ hand: ["gold", "gold"], camelsCount: 0 },
],
}
gameService.putCamelsFromHandToHerd(game)
expect(game._players[0].hand.length).toBe(1)
expect(game._players[0].hand).toStrictEqual(["gold"])
expect(game._players[0].camelsCount).toBe(1)
expect(game._players[1].hand.length).toBe(2)
expect(game._players[1].hand).toStrictEqual(["gold", "gold"])
expect(game._players[1].camelsCount).toBe(0)
})
})Tests d’intégration
src/routes/gameRouter.test.js
import request from "supertest"
import app from "../app"
import lodash from "lodash"
import * as db from "../database"
// Prevent writing tests game to filesystem using src/database/__mocks__/index.js implementation
jest.mock("../database")
// Prevent shuffle for tests
jest.mock("lodash")
lodash.shuffle.mockImplementation((array) => array)
afterEach(() => {
db.clear()
})
describe("Game router", () => {
test("should create a game", async () => {
const expectedGame = {
id: 1,
name: "test",
market: ["camel", "camel", "camel", "diamonds", "diamonds"],
_deck: [
"silver",
"silver",
"silver",
"silver",
"silver",
"silver",
"cloth",
"cloth",
"cloth",
"cloth",
"cloth",
"cloth",
"cloth",
"cloth",
"spice",
"spice",
"spice",
"spice",
"spice",
"spice",
"spice",
"spice",
"leather",
"leather",
"leather",
"leather",
"leather",
"leather",
"leather",
"leather",
"leather",
"leather",
"camel",
"camel",
"camel",
"camel",
"camel",
"camel",
"camel",
"camel",
],
_players: [
{
hand: ["diamonds", "diamonds", "diamonds", "diamonds", "gold"],
camelsCount: 0,
score: 0,
},
{
hand: ["gold", "gold", "gold", "gold", "gold"],
camelsCount: 0,
score: 0,
},
],
currentPlayerIndex: 0,
tokens: {
diamonds: [7, 7, 5, 5, 5],
gold: [6, 6, 5, 5, 5],
silver: [5, 5, 5, 5, 5],
cloth: [5, 3, 3, 2, 2, 1, 1],
spice: [5, 3, 3, 2, 2, 1, 1],
leather: [4, 3, 2, 1, 1, 1, 1, 1, 1],
},
_bonusTokens: {
3: [2, 1, 2, 3, 1, 2, 3],
4: [4, 6, 6, 4, 5, 5],
5: [8, 10, 9, 8, 10],
},
winnerId: undefined,
}
const response = await request(app).post("/games").send({ name: "test" })
expect(response.statusCode).toBe(201)
expect(response.body).toStrictEqual({
id: 1,
name: "test",
})
const games = db.getGames()
expect(games.length).toBe(1)
expect(games[0]).toStrictEqual(expectedGame)
})
test("should return 400 if name not provided", async () => {
const response = await request(app).post("/games").send({})
expect(response.statusCode).toBe(400)
})
test("should return 400 if name is empty", async () => {
const response = await request(app).post("/games").send({ name: "" })
expect(response.statusCode).toBe(400)
})
})