@@ -10,4 +10,78 @@ describe('Chess class instance', () => {
10
10
chess . loadFen ( 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' ) ;
11
11
expect ( chess . castling ) . toEqual ( { K : true , Q : true , k : true , q : true } ) ;
12
12
} ) ;
13
+
14
+ describe ( 'chess moves' , ( ) => {
15
+ it ( 'should prevent black from making a first move' , ( ) => {
16
+ expect ( ( ) => chess . move ( 'p@D7' , 'D5' ) ) . toThrowError ( ) ;
17
+ } ) ;
18
+ it ( 'should allow white to make only 1 move' , ( ) => {
19
+ expect ( ( ) => chess . move ( 'P@D2' , 'D4' ) ) . not . toThrowError ( ) ;
20
+ expect ( ( ) => chess . move ( 'P@E2' , 'E3' ) ) . toThrowError ( 'Not w turn' ) ;
21
+ } ) ;
22
+ it ( 'should record en passant target when 2 step pawn move' , ( ) => {
23
+ chess . move ( 'P@D2' , 'D4' ) ;
24
+ expect ( chess . enPassantTarget ) . toBe ( 'D3' ) ;
25
+ chess . move ( 'p@D7' , 'D5' ) ;
26
+ expect ( chess . enPassantTarget ) . toBe ( 'D6' ) ;
27
+ chess . move ( 'P@C2' , 'C3' ) ;
28
+ expect ( chess . enPassantTarget ) . toBe ( null ) ;
29
+ } ) ;
30
+ it ( 'should move rooks when castling queen side' , ( ) => {
31
+ chess . move ( 'P@D2' , 'D4' ) ;
32
+ chess . move ( 'p@D7' , 'D5' ) ;
33
+ chess . move ( 'P@B2' , 'B3' ) ;
34
+ chess . move ( 'p@B7' , 'B6' ) ;
35
+ chess . move ( 'B@C1' , 'B2' ) ;
36
+ chess . move ( 'b@C8' , 'B7' ) ;
37
+ chess . move ( 'N@B1' , 'C3' ) ;
38
+ chess . move ( 'n@B8' , 'C6' ) ;
39
+ chess . move ( 'Q@D1' , 'D2' ) ;
40
+ chess . move ( 'q@D8' , 'D7' ) ;
41
+
42
+ // white queen side castle
43
+ chess . move ( 'K@E1' , 'C1' ) ;
44
+ let board = chess . getBoard ( ) ;
45
+ expect ( board [ 7 ] [ 0 ] ) . toBe ( null ) ;
46
+ expect ( board [ 7 ] [ 3 ] ) . toBe ( 'R@D1' ) ;
47
+
48
+ // black queen side castle
49
+ chess . move ( 'k@E8' , 'C8' ) ;
50
+ board = chess . getBoard ( ) ;
51
+ expect ( board [ 0 ] [ 0 ] ) . toBe ( null ) ;
52
+ expect ( board [ 0 ] [ 3 ] ) . toBe ( 'r@D8' ) ;
53
+ } ) ;
54
+ it ( 'should move rooks when castling king side' , ( ) => {
55
+ chess . move ( 'P@E2' , 'E4' ) ;
56
+ chess . move ( 'p@E7' , 'E5' ) ;
57
+ chess . move ( 'P@G2' , 'G3' ) ;
58
+ chess . move ( 'p@G7' , 'G6' ) ;
59
+ chess . move ( 'B@F1' , 'G2' ) ;
60
+ chess . move ( 'b@F8' , 'G7' ) ;
61
+ chess . move ( 'N@G1' , 'F3' ) ;
62
+ chess . move ( 'n@G8' , 'F6' ) ;
63
+
64
+ // white king side castle
65
+ chess . move ( 'K@E1' , 'G1' ) ;
66
+ let board = chess . getBoard ( ) ;
67
+ expect ( board [ 7 ] [ 7 ] ) . toBe ( null ) ;
68
+ expect ( board [ 7 ] [ 5 ] ) . toBe ( 'R@F1' ) ;
69
+
70
+ // black king side castle
71
+ chess . move ( 'k@E8' , 'G8' ) ;
72
+ board = chess . getBoard ( ) ;
73
+ expect ( board [ 0 ] [ 7 ] ) . toBe ( null ) ;
74
+ expect ( board [ 0 ] [ 5 ] ) . toBe ( 'r@F8' ) ;
75
+ } ) ;
76
+ it ( 'should mark game as done if checkmate' , ( ) => {
77
+ chess . move ( 'P@E2' , 'E4' ) ;
78
+ chess . move ( 'p@E7' , 'E5' ) ;
79
+ chess . move ( 'B@F1' , 'C4' ) ;
80
+ chess . move ( 'p@D7' , 'D6' ) ;
81
+ chess . move ( 'Q@D1' , 'F3' ) ;
82
+ chess . move ( 'n@B8' , 'C6' ) ;
83
+ chess . move ( 'Q@F3' , 'F7' ) ;
84
+ expect ( chess . isDone ( ) ) . toBe ( true ) ;
85
+ } ) ;
86
+ } ) ;
13
87
} ) ;
0 commit comments