11const { JSDOM } = require ( 'jsdom' ) ;
2- const fs = require ( 'fs' ) ;
3- const path = require ( 'path' ) ;
4-
5- function loadScripts ( window ) {
6- [ 'js/utils.js' , 'js/pieces.js' , 'js/board.js' , 'js/game.js' ] . forEach ( ( p ) => {
7- const script = window . document . createElement ( 'script' ) ;
8- script . textContent = fs . readFileSync ( path . resolve ( p ) , 'utf-8' ) ;
9- window . document . body . appendChild ( script ) ;
10- } ) ;
11- }
122
133describe ( 'integration: game flow' , ( ) => {
14- let window ;
4+ let window , initializeGame , handleBoardClick ;
155
166 beforeEach ( ( ) => {
177 const dom = new JSDOM (
@@ -20,20 +10,66 @@ describe('integration: game flow', () => {
2010 <ul id="move-history"></ul>
2111 <div id="game-status"></div>
2212 <button id="reset-button"></button>
13+ <button id="undo-button"></button>
2314 </body>` ,
2415 { url : 'http://localhost' }
2516 ) ;
2617 window = dom . window ;
2718 global . window = window ;
2819 global . document = window . document ;
29- loadScripts ( window ) ;
30- window . initializeGame ( ) ;
20+ global . localStorage = window . localStorage ;
21+
22+ // Set up globals in dependency order
23+ const config = require ( '../../js/config' ) ;
24+ global . CONFIG = config . CONFIG ;
25+
26+ const utils = require ( '../../js/utils' ) ;
27+ global . coordsToAlgebraic = utils . coordsToAlgebraic ;
28+ global . algebraicToCoords = utils . algebraicToCoords ;
29+ global . isWithinBounds = utils . isWithinBounds ;
30+ global . deepClone = utils . deepClone ;
31+ global . playSound = utils . playSound ;
32+ global . debugLog = utils . debugLog ;
33+
34+ const pieces = require ( '../../js/pieces' ) ;
35+ global . PAWN = pieces . PAWN ;
36+ global . ROOK = pieces . ROOK ;
37+ global . KNIGHT = pieces . KNIGHT ;
38+ global . BISHOP = pieces . BISHOP ;
39+ global . QUEEN = pieces . QUEEN ;
40+ global . KING = pieces . KING ;
41+ global . WHITE = pieces . WHITE ;
42+ global . BLACK = pieces . BLACK ;
43+ global . Piece = pieces . Piece ;
44+ global . isPseudoLegalMove = pieces . isPseudoLegalMove ;
45+ global . getInitialPieces = pieces . getInitialPieces ;
46+
47+ const board = require ( '../../js/board' ) ;
48+ global . boardElement = board . boardElement ;
49+ global . createBoard = board . createBoard ;
50+ global . renderPieces = board . renderPieces ;
51+ global . getSquareElement = board . getSquareElement ;
52+ global . updateBoardVisuals = board . updateBoardVisuals ;
53+ global . setSelectedSquare = board . setSelectedSquare ;
54+ global . clearSelectedSquare = board . clearSelectedSquare ;
55+ global . setPossibleMoves = board . setPossibleMoves ;
56+ global . clearPossibleMoves = board . clearPossibleMoves ;
57+
58+ const game = require ( '../../js/game' ) ;
59+ initializeGame = game . initializeGame ;
60+ handleBoardClick = game . handleBoardClick ;
61+
62+ // Also attach to window for code that expects window.*
63+ window . initializeGame = initializeGame ;
64+ window . handleBoardClick = handleBoardClick ;
65+
66+ initializeGame ( ) ;
3167 } ) ;
3268
3369 test ( 'select move updates status and history' , ( ) => {
3470 // e2 -> e4 (6,4 to 4,4)
35- window . handleBoardClick ( 6 , 4 ) ;
36- window . handleBoardClick ( 4 , 4 ) ;
71+ handleBoardClick ( 6 , 4 ) ;
72+ handleBoardClick ( 4 , 4 ) ;
3773 expect ( window . document . getElementById ( 'game-status' ) . textContent ) . toMatch ( / B l a c k ' s t u r n / ) ;
3874 const items = window . document . querySelectorAll ( '#move-history li .white-move' ) ;
3975 expect ( items . length ) . toBeGreaterThan ( 0 ) ;
0 commit comments