Skip to content

Commit

Permalink
Merge pull request #2 from BenoitAverty/gogame-methods
Browse files Browse the repository at this point in the history
Gogame methods
  • Loading branch information
BenoitAverty authored Jun 16, 2016
2 parents 2a0b75e + 421f303 commit eb222ce
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"plugins": ["transform-object-rest-spread", "add-module-exports"],
"env": {
"test": {
"plugins": [["__coverage__", { "only": "src/GoGame" }]]
"plugins": [
["__coverage__", { "only": "src/GoGame" }],
"rewire",
]
}
}
}
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
sudo: false
language: node_js
cache:
directories:
- node_modules
notifications:
email: false
node_js:
- '6'
- '5'
- '4'
before_script:
- npm prune
script:
- npm run test
- npm run test:with-coverage
- npm run test:check-coverage
after_success:
# No need to build as it is done in the prepublish script
- npm run test:publish-coverage
# No need to build as it is done in the prepublish script
- npm run semantic-release
branches:
only:
Expand Down
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
"test": "test"
},
"scripts": {
"test": "npm run clean:coverage && npm run test:run && npm run test:check-coverage",
"test:run": "cross-env BABEL_ENV=test nyc --reporter=lcov --reporter=clover mocha --compilers js:babel-core/register --recursive",
"test:debug": "mocha --recursive --compilers js:babel-core/register --debug-brk",
"test": "cross-env BABEL_ENV=test mocha --compilers js:babel-core/register --recursive",
"test:with-coverage": "npm run clean:coverage && cross-env BABEL_ENV=test nyc --reporter=lcov mocha --compilers js:babel-core/register --recursive",
"test:check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
"test:publish-coverage": "cat ./coverage/lcov.info | codecov",
"prebuild": "npm run clean",
Expand Down Expand Up @@ -48,11 +47,11 @@
"babel-loader": "^6.2.4",
"babel-plugin-__coverage__": "^1.11.111",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-rewire": "^1.0.0-rc-3",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"chai": "^3.5.0",
"chai-enzyme": "^0.4.2",
"codecov.io": "^0.1.6",
"commitizen": "^2.8.2",
"cross-env": "^1.0.8",
Expand All @@ -63,11 +62,15 @@
"eslint-plugin-import": "^1.8.0",
"eslint-plugin-jsx-a11y": "^1.2.2",
"eslint-plugin-react": "^5.1.1",
"ghooks": "^1.2.4",
"mocha": "^2.5.1",
"npm-run-all": "^2.1.2",
"nyc": "^6.4.4",
"rimraf": "^2.5.2",
"semantic-release": "^4.3.5",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0",
"validate-commit-msg": "^2.6.1",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
},
Expand All @@ -82,6 +85,10 @@
"rx": "^4.1.0"
},
"config": {
"ghooks": {
"pre-commit": "npm run test:with-coverage -- -R dot && npm run test:check-coverage",
"commit-msg": "validate-commit-msg"
},
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
Expand Down
10 changes: 10 additions & 0 deletions src/GoGame/GoGame.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _forOwn from 'lodash/forOwn';

import goGameReducer from './reducer';
import actions from './actions';

Expand All @@ -19,4 +21,12 @@ GoGame.prototype = {
},
};

_forOwn(actions, (actionCreator, actionName) => {
if (actionName !== 'init') {
GoGame.prototype[actionName] = function goGameShortcutMethod(...args) {
return new GoGame(goGameReducer(this, actionCreator(...args)));
};
}
});

export default GoGame;
2 changes: 1 addition & 1 deletion src/GoGame/reducer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import playMoveReducer from './playMoveReducer';
import setMarkReducer from './setMarkReducer';

const initialGame = {
board: _map(Array(19), () => _map(Array(19), {})),
board: _map(Array(19), () => _map(Array(19), () => ({}))),
moves: [],
koCoordinates: null,
actions: [],
Expand Down
53 changes: 48 additions & 5 deletions test/game/GoGame.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/* eslint-env mocha */
import _ from 'lodash';
import { expect } from 'chai';
import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);

import { GoGame } from '../../src';
import { GoGame, actions, goGameReducer } from '../../src';

describe('GoGame', () => {
describe('default / empty GoGame object', () => {
it('Should contain a 19x19 array of empty objects', () => {
it('Should return the result of the reducer with init action', () => {
const game = new GoGame();
const expected = _.map(Array(19), () => _.map(Array(19), {}));

expect(game.board).to.deep.equal(expected);
expect(game).to.deep.equal(new GoGame(goGameReducer(undefined, actions.init())));
});

it('Should have the ko property set to false', () => {
Expand Down Expand Up @@ -49,4 +51,45 @@ describe('GoGame', () => {
expect(game.turn).to.equal('BLACK');
});
});

/* eslint-disable no-underscore-dangle */
describe('Shortcuts methods', () => {
const resultGame = { board: [], moves: [], actions: [] };
const reducerStub = sinon.stub().returns(resultGame);
before(() => {
GoGame.__Rewire__('goGameReducer', reducerStub);
});
after(() => {
GoGame.__ResetDependency__('goGameReducer');
});

it('Should not include the init() action', () => {
const game = new GoGame();
expect(game.init).to.not.exist;
});

const methods = {
playMove: [3, 3],
pass: [],
setMark: [{ i: 3, j: 3 }, 'test'],
};

_.forOwn(methods, (actionArgs, action) => {
describe(`${action} method`, () => {
it(`Should call the reducer with itself and actions.${action}(), passing its args`, () => {
const game = new GoGame();
game[action](...actionArgs);

expect(reducerStub).to.have.been.calledWith(game, actions[action](...actionArgs));
});

it('Should return the result of the reducer as a GoGame object', () => {
const game = new GoGame();
const actual = game[action](...actionArgs);

expect(actual).to.deep.equal(new GoGame(resultGame));
});
});
});
});
});
12 changes: 12 additions & 0 deletions test/game/gameReducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ describe('Game Reducer', () => {

expect(result).to.equal(state);
});

it('Should return an object with a correct board', () => {
const newGame = goGameReducer(undefined, actions.init());

expect(newGame.board).to.exist;
newGame.board.forEach((row) => {
expect(row).to.exist.and.have.length(19);
row.forEach((intersection) => {
expect(intersection).to.deep.equal({});
});
});
});
});

describe('With the playMove(i,j) action', () => {
Expand Down

0 comments on commit eb222ce

Please sign in to comment.