@@ -6,55 +6,94 @@ namespace CodeBreaker.ViewModels.Tests;
66
77public class GamePageViewModelTests
88{
9- private readonly GamePageViewModel _viewModel ;
9+ private static readonly string [ ] s_code = [ "Green" , "Blue" , "Yellow" , "Orange" ] ;
10+ private static readonly ( string [ ] GuessPegs , string [ ] KeyPegs , bool HasEnded , bool IsVictory ) [ ] s_moves = [
11+ ( [ "Red" , "Red" , "Red" , "Red" ] , [ ] , false , false ) ,
12+ ( [ "Green" , "Green" , "Green" , "Green" ] , [ "Black" ] , false , false ) ,
13+ ( [ "Blue" , "Green" , "Green" , "Green" ] , [ "White" , "White" ] , false , false ) ,
14+ ( [ "Green" , "Blue" , "Blue" , "Blue" ] , [ "Black" , "Black" ] , false , false ) ,
15+ ( [ "Green" , "Blue" , "Yellow" , "Yellow" ] , [ "Black" , "Black" , "Black" ] , false , false ) ,
16+ ( [ "Green" , "Blue" , "Yellow" , "Purple" ] , [ "Black" , "Black" , "Black" ] , false , false ) ,
17+ ( [ "Green" , "Blue" , "Yellow" , "Orange" ] , [ "Black" , "Black" , "Black" , "Black" ] , true , true ) ,
18+ ] ;
19+
20+ private readonly Mock < IGamesClient > _gamesClientMock ;
21+ private readonly Mock < IInfoBarService > _infoBarServiceMock ;
1022
1123 public GamePageViewModelTests ( )
1224 {
13- ( Guid GameType , int NumberCodes , int MaxMoves , IDictionary < string , string [ ] > FieldValues ) returnValue =
25+ ( Guid Id , int NumberCodes , int MaxMoves , IDictionary < string , string [ ] > FieldValues ) gameReturnValue =
1426 ( Guid . NewGuid ( ) , 4 , 12 , new Dictionary < string , string [ ] > ( )
1527 {
16- { "colors" , new string [ ] { "Black" , "White" , "Red" , "Green" , "Blue" , "Yellow" } }
28+ { "colors" , [ "Black" , "White" , "Red" , "Green" , "Blue" , "Yellow" ] }
1729 } ) ;
1830
19- Mock < IGamesClient > gameClient = new ( ) ;
20- gameClient . Setup (
21- client => client . StartGameAsync ( GameType . Game6x4 , "Test" , CancellationToken . None ) ) . ReturnsAsync ( returnValue ) ;
22-
23- Mock < IOptions < GamePageViewModelOptions > > options = new ( ) ;
24- options . Setup ( o => o . Value ) . Returns ( new GamePageViewModelOptions ( ) ) ;
31+ _gamesClientMock = new ( ) ;
32+ // Setup the StartGameAsync method of the GamesClient.
33+ _gamesClientMock . Setup ( client => client . StartGameAsync ( GameType . Game6x4 , "Test" , CancellationToken . None ) ) . ReturnsAsync ( gameReturnValue ) ;
2534
26- Mock < IDialogService > dialogService = new ( ) ;
27- Mock < IInfoBarService > infoBarService = new ( ) ;
35+ // Setup the SetMoveAsync method of the GamesClient.
36+ for ( int i = 0 ; i < s_moves . Length ; i ++ )
37+ {
38+ var move = s_moves [ i ] ;
39+ _gamesClientMock . Setup ( client => client . SetMoveAsync ( gameReturnValue . Id , "Test" , GameType . Game6x4 , i + 1 , move . GuessPegs , CancellationToken . None ) )
40+ . ReturnsAsync ( ( move . KeyPegs , move . HasEnded , move . IsVictory ) ) ;
41+ }
2842
29- _viewModel = new GamePageViewModel ( gameClient . Object , options . Object , dialogService . Object , infoBarService . Object ) ;
43+ _infoBarServiceMock = new ( ) ;
3044 }
3145
3246 [ Fact ]
33- public async Task TestGameModeStartedAfterStart ( )
47+ public async Task TestGameStart ( )
3448 {
35- _viewModel . Name = "Test" ;
36- await _viewModel . StartGameCommand . ExecuteAsync ( null ) ;
49+ var viewModel = new GamePageViewModel ( _gamesClientMock . Object , _infoBarServiceMock . Object ) ;
50+ viewModel . Username = "Test" ;
51+ await viewModel . StartGameCommand . ExecuteAsync ( null ) ;
3752
38- Assert . Equal ( GameMode . Started , _viewModel . GameStatus ) ;
53+ Assert . Equal ( 4 , viewModel . SelectedFields . Length ) ;
54+ Assert . All ( viewModel . SelectedFields , field => Assert . NotNull ( field ) ) ;
55+ Assert . NotNull ( viewModel . Game ) ;
3956 }
4057
4158 [ Fact ]
42- public async Task TestInProgressNotificationAfterStart ( )
59+ public async Task TestIsLoadingNotificationAfterStart ( )
4360 {
44- List < bool > expected = new ( ) { true , false } ;
45- _viewModel . Name = "Test" ;
46- List < bool > inProgressValues = new ( ) ;
61+ var viewModel = new GamePageViewModel ( _gamesClientMock . Object , _infoBarServiceMock . Object ) ;
62+ List < bool > expectedIsLoadingValues = [ true , false ] ;
63+ List < bool > actualInProgressValues = [ ] ;
64+ viewModel . Username = "Test" ;
4765
48- _viewModel . PropertyChanged += ( sender , e ) =>
66+ viewModel . PropertyChanged += ( sender , e ) =>
4967 {
50- if ( e . PropertyName is "InProgress" )
51- {
52- inProgressValues . Add ( _viewModel . InProgress ) ;
53- }
68+ if ( e . PropertyName is nameof ( GamePageViewModel . IsLoading ) )
69+ actualInProgressValues . Add ( viewModel . IsLoading ) ;
5470 } ;
5571
56- await _viewModel . StartGameCommand . ExecuteAsync ( null ) ;
72+ await viewModel . StartGameCommand . ExecuteAsync ( null ) ;
73+
74+ Assert . Equal ( expectedIsLoadingValues , actualInProgressValues ) ;
75+ }
76+
77+ [ Fact ]
78+ public async Task TestMoves ( )
79+ {
80+ // Start game
81+ var viewModel = new GamePageViewModel ( _gamesClientMock . Object , _infoBarServiceMock . Object ) ;
82+ viewModel . Username = "Test" ;
83+
84+ await viewModel . StartGameCommand . ExecuteAsync ( null ) ;
85+
86+ // Play game
87+ foreach ( ( string [ ] guessPegs , _ , _ , _ ) in s_moves )
88+ {
89+ for ( int i = 0 ; i < guessPegs . Length ; i ++ )
90+ viewModel . SelectedFields [ i ] . Color = guessPegs [ i ] ;
91+
92+ await viewModel . MakeMoveCommand . ExecuteAsync ( null ) ;
93+ }
5794
58- Assert . Equal ( expected , inProgressValues ) ;
95+ Assert . Equal ( s_moves . Length , viewModel . Game ? . Moves . Count ) ;
96+ Assert . NotNull ( viewModel . Game ? . EndTime ) ;
97+ Assert . True ( viewModel . Game ? . IsVictory ) ;
5998 }
6099}
0 commit comments