Skip to content

Commit

Permalink
Merge pull request #175 from CodebreakerApp/57-viewmodels
Browse files Browse the repository at this point in the history
Fixed and enhanced ViewModel tests
  • Loading branch information
christiannagel authored Mar 20, 2024
2 parents 2244380 + 08f1b31 commit e4c23a4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
93 changes: 66 additions & 27 deletions src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,94 @@ namespace CodeBreaker.ViewModels.Tests;

public class GamePageViewModelTests
{
private readonly GamePageViewModel _viewModel;
private static readonly string[] s_code = ["Green", "Blue", "Yellow", "Orange"];
private static readonly (string[] GuessPegs, string[] KeyPegs, bool HasEnded, bool IsVictory)[] s_moves = [
(["Red", "Red", "Red", "Red"], [], false, false),
(["Green", "Green", "Green", "Green"], ["Black"], false, false),
(["Blue", "Green", "Green", "Green"], ["White", "White"], false, false),
(["Green", "Blue", "Blue", "Blue"], ["Black", "Black"], false, false),
(["Green", "Blue", "Yellow", "Yellow"], ["Black", "Black", "Black"], false, false),
(["Green", "Blue", "Yellow", "Purple"], ["Black", "Black", "Black"], false, false),
(["Green", "Blue", "Yellow", "Orange"], ["Black", "Black", "Black", "Black"], true, true),
];

private readonly Mock<IGamesClient> _gamesClientMock;
private readonly Mock<IInfoBarService> _infoBarServiceMock;

public GamePageViewModelTests()
{
(Guid GameType, int NumberCodes, int MaxMoves, IDictionary<string, string[]> FieldValues) returnValue =
(Guid Id, int NumberCodes, int MaxMoves, IDictionary<string, string[]> FieldValues) gameReturnValue =
(Guid.NewGuid(), 4, 12, new Dictionary<string, string[]>()
{
{ "colors", new string[] { "Black", "White", "Red", "Green", "Blue", "Yellow" } }
{ "colors", ["Black", "White", "Red", "Green", "Blue", "Yellow"] }
});

Mock<IGamesClient> gameClient = new();
gameClient.Setup(
client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(returnValue);

Mock<IOptions<GamePageViewModelOptions>> options = new();
options.Setup(o => o.Value).Returns(new GamePageViewModelOptions());
_gamesClientMock = new();
// Setup the StartGameAsync method of the GamesClient.
_gamesClientMock.Setup(client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(gameReturnValue);

Mock<IDialogService> dialogService = new();
Mock<IInfoBarService> infoBarService = new();
// Setup the SetMoveAsync method of the GamesClient.
for (int i = 0; i < s_moves.Length; i++)
{
var move = s_moves[i];
_gamesClientMock.Setup(client => client.SetMoveAsync(gameReturnValue.Id, "Test", GameType.Game6x4, i + 1, move.GuessPegs, CancellationToken.None))
.ReturnsAsync((move.KeyPegs, move.HasEnded, move.IsVictory));
}

_viewModel = new GamePageViewModel(gameClient.Object, options.Object, dialogService.Object, infoBarService.Object);
_infoBarServiceMock = new();
}

[Fact]
public async Task TestGameModeStartedAfterStart()
public async Task TestGameStart()
{
_viewModel.Name = "Test";
await _viewModel.StartGameCommand.ExecuteAsync(null);
var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object);
viewModel.Username = "Test";
await viewModel.StartGameCommand.ExecuteAsync(null);

Assert.Equal(GameMode.Started, _viewModel.GameStatus);
Assert.Equal(4, viewModel.SelectedFields.Length);
Assert.All(viewModel.SelectedFields, field => Assert.NotNull(field));
Assert.NotNull(viewModel.Game);
}

[Fact]
public async Task TestInProgressNotificationAfterStart()
public async Task TestIsLoadingNotificationAfterStart()
{
List<bool> expected = new() { true, false };
_viewModel.Name = "Test";
List<bool> inProgressValues = new();
var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object);
List<bool> expectedIsLoadingValues = [true, false];
List<bool> actualInProgressValues = [];
viewModel.Username = "Test";

_viewModel.PropertyChanged += (sender, e) =>
viewModel.PropertyChanged += (sender, e) =>
{
if (e.PropertyName is "InProgress")
{
inProgressValues.Add(_viewModel.InProgress);
}
if (e.PropertyName is nameof(GamePageViewModel.IsLoading))
actualInProgressValues.Add(viewModel.IsLoading);
};

await _viewModel.StartGameCommand.ExecuteAsync(null);
await viewModel.StartGameCommand.ExecuteAsync(null);

Assert.Equal(expectedIsLoadingValues, actualInProgressValues);
}

[Fact]
public async Task TestMoves()
{
// Start game
var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object);
viewModel.Username = "Test";

await viewModel.StartGameCommand.ExecuteAsync(null);

// Play game
foreach ((string[] guessPegs, _, _, _) in s_moves)
{
for (int i = 0; i < guessPegs.Length; i++)
viewModel.SelectedFields[i].Color = guessPegs[i];

await viewModel.MakeMoveCommand.ExecuteAsync(null);
}

Assert.Equal(expected, inProgressValues);
Assert.Equal(s_moves.Length, viewModel.Game?.Moves.Count);
Assert.NotNull(viewModel.Game?.EndTime);
Assert.True(viewModel.Game?.IsVictory);
}
}

0 comments on commit e4c23a4

Please sign in to comment.