Skip to content

Commit

Permalink
Merge branch 'unit-tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
everyonesdesign committed Jul 27, 2018
2 parents 56a6a93 + 103d286 commit bc4a6ed
Show file tree
Hide file tree
Showing 4 changed files with 765 additions and 26 deletions.
40 changes: 17 additions & 23 deletions app/src/chess.js
@@ -1,3 +1,4 @@
const get = require('lodash/get');
const {
sendDataToAnalytics,
} = require('./analytics');
Expand Down Expand Up @@ -36,34 +37,27 @@ function parseMoveText(input) {
* @return {ChessBoard?}
*/
function getBoard() {
// board for training with computer
const computerBoard = window.myEvent.capturingBoard;
if (computerBoard) {
return computerBoard;
}
let cb = (
// board for training with computer
get(window, 'myEvent.capturingBoard') ||
// new live mode
get(window, 'liveClient.controller.activeBoard.chessboard')
);

// old live mode: probably not working anywhere now
if (window.boardsService && window.boardsService.getSelectedBoard) {
const activeBoard = window.boardsService.getSelectedBoard();

if (activeBoard) {
return activeBoard.chessboard;
}
}
if (!cb) {
// legacy old chessboard
// probably should be removed
if (window.boardsService && window.boardsService.getSelectedBoard) {
const activeBoard = window.boardsService.getSelectedBoard();

// new live mode
const lc = window.liveClient;
if (
lc &&
lc.controller &&
lc.controller.activeBoard &&
lc.controller.activeBoard.chessboard
) {
return lc.controller.activeBoard.chessboard;
if (activeBoard) {
return activeBoard.chessboard;
}
}
}


return null;
return cb || null;
}

/**
Expand Down
59 changes: 59 additions & 0 deletions app/test/test-chess.js
@@ -0,0 +1,59 @@
require('jsdom-global')();
const assert = require('assert');

const {
getBoard,
} = require('../src/chess');

describe('getBoard', function() {
it('should return board for computer chess', function() {
window.myEvent = {
capturingBoard: 'chessboard!',
};

const board = getBoard();

assert.equal(board, 'chessboard!');

delete window.myEvent;
});

it('should return board for old live chess', function() {
window.boardsService = {
getSelectedBoard() {
return {
chessboard: 'chessboard!',
};
},
};

const board = getBoard();

assert.equal(board, 'chessboard!');

delete window.boardsService;
});

it('should return board for new live chess', function() {
window.liveClient = {
controller: {
activeBoard: {
chessboard: 'chessboard!',
},
},
};

const board = getBoard();

assert.equal(board, 'chessboard!');

delete window.liveClient;
});

it('should return null if theres no board', function() {
const board = getBoard();

assert.strictEqual(board, null);
});
});

0 comments on commit bc4a6ed

Please sign in to comment.