Skip to content

Commit

Permalink
unitテスト追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Shota Matsushita committed Nov 23, 2016
1 parent 45cf6e9 commit 6e35133
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 7 deletions.
4 changes: 2 additions & 2 deletions App/Reducers/OutputReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export const INITIAL_STATE = immutable({
raw: '',
contents: '',
format: DIFF_FORMAT.html,
split: 'side-by-side',
split: 'line-by-line',
});

const outputResult = (state, action) =>
state.merge({
raw: action.diff,
raw: action.raw,
contents: action.contents,
});

Expand Down
4 changes: 2 additions & 2 deletions App/Sagas/InputSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { left, right } from '../Selectors/Input';
import { format, split } from '../Selectors/Output';
import Types from '../Actions/Types';
import Actions from '../Actions/Creators';
import createDiff from '../Services/CalculateDiff';
import { createDiff } from '../Services/CalculateDiff';

// 間引き時間
// const debouncing = ms => new Promise(resolve => setTimeout(resolve, ms));

function* worker() {
export function* worker() {
// Debouncing(間引き)処理
// yield call(debouncing, 1000);

Expand Down
1 change: 0 additions & 1 deletion App/Sagas/StartupSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import THEMES from '../Config/Theme';
export default function* watcher() {
yield take(Types.STARTUP);
// マウント前に設定するものがあればここに記述
console.log('startup');

/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
Expand Down
3 changes: 2 additions & 1 deletion App/Services/CalculateDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { createPatch } from 'diff';
import diff2html from 'diff2html';
import DIFF_FORMAT from '../Config/DiffFormat';

export default function createDiff(left, right, format, split) {
/* eslint-disable import/prefer-default-export*/
export function createDiff(left, right, format, split) {
const rawDiff = createPatch('result', left, right, 'before', 'after');
let contents = '';
if (format === DIFF_FORMAT.unified) {
Expand Down
39 changes: 39 additions & 0 deletions Tests/Reducers/InputReducerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'ava';
import reducer, { INITIAL_STATE } from '../../App/Reducers/InputReducer';
import Actions from '../../App/Actions/Creators';

test('changeLeft', (t) => {
const input = 'New Text';
const state = reducer(INITIAL_STATE, Actions.inputLeftChange(input));
t.is(state.left, input);
t.is(state.right, INITIAL_STATE.right);
t.is(state.language, INITIAL_STATE.language);
t.is(state.theme, INITIAL_STATE.theme);
});

test('changeRight', (t) => {
const input = 'New Text';
const state = reducer(INITIAL_STATE, Actions.inputRightChange(input));
t.is(state.left, INITIAL_STATE.left);
t.is(state.right, input);
t.is(state.language, INITIAL_STATE.language);
t.is(state.theme, INITIAL_STATE.theme);
});

test('changeLanguage', (t) => {
const language = 'java';
const state = reducer(INITIAL_STATE, Actions.inputLanguageChange(language));
t.is(state.left, INITIAL_STATE.left);
t.is(state.right, INITIAL_STATE.right);
t.is(state.language, language);
t.is(state.theme, INITIAL_STATE.theme);
});

test('changeTheme', (t) => {
const theme = 'monokai';
const state = reducer(INITIAL_STATE, Actions.inputThemeChange(theme));
t.is(state.left, INITIAL_STATE.left);
t.is(state.right, INITIAL_STATE.right);
t.is(state.language, INITIAL_STATE.language);
t.is(state.theme, theme);
});
31 changes: 31 additions & 0 deletions Tests/Reducers/OutputReducerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import test from 'ava';
import reducer, { INITIAL_STATE } from '../../App/Reducers/OutputReducer';
import Actions from '../../App/Actions/Creators';

test('outputResult', (t) => {
const raw = 'raw output';
const contents = '<html>contents</html>';
const state = reducer(INITIAL_STATE, Actions.outputDiffResult(raw, contents));
t.is(state.raw, raw);
t.is(state.contents, contents);
t.is(state.format, INITIAL_STATE.format);
t.is(state.split, INITIAL_STATE.split);
});

test('changeFormat', (t) => {
const format = 0;
const state = reducer(INITIAL_STATE, Actions.outputFormatChange(format));
t.is(state.raw, INITIAL_STATE.raw);
t.is(state.contents, INITIAL_STATE.contents);
t.is(state.format, format);
t.is(state.split, INITIAL_STATE.split);
});

test('changeSplit', (t) => {
const split = 'side-by-side';
const state = reducer(INITIAL_STATE, Actions.outputSplitChange(split));
t.is(state.raw, INITIAL_STATE.raw);
t.is(state.contents, INITIAL_STATE.contents);
t.is(state.format, INITIAL_STATE.format);
t.is(state.split, split);
});
46 changes: 46 additions & 0 deletions Tests/Sagas/InputSagaTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import test from 'ava';
import { take, select, fork } from 'redux-saga/effects';
import { left, right } from '../../App/Selectors/Input';
import { format, split } from '../../App/Selectors/Output';
import Types from '../../App/Actions/Types';
import { leftWatcher, rightWatcher, formatWatcher, splitWatcher, worker } from '../../App/Sagas/InputSaga';

// 呼び出すたびにGeneratorのstepを1つ進める
const stepper = fn => mock => fn.next(mock).value;

test('leftWatcher step test', (t) => {
const step = stepper(leftWatcher());
// takeEveryのステップとworker起動のテスト
t.deepEqual(step(), take(Types.INPUT_LEFT_CHANGE));
t.deepEqual(step(), fork(worker, undefined));
t.deepEqual(step(), take(Types.INPUT_LEFT_CHANGE));
});

test('rightWatcher step test', (t) => {
const step = stepper(rightWatcher());
t.deepEqual(step(), take(Types.INPUT_RIGHT_CHANGE));
t.deepEqual(step(), fork(worker, undefined));
t.deepEqual(step(), take(Types.INPUT_RIGHT_CHANGE));
});

test('formatWatcher step test', (t) => {
const step = stepper(formatWatcher());
t.deepEqual(step(), take(Types.OUTPUT_FORMAT_CHANGE));
t.deepEqual(step(), fork(worker, undefined));
t.deepEqual(step(), take(Types.OUTPUT_FORMAT_CHANGE));
});

test('splitWatcher step test', (t) => {
const step = stepper(splitWatcher());
t.deepEqual(step(), take(Types.OUTPUT_SPLIT_CHANGE));
t.deepEqual(step(), fork(worker, undefined));
t.deepEqual(step(), take(Types.OUTPUT_SPLIT_CHANGE));
});

test('worker step test', (t) => {
const step = stepper(worker());
t.deepEqual(step(), select(left));
t.deepEqual(step(), select(right));
t.deepEqual(step(), select(format));
t.deepEqual(step(), select(split));
});
37 changes: 37 additions & 0 deletions Tests/Services/CalculateDiffTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import test from 'ava';
import proxyquire from 'proxyquire';
import DIFF_FORMAT from '../../App/Config/DiffFormat';

test('createDiff() フォーマットがunifiedならコンテンツはunified生データが返却される', (t) => {
const diffStub = {
createPatch() {
return 'rawDiff';
},
};
const createDiffMock = proxyquire('../../App/Services/CalculateDiff', {
diff: diffStub,
});
const expected = ['rawDiff', 'rawDiff'];
t.deepEqual(createDiffMock.createDiff('left', 'right', DIFF_FORMAT.unified, 'line-by-line'), expected);
});

test('createDiff() フォーマットがhtmlならコンテンツはhtml変換後のデータが返却される', (t) => {
const diffStub = {
createPatch() {
return 'rawDiff';
},
};
const diff2htmlStub = {
Diff2Html: {
getPrettyHtml() {
return '<html>contents</html>';
},
},
};
const createDiffMock = proxyquire('../../App/Services/CalculateDiff', {
diff: diffStub,
diff2html: diff2htmlStub,
});
const expected = ['rawDiff', '<html>contents</html>'];
t.deepEqual(createDiffMock.createDiff('left', 'right', DIFF_FORMAT.html, 'line-by-line'), expected);
});
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dev": "concurrently --kill-others \"npm run hot-server\" \"npm run start-hot\"",
"hot-server": "cross-env NODE_ENV=development node -r babel-register server.js",
"start-hot": "cross-env HOT=1 NODE_ENV=development electron -r babel-register -r babel-polyfill ./main.development",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "ava",
"test:watch": "ava --watch"
},
"repository": {
"type": "git",
Expand All @@ -24,6 +25,7 @@
},
"homepage": "https://github.com/romiogaku/difftron#readme",
"devDependencies": {
"ava": "^0.17.0",
"babel-core": "^6.17.0",
"babel-eslint": "^7.0.0",
"babel-loader": "^6.2.5",
Expand Down Expand Up @@ -55,6 +57,7 @@
"eslint-plugin-react": "^6.4.1",
"express": "^4.14.0",
"extract-text-webpack-plugin": "^1.0.1",
"proxyquire": "^1.7.10",
"style-loader": "^0.13.1",
"webpack": "^1.13.2",
"webpack-dev-middleware": "^1.8.4",
Expand All @@ -77,5 +80,16 @@
"redux-saga": "^0.12.1",
"reduxsauce": "^0.2.0",
"seamless-immutable": "^6.3.0"
},
"ava": {
"files": [
"Tests/**/*.js",
"!Tests/Setup.js"
],
"require": [
"babel-register",
"babel-polyfill"
],
"babel": "inherit"
}
}

0 comments on commit 6e35133

Please sign in to comment.