Skip to content

Commit

Permalink
test(object-equality): use strictEqual instead of matchObject (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed May 7, 2023
1 parent 00be608 commit 1cf3779
Show file tree
Hide file tree
Showing 24 changed files with 23,802 additions and 24,439 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from "@nestjs/swagger";
import { Transform, Type } from "class-transformer";
import { Expose, Transform, Type } from "class-transformer";
import { IsInt, IsString, MaxLength, Min, MinLength, ValidateNested } from "class-validator";
import { playerApiProperties, playersFieldsSpecs } from "../../../constants/player/player.constant";
import { GamePlayerRoleBaseDto } from "./game-player-role/game-player-role.base.dto";
Expand All @@ -12,23 +12,27 @@ class GamePlayerBaseDto {
@IsString()
@MinLength(playersFieldsSpecs.name.minLength)
@MaxLength(playersFieldsSpecs.name.maxLength)
@Expose()
public name: string;

@ApiProperty(playerApiProperties.role)
@Transform(playerRoleTransformer)
@Type(() => GamePlayerRoleBaseDto)
@ValidateNested()
@Expose()
public role: GamePlayerRoleBaseDto;

@ApiProperty(playerApiProperties.role)
@Transform(playerSideTransformer)
@Type(() => GamePlayerRoleBaseDto)
@ValidateNested()
@Expose()
public side: GamePlayerSideBaseDto;

@ApiProperty(playerApiProperties.position)
@IsInt()
@Min(playersFieldsSpecs.position.minimum)
@Expose()
public position: number;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { ApiHideProperty, PickType } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { Allow } from "class-validator";
import { ROLE_NAMES } from "../../../../../role/enums/role.enum";
import { GamePlayerRoleBaseDto } from "../../../base/game-player/game-player-role/game-player-role.base.dto";

class CreateGamePlayerRoleDto extends PickType(GamePlayerRoleBaseDto, ["name"] as const) {
@ApiHideProperty()
@Allow()
@Expose()
public original?: ROLE_NAMES;

@ApiHideProperty()
@Allow()
@Expose()
public current?: ROLE_NAMES;

@ApiHideProperty()
@Allow()
@Expose()
public isRevealed?: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { ApiHideProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { Allow } from "class-validator";
import { ROLE_SIDES } from "../../../../../role/enums/role.enum";

class CreateGamePlayerSideDto {
@ApiHideProperty()
@Allow()
@Expose()
public original?: ROLE_SIDES;

@ApiHideProperty()
@Allow()
@Expose()
public current?: ROLE_SIDES;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiHideProperty, ApiProperty, IntersectionType, PartialType, PickType } from "@nestjs/swagger";
import { Transform, Type } from "class-transformer";
import { Expose, Transform, Type } from "class-transformer";
import { ValidateNested } from "class-validator";
import { playerApiProperties } from "../../../constants/player/player.constant";
import { GamePlayerBaseDto } from "../../base/game-player/game-player.base.dto";
Expand All @@ -16,15 +16,18 @@ class CreateGamePlayerDto extends IntersectionType(
@ApiProperty(playerApiProperties.role)
@Type(() => CreateGamePlayerRoleDto)
@ValidateNested()
@Expose()
public role: CreateGamePlayerRoleDto;

@ApiHideProperty()
@Type(() => CreateGamePlayerSideDto)
@ValidateNested()
@Transform(playerSideTransformer)
@Expose()
public side: CreateGamePlayerSideDto = {};

@ApiProperty({ description: "Player's unique position among all players. Maximum is `players.length - 1`. Either all players position must be set or none of them. In that last case, it will be generated automatically" })
@Expose()
public position?: number;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { OmitType } from "@nestjs/swagger";
import type { Player } from "../../../schemas/player/player.schema";
import { Expose } from "class-transformer";
import { Player } from "../../../schemas/player/player.schema";
import { MakeGamePlayTargetDto } from "./make-game-play-target.dto";

class MakeGamePlayTargetWithRelationsDto extends OmitType(MakeGamePlayTargetDto, ["playerId"] as const) {
@Expose()
public player: Player;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { Expose, Type } from "class-transformer";
import { IsBoolean, IsEnum, IsMongoId, IsOptional } from "class-validator";
import { Types } from "mongoose";
import { GAME_PLAY_ACTIONS, WITCH_POTIONS } from "../../../enums/game-play.enum";
Expand All @@ -8,17 +8,18 @@ class MakeGamePlayTargetDto {
@ApiProperty({ description: `Player's Id` })
@Type(() => String)
@IsMongoId()
// @Transform(stringToObjectIdTransformer)
public playerId: Types.ObjectId;

@ApiProperty({ description: `Can be set only if there is a \`vile father of wolves\` in the game and game's upcoming action is \`${GAME_PLAY_ACTIONS.EAT}\`. If set to \`true\`, the \`werewolves\` victim will instantly join the \`werewolves\` side if possible.` })
@IsOptional()
@IsBoolean()
@Expose()
public isInfected?: boolean;

@ApiProperty({ description: `Can be set only if game's upcoming action is \`${GAME_PLAY_ACTIONS.USE_POTIONS}\`. If set to \`${WITCH_POTIONS.LIFE}\`, the \`witch\` saves target's life from \`werewolves\` meal. If set to \`${WITCH_POTIONS.DEATH}\`, the \`witch\` kills the target` })
@IsOptional()
@IsEnum(WITCH_POTIONS)
@Expose()
public drankPotion?: WITCH_POTIONS;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { OmitType } from "@nestjs/swagger";
import type { GameAdditionalCard } from "../../schemas/game-additional-card/game-additional-card.schema";
import type { MakeGamePlayTargetWithRelationsDto } from "./make-game-play-target/make-game-play-target-with-relations.dto";
import type { MakeGamePlayVoteWithRelationsDto } from "./make-game-play-vote/make-game-play-vote-with-relations.dto";
import { Expose, Type } from "class-transformer";
import { GameAdditionalCard } from "../../schemas/game-additional-card/game-additional-card.schema";
import { MakeGamePlayTargetWithRelationsDto } from "./make-game-play-target/make-game-play-target-with-relations.dto";
import { MakeGamePlayVoteWithRelationsDto } from "./make-game-play-vote/make-game-play-vote-with-relations.dto";
import { MakeGamePlayDto } from "./make-game-play.dto";

class MakeGamePlayWithRelationsDto extends OmitType(MakeGamePlayDto, ["targets", "votes", "chosenCardId"] as const) {
@Expose()
@Type(() => MakeGamePlayTargetWithRelationsDto)
public targets?: MakeGamePlayTargetWithRelationsDto[];

@Expose()
@Type(() => MakeGamePlayVoteWithRelationsDto)
public votes?: MakeGamePlayVoteWithRelationsDto[];


@Expose()
public chosenCard?: GameAdditionalCard;
}

Expand Down
7 changes: 6 additions & 1 deletion src/modules/game/dto/make-game-play/make-game-play.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { Expose, Type } from "class-transformer";
import { ArrayUnique, IsArray, IsBoolean, IsEnum, IsMongoId, IsOptional, ValidateNested } from "class-validator";
import { Types } from "mongoose";
import { ROLE_NAMES, ROLE_SIDES } from "../../../role/enums/role.enum";
Expand All @@ -15,6 +15,7 @@ class MakeGamePlayDto {
@ValidateNested()
@IsArray()
@ArrayUnique((target: MakeGamePlayTargetDto) => target.playerId, { message: "targets.playerId must be unique" })
@Expose()
public targets?: MakeGamePlayTargetDto[];

@ApiProperty({ description: `Players votes. Must be set when game's upcoming play action is one of the following : ${requiredVotesActions.toString()}` })
Expand All @@ -23,22 +24,26 @@ class MakeGamePlayDto {
@ValidateNested()
@IsArray()
@ArrayUnique((vote: MakeGamePlayVoteDto) => vote.sourceId, { message: "votes.sourceId must be unique" })
@Expose()
public votes?: MakeGamePlayVoteDto[];

@ApiProperty({ description: `Can be set to \`true\` only if there is a \`stuttering judge\` in the game and the game's upcoming action is one of the following : ${stutteringJudgeRequestOpportunityActions.toString()}. If set to \`true\`, there is another vote immediately` })
@IsOptional()
@IsBoolean()
@Expose()
public doesJudgeRequestAnotherVote?: boolean;

@ApiProperty({ description: `Can be set when game's upcoming action is \`${GAME_PLAY_ACTIONS.CHOOSE_CARD}\`` })
@IsOptional()
@Type(() => String)
@IsMongoId()
@Expose()
public chosenCardId?: Types.ObjectId;

@ApiProperty({ description: `Side chosen by \`${ROLE_NAMES.DOG_WOLF}\`. Required when game's upcoming action is \`${GAME_PLAY_ACTIONS.CHOOSE_SIDE}\`` })
@IsOptional()
@IsEnum(ROLE_SIDES)
@Expose()
public chosenSide?: ROLE_SIDES;
}

Expand Down
9 changes: 6 additions & 3 deletions src/modules/game/helpers/game-play.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { plainToInstance } from "class-transformer";
import { API_RESOURCES } from "../../../shared/api/enums/api.enum";
import { RESOURCE_NOT_FOUND_REASONS } from "../../../shared/error/enums/resource-not-found-error.enum";
import { ResourceNotFoundError } from "../../../shared/error/types/resource-not-found-error.type";
import { plainToInstanceDefaultOptions } from "../../../shared/validation/constants/validation.constant";
import { MakeGamePlayTargetWithRelationsDto } from "../dto/make-game-play/make-game-play-target/make-game-play-target-with-relations.dto";
import { MakeGamePlayVoteWithRelationsDto } from "../dto/make-game-play/make-game-play-vote/make-game-play-vote-with-relations.dto";
import { MakeGamePlayWithRelationsDto } from "../dto/make-game-play/make-game-play-with-relations.dto";
Expand All @@ -23,7 +24,8 @@ function getVotesWithRelationsFromMakeGamePlayDto(makeGamePlayDto: MakeGamePlayD
if (target === undefined) {
throw new ResourceNotFoundError(API_RESOURCES.PLAYERS, vote.targetId.toString(), RESOURCE_NOT_FOUND_REASONS.UNMATCHED_GAME_PLAY_PLAYER_VOTE_TARGET);
}
const voteWithRelations = plainToInstance(MakeGamePlayVoteWithRelationsDto, { ...vote, source, target }, { enableCircularCheck: true });
const plainToInstanceOptions = { ...plainToInstanceDefaultOptions, excludeExtraneousValues: true };
const voteWithRelations = plainToInstance(MakeGamePlayVoteWithRelationsDto, { ...vote, source, target }, plainToInstanceOptions);
return [...acc, voteWithRelations];
}, []);
}
Expand All @@ -37,7 +39,8 @@ function getTargetsWithRelationsFromMakeGamePlayDto(makeGamePlayDto: MakeGamePla
if (player === undefined) {
throw new ResourceNotFoundError(API_RESOURCES.PLAYERS, target.playerId.toString(), RESOURCE_NOT_FOUND_REASONS.UNMATCHED_GAME_PLAY_PLAYER_TARGET);
}
const targetWithRelations = plainToInstance(MakeGamePlayTargetWithRelationsDto, { ...target, player }, { enableCircularCheck: true });
const plainToInstanceOptions = { ...plainToInstanceDefaultOptions, excludeExtraneousValues: true };
const targetWithRelations = plainToInstance(MakeGamePlayTargetWithRelationsDto, { ...target, player }, plainToInstanceOptions);
return [...acc, targetWithRelations];
}, []);
}
Expand All @@ -62,7 +65,7 @@ function createMakeGamePlayDtoWithRelations(makeGamePlayDto: MakeGamePlayDto, ga
chosenCard,
targets,
votes,
});
}, { ...plainToInstanceDefaultOptions, excludeExtraneousValues: true });
}

export {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { Expose, Type } from "class-transformer";
import { Types } from "mongoose";
import { ROLE_NAMES } from "../../../role/enums/role.enum";
import { gameAdditionalCardApiProperties, gameAdditionalCardFieldsSpecs } from "../../constants/game-additional-card/game-additional-card.constant";
Expand All @@ -9,24 +9,28 @@ import { gameAdditionalCardApiProperties, gameAdditionalCardFieldsSpecs } from "
class GameAdditionalCard {
@ApiProperty(gameAdditionalCardApiProperties._id)
@Type(() => String)
@Expose()
public _id: Types.ObjectId;

@ApiProperty(gameAdditionalCardApiProperties.roleName)
@Prop({
required: gameAdditionalCardFieldsSpecs.roleName.required,
enum: gameAdditionalCardFieldsSpecs.roleName.enum,
})
@Expose()
public roleName: ROLE_NAMES;

@ApiProperty(gameAdditionalCardApiProperties.recipient)
@Prop({
required: gameAdditionalCardFieldsSpecs.recipient.required,
enum: gameAdditionalCardFieldsSpecs.recipient.enum,
})
@Expose()
public recipient: ROLE_NAMES.THIEF;

@ApiProperty(gameAdditionalCardApiProperties.isUsed)
@Prop({ default: gameAdditionalCardFieldsSpecs.isUsed.default as boolean })
@Expose()
public isUsed: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { ROLE_SIDES } from "../../../../role/enums/role.enum";
import { gameHistoryRecordPlayApiProperties, gameHistoryRecordPlayFieldsSpecs } from "../../../constants/game-history-record/game-history-record-play/game-history-record-play.constant";
import { GAME_HISTORY_RECORD_VOTING_RESULTS } from "../../../enums/game-history-record.enum";
Expand All @@ -22,52 +23,62 @@ class GameHistoryRecordPlay {
required: gameHistoryRecordPlayFieldsSpecs.action.required,
enum: gameHistoryRecordPlayFieldsSpecs.action.enum,
})
@Expose()
public action: GAME_PLAY_ACTIONS;

@ApiProperty(gameHistoryRecordPlayApiProperties.source)
@Prop({
required: gameHistoryRecordPlayFieldsSpecs.source.required,
type: GameHistoryRecordPlaySourceSchema,
})
@Expose()
public source: GameHistoryRecordPlaySource;

@ApiProperty(gameHistoryRecordPlayApiProperties.targets)
@Prop({
required: gameHistoryRecordPlayFieldsSpecs.targets.required,
type: [GameHistoryRecordPlayTargetSchema],
default: undefined,
})
@Expose()
public targets?: GameHistoryRecordPlayTarget[];

@ApiProperty(gameHistoryRecordPlayApiProperties.votes)
@Prop({
required: gameHistoryRecordPlayFieldsSpecs.votes.required,
type: [GameHistoryRecordPlayVoteSchema],
default: undefined,
})
@Expose()
public votes?: GameHistoryRecordPlayVote[];

@ApiProperty(gameHistoryRecordPlayApiProperties.votingResult)
@Prop({
required: gameHistoryRecordPlayFieldsSpecs.votingResult.required,
enum: gameHistoryRecordPlayFieldsSpecs.votingResult.enum,
})
@Expose()
public votingResult?: GAME_HISTORY_RECORD_VOTING_RESULTS;

@ApiProperty(gameHistoryRecordPlayApiProperties.didJudgeRequestAnotherVote)
@Prop({ required: gameHistoryRecordPlayFieldsSpecs.didJudgeRequestAnotherVote.required })
@Expose()
public didJudgeRequestAnotherVote?: boolean;

@ApiProperty(gameHistoryRecordPlayApiProperties.chosenCard)
@Prop({
required: gameHistoryRecordPlayFieldsSpecs.chosenCard.required,
type: GameAdditionalCardSchema,
})
@Expose()
public chosenCard?: GameAdditionalCard;

@ApiProperty(gameHistoryRecordPlayApiProperties.chosenSide)
@Prop({
required: gameHistoryRecordPlayFieldsSpecs.chosenSide.required,
enum: gameHistoryRecordPlayFieldsSpecs.chosenSide.enum,
})
@Expose()
public chosenSide?: ROLE_SIDES;
}

Expand Down

0 comments on commit 1cf3779

Please sign in to comment.