Skip to content

Commit

Permalink
refactor tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
julesterrien committed May 5, 2017
1 parent 902dd7a commit 459d93f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 100 deletions.
1 change: 0 additions & 1 deletion .mocharc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
--require babel-register
--reporter dot
--colors
10 changes: 5 additions & 5 deletions build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ var _dotty2 = _interopRequireDefault(_dotty);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/*
/**
* resetOrOmit
* initialState: object with the reducer's initial state
* state: object with the reducer's initial state
* keys: array of strings representing keys or paths to keys to reset
* returns an object with the completely or partially resetted state
* @param {Object} initialState: object with the reducer's initial state
* @param {Object} state: object with the reducer's initial state
* @param {Array} keys: array of strings representing keys or paths to keys to reset
* @return {Object} an object with the completely or partially resetted state
*/
var resetOrOmit = function resetOrOmit(initialState, state, keys) {
var resetState = state;
Expand Down
10 changes: 5 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import dotty from 'dotty';

/*
/**
* resetOrOmit
* initialState: object with the reducer's initial state
* state: object with the reducer's initial state
* keys: array of strings representing keys or paths to keys to reset
* returns an object with the completely or partially resetted state
* @param {Object} initialState: object with the reducer's initial state
* @param {Object} state: object with the reducer's initial state
* @param {Array} keys: array of strings representing keys or paths to keys to reset
* @return {Object} an object with the completely or partially resetted state
*/
const resetOrOmit = (initialState, state, keys) => {
const resetState = state;
Expand Down
114 changes: 25 additions & 89 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,146 +7,82 @@ const { assert } = chai;
const { update, reset, createReducer } = novaRedux;

const test = describe('Nova-Redux:', () => {
it('should update its state', (done) => {
const initialState = { status: { isFetching: false } };
const api = createReducer('api', initialState);
const initialState = { status: { isFetching: false } };
let nextState;
let api;
beforeEach(() => {
api = createReducer('api', initialState);
const action = update('api', 'test', {
test: true,
});
nextState = api(initialState, action);
})

it('can update its state', (done) => {
const expected = {
status: { isFetching: false }, // initial state
test: true, // new state
_lastAction: 'test', // metaData returned by creators
_lastAction: 'test', // log
};
const lastState = initialState;
const actual = api(lastState, action);
assert.deepEqual(actual, expected, 'the reducer updates');
assert.deepEqual(nextState, expected, 'the reducer can update its state');
done();
});

it('should reset the whole state to initial state', (done) => {
const initialState = { status: { isFetching: false } };
const api = createReducer('api', initialState);
const updateAction = update('api', 'test', {
test: true,
});
let nextState = api(initialState, updateAction);
const updateExpected = {
status: { isFetching: false },
test: true,
_lastAction: 'test',
};
assert.deepEqual(nextState, updateExpected, 'the reducer updates');

it('can reset the whole state to initial state', (done) => {
const resetAction = reset('api', 'Reset the whole state', {
reset: [],
});
nextState = api(nextState, resetAction);
assert.deepEqual(nextState, initialState, 'the reducer can reset itself to the initial state');
const state = api(nextState, resetAction);
assert.deepEqual(state, initialState, 'the reducer can reset itself to the initial state');
done();
});

it('should reset given keys', (done) => {
const initialState = { status: { isFetching: false } };
const api = createReducer('api', initialState);
const updateAction = update('api', 'test', {
test: true,
});
const updateExpected = {
status: { isFetching: false },
test: true,
_lastAction: 'test',
};
let nextState = api(initialState, updateAction);
assert.deepEqual(nextState, updateExpected, 'the reducer updates');

it('can reset specific keys', (done) => {
const resetAction = reset('api', 'resetTest', {
reset: ['test'],
});
nextState = api(nextState, resetAction);
const resetExpected = {
const expected = {
status: { isFetching: false },
_lastAction: 'resetTest',
};
assert.deepEqual(nextState, resetExpected, 'the reducer can reset selected keys');
const state = api(nextState, resetAction);
assert.deepEqual(state, expected, 'the reducer can reset specific keys');
done();
});

it('should omit given keys that do not exist in initial state', (done) => {
const initialState = { status: { isFetching: false } };
const api = createReducer('api', initialState);
const updateAction = update('api', 'test', {
test: true,
});
const updateExpected = {
status: { isFetching: false },
test: true,
_lastAction: 'test',
};
let nextState = api(initialState, updateAction);
assert.deepEqual(nextState, updateExpected, 'the reducer updates');

it('on reset, it will remove specific keys from the state if they do not exist in the initial state', (done) => {
const resetAction = reset('api', 'resetTest', {
reset: ['test'],
});
nextState = api(nextState, resetAction);
const resetExpected = {
const expected = {
status: { isFetching: false },
_lastAction: 'resetTest',
};
assert.deepEqual(nextState, resetExpected, 'the reducer can reset selected keys');
const state = api(nextState, resetAction);
assert.deepEqual(state, expected, 'the reducer can reset selected keys');
done();
});

it('should handle resetting the last key/val if given a path', (done) => {
const initialState = { status: { isFetching: false } };
const api = createReducer('api', initialState);
const updateAction = update('api', 'test', {
status: {
isFetching: true,
}
});
const updateExpected = {
status: { isFetching: true },
_lastAction: 'test',
};
let nextState = api(initialState, updateAction);
assert.deepEqual(nextState, updateExpected, 'the reducer updates');

const resetAction = reset('api', 'reset path', {
reset: ['status.isFetching'],
});
nextState = api(nextState, resetAction);
const resetExpected = {
const expected = {
status: { isFetching: false },
_lastAction: 'reset path',
};
assert.deepEqual(nextState, resetExpected, 'the reducer can handle paths');
const state = api(nextState, resetAction);
assert.deepEqual(state, expected, 'the reducer can handle paths');
done();
});

it('should return an error if the path is undefined', (done) => {
const initialState = { status: { isFetching: false } };
const api = createReducer('api', initialState);
const updateAction = update('api', 'test', {
status: {
isFetching: true,
}
});
const updateExpected = {
status: { isFetching: true },
_lastAction: 'test',
};
let nextState = api(initialState, updateAction);
assert.deepEqual(nextState, updateExpected, 'the reducer updates');

const resetAction = reset('api', 'reset wrong path', {
reset: ['wrong.path'],
});
const wrapper = () => {
api(nextState, resetAction);
}

assert.throws(wrapper, 'Provided path does not exist');
done();
});
Expand Down

0 comments on commit 459d93f

Please sign in to comment.