Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reducer specs #150

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ build
# dependencies
node_modules

# coverage
coverage
.nyc_output

# logs
npm-debug.log

Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ before_script:
- sleep 3

script:
- npm test
- npm run lint
8 changes: 4 additions & 4 deletions app/lib/reducers/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import {
SESSION_SET_PROCESS_TITLE
} from '../constants/sessions';

const initialState = Immutable({
export const initialState = Immutable({
sessions: {},
write: null,
activeUid: null
});

function Session (obj) {
export function Session (obj) {
return Immutable({
uid: '',
title: '',
Expand All @@ -31,14 +31,14 @@ function Session (obj) {
}).merge(obj);
}

function Write (obj) {
export function Write (obj) {
return Immutable({
uid: '',
data: ''
}).merge(obj);
}

const reducer = (state = initialState, action) => {
export const reducer = (state = initialState, action) => {
switch (action.type) {
case SESSION_ADD:
return state.setIn(['sessions', action.uid], Session({
Expand Down
4 changes: 2 additions & 2 deletions app/lib/reducers/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { UPDATE_AVAILABLE } from '../constants/updater';

// TODO: populate `config-default.js` from this :)
const initial = Immutable({
export const initialState = Immutable({
cols: null,
rows: null,
activeUid: null,
Expand Down Expand Up @@ -57,7 +57,7 @@ const initial = Immutable({
updateNotes: null
});

const reducer = (state = initial, action) => {
export const reducer = (state = initialState, action) => {
let state_ = state;

switch (action.type) {
Expand Down
30 changes: 29 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@
"seamless-immutable": "6.1.1"
},
"devDependencies": {
"ava": "0.15.2",
"ava-spec": "1.0.1",
"babel-cli": "6.10.1",
"babel-core": "6.10.4",
"babel-eslint": "6.1.1",
"babel-loader": "6.2.4",
"babel-plugin-transform-es2015-modules-commonjs": "6.10.3",
"babel-preset-es2015-native-modules": "6.6.0",
"babel-preset-react": "6.11.1",
"eslint": "3.0.1",
"eslint-config-standard": "5.3.5",
"eslint-plugin-promise": "1.3.2",
"eslint-plugin-react": "5.2.2",
"eslint-plugin-standard": "1.3.2",
"mockery": "1.7.0",
"nyc": "7.0.0",
"webpack": "2.1.0-beta.15"
},
"eslintConfig": {
Expand Down Expand Up @@ -65,14 +70,37 @@
}
},
"babel": {
"env": {
"test": {
"plugins": [
"transform-es2015-modules-commonjs"
]
}
},
"presets": [
"es2015-native-modules",
"react"
]
},
"ava": {
"babel": "inherit",
"files": [
"./spec/**/*.spec.js"
],
"require": [
"babel-register",
"./spec/support/helper.js"
]
},
"nyc": {
"include": ["lib/**/*.js"],
"reporter": ["lcov", "text-summary"]
},
"scripts": {
"dev": "webpack --watch",
"lint": "eslint *.js",
"build": "NODE_ENV=production webpack"
"build": "NODE_ENV=production webpack",
"test": "NODE_ENV=test ava",
"coverage": "NODE_ENV=test nyc ava"
}
}
171 changes: 171 additions & 0 deletions app/spec/reducers/sessions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { describe } from 'ava-spec';
import {
reducer,
initialState,
Session,
Write
} from '../../lib/reducers/sessions';
import {
SESSION_ADD,
SESSION_PTY_EXIT,
SESSION_USER_EXIT,
SESSION_PTY_DATA,
SESSION_SET_ACTIVE,
SESSION_CLEAR_ACTIVE,
SESSION_URL_SET,
SESSION_URL_UNSET,
SESSION_SET_XTERM_TITLE,
SESSION_SET_PROCESS_TITLE
} from '../../lib/constants/sessions';

describe('Sessions reducer', () => {
describe('on @@redux/INIT', it => {
const state = undefined;
const action = { type: '@@redux/INIT' };

it('should return initial state', t => {
t.deepEqual(reducer(state, action), initialState);
});
});

describe('on SESSION_ADD', it => {
const state = initialState;
const action = { type: SESSION_ADD, uid: 'mysession', pid: 1, shell: '/my/shell' };

it('should add a session to the state based on its uid', t => {
const result = reducer(state, action);

t.deepEqual(result.sessions, {
mysession: {
uid: 'mysession', title: '', write: null, url: null, cleared: false, shell: 'shell', pid: 1
}
});
});
});

describe('on SESSION_URL_SET', it => {
const uid = 'mysession';
const url = '/my/url';
const state = initialState.setIn(['sessions', uid], Session({ uid }));
const action = { type: SESSION_URL_SET, uid, url };

it('should set the url on the action uid\'s session', t => {
const result = reducer(state, action);

t.is(result.sessions.mysession.url, url);
});
});

describe('on SESSION_URL_UNSET', it => {
const uid = 'mysession';
const url = '/my/url';
const state = initialState.setIn(['sessions', uid], Session({ uid, url }));
const action = { type: SESSION_URL_UNSET, uid };

it('should assign url as null on the action uid\'s session', t => {
const result = reducer(state, action);

t.is(result.sessions.mysession.url, null);
});
});

describe('on SESSION_SET_ACTIVE', it => {
const uid = 'mysession';
const state = initialState.setIn(['sessions', uid], Session({ uid }));
const action = { type: SESSION_SET_ACTIVE, uid };

it('should set activeUid on session based on action uid', t => {
const result = reducer(state, action);

t.is(result.activeUid, uid);
});
});

describe('on SESSION_CLEAR_ACTIVE', it => {
const uid = 'mysession';
const state = initialState
.setIn(['sessions', uid], Session({ uid }))
.setIn(['activeUid'], uid);
const action = { type: SESSION_CLEAR_ACTIVE };

it('should set "cleared: true" on state\'s active session', t => {
const result = reducer(state, action);

t.true(result.sessions.mysession.cleared);
});
});

describe('on SESSION_PTY_DATA', it => {
const uid = 'mysession';
const data = { a: 'b' };
const state = initialState.setIn(['sessions', uid], Session({ uid }));
const action = { type: SESSION_PTY_DATA, uid, data };

it('should set write on state based on action data', t => {
const result = reducer(state, action);

t.deepEqual(result.write, Write(action));
t.false(result.sessions.mysession.cleared);
});
});

describe('on SESSION_PTY_EXIT', () => {
describe('when session is on state', it => {
const uid = 'mysession';
const state = initialState.setIn(['sessions', uid], Session({ uid }));
const action = { type: SESSION_PTY_EXIT, uid };

it('should remove session from state', t => {
const result = reducer(state, action);

t.is(result.sessions.mysession, undefined);
});
});

describe('when session is not on state', it => {
const uid = 'mysession';
const state = initialState.setIn(['sessions', uid], Session({ uid }));
const action = { type: SESSION_PTY_EXIT, uid: 'anotherUid' };

it('should return current state', t => {
t.deepEqual(reducer(state, action), state);
});
});
});

describe('on SESSION_USER_EXIT', it => {
const uid = 'mysession';
const state = initialState.setIn(['sessions', uid], Session({ uid }));
const action = { type: SESSION_USER_EXIT, uid };

it('should remove session from state', t => {
const result = reducer(state, action);

t.is(result.sessions.mysession, undefined);
});
});

describe('on SESSION_SET_XTERM_TITLE', it => {
const uid = 'mysession';
const title = 'mytitle';
const state = initialState.setIn(['sessions', uid], Session({ uid }));
const action = { type: SESSION_SET_XTERM_TITLE, uid, title };

it('should set title on session based on action uid', t => {
const result = reducer(state, action);
t.is(result.sessions.mysession.title, title);
});
});

describe('on SESSION_SET_PROCESS_TITLE', it => {
const uid = 'mysession';
const title = 'mytitle';
const state = initialState.setIn(['sessions', uid], Session({ uid }));
const action = { type: SESSION_SET_PROCESS_TITLE, uid, title };

it('should set title on session based on action uid', t => {
const result = reducer(state, action);
t.is(result.sessions.mysession.title, title);
});
});
});
Loading