Skip to content

Commit

Permalink
refactor(game): get playing game helper removed (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed Jun 1, 2023
1 parent 81d7f9e commit e2cfd8a
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 44 deletions.
11 changes: 0 additions & 11 deletions src/modules/game/providers/services/game.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable } from "@nestjs/common";
import { plainToInstance } from "class-transformer";
import type { Types } from "mongoose";
import { API_RESOURCES } from "../../../../shared/api/enums/api.enum";
import { BAD_RESOURCE_MUTATION_REASONS } from "../../../../shared/exception/enums/bad-resource-mutation-error.enum";
import { BadResourceMutationException } from "../../../../shared/exception/types/bad-resource-mutation-exception.type";
Expand Down Expand Up @@ -43,16 +42,6 @@ export class GameService {
return this.gameRepository.create(gameToCreate);
}

public async getGameAndCheckPlayingStatus(gameId: Types.ObjectId): Promise<Game> {
const game = await this.gameRepository.findOne({ _id: gameId });
if (game === null) {
throw new ResourceNotFoundException(API_RESOURCES.GAMES, gameId.toString());
} else if (game.status !== GAME_STATUSES.PLAYING) {
throw new BadResourceMutationException(API_RESOURCES.GAMES, game._id.toString(), BAD_RESOURCE_MUTATION_REASONS.GAME_NOT_PLAYING);
}
return game;
}

public async cancelGame(game: Game): Promise<Game> {
if (game.status !== GAME_STATUSES.PLAYING) {
throw new BadResourceMutationException(API_RESOURCES.GAMES, game._id.toString(), BAD_RESOURCE_MUTATION_REASONS.GAME_NOT_PLAYING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { createFakeMakeGamePlayDto } from "../../../../../../factories/game/dto/
import { createFakeGameVictory } from "../../../../../../factories/game/schemas/game-victory/game-victory.schema.factory";
import { createFakeGame } from "../../../../../../factories/game/schemas/game.schema.factory";
import { createFakeVillagerAlivePlayer, createFakeWerewolfAlivePlayer } from "../../../../../../factories/game/schemas/player/player-with-role.schema.factory";
import { createFakeObjectId } from "../../../../../../factories/shared/mongoose/mongoose.factory";

jest.mock("../../../../../../../src/shared/exception/types/bad-resource-mutation-exception.type");
jest.mock("../../../../../../../src/shared/exception/types/resource-not-found-exception.type");
Expand Down Expand Up @@ -100,41 +99,9 @@ describe("Game Service", () => {
});
});

describe("getGameAndCheckPlayingStatus", () => {
const existingPlayingId = createFakeObjectId();
const existingPlayingGame = createFakeGame({ _id: existingPlayingId, status: GAME_STATUSES.PLAYING });
const existingDoneId = createFakeObjectId();
const existingDoneGame = createFakeGame({ _id: existingDoneId, status: GAME_STATUSES.OVER });
const unknownId = createFakeObjectId();

beforeEach(() => {
when(gameRepositoryMock.findOne).calledWith({ _id: existingPlayingId }).mockResolvedValue(existingPlayingGame);
when(gameRepositoryMock.findOne).calledWith({ _id: existingDoneId }).mockResolvedValue(existingDoneGame);
when(gameRepositoryMock.findOne).calledWith({ _id: unknownId }).mockResolvedValue(null);
});

it("should throw an error when called with unknown id.", async() => {
await expect(service.getGameAndCheckPlayingStatus(unknownId)).toReject();
expect(ResourceNotFoundException).toHaveBeenCalledWith(API_RESOURCES.GAMES, unknownId.toString());
});

it("should throw an error when game doesn't have playing status.", async() => {
await expect(service.getGameAndCheckPlayingStatus(existingDoneId)).toReject();
expect(BadResourceMutationException).toHaveBeenCalledWith(API_RESOURCES.GAMES, existingDoneId.toString(), `Game doesn't have status with value "playing"`);
});

it("should return existing when game exists in database.", async() => {
await expect(service.getGameAndCheckPlayingStatus(existingPlayingId)).resolves.toStrictEqual(existingPlayingGame);
});
});

describe("cancelGame", () => {
const existingPlayingGame = createFakeGame({ status: GAME_STATUSES.PLAYING });

beforeEach(() => {
jest.spyOn(service, "getGameAndCheckPlayingStatus").mockImplementation();
});

it("should throw error when game is not playing.", async() => {
const canceledGame = createFakeGame({ status: GAME_STATUSES.CANCELED });
await expect(service.cancelGame(canceledGame)).toReject();
Expand Down

0 comments on commit e2cfd8a

Please sign in to comment.