Skip to content

Commit

Permalink
Merge pull request #41 from mkozhukharenko/feature/tests
Browse files Browse the repository at this point in the history
refactor dictionary helpers + tests + table dimensions generator
  • Loading branch information
ormus2002 committed Apr 27, 2016
2 parents 1f3c9d0 + 82d6e89 commit 44ac46b
Show file tree
Hide file tree
Showing 18 changed files with 486 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import './styles/spinner.styl'
import classNames from 'classNames'
import classNames from 'classnames'

export default ({className}) =>
<div className={classNames('sk-fading-circle', className)}>
Expand Down
13 changes: 13 additions & 0 deletions src/modules/commons/tableHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

// FIXME: need to find out a better way to calculate a table dimensions.
// maybe we can set a different column width depending how wide a user screen is
export function setTableDimensions ({width, widthGutter, minHeight = 200, heightGutter}) {
widthGutter = widthGutter || 20;
heightGutter = heightGutter || 80;
width = width || window.innerWidth - widthGutter;
let height = window.innerHeight - heightGutter;
return {
width: width,
height: height > minHeight ? height : minHeight
}
}
34 changes: 25 additions & 9 deletions src/modules/dictionaries/helpers.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
import forEach from 'lodash/forEach';
import isObject from 'lodash/isObject';
import isString from 'lodash/isString';

/**
* check whether a specific distionaries is loaded
* @param listOfDict {Array} - array of dict
* @param listOfDict {Array} || {String} - array of dict
* @param dictState {Object} - state
* @returns {boolean}
*/
export function isDictLoaded(listOfDict, dictState) {
if (!listOfDict || !Array.isArray(listOfDict) && !isString(listOfDict)) {
throw new Error('list of dic {array of strings} || {sting} must be passed!');
}

if (!dictState || !isObject(dictState)) {
throw new Error('dic state {object} is needed by isDictLoaded fn as a second parameter');
}

if (isString(listOfDict)) {
listOfDict = [listOfDict]
}

if (!Object.keys(dictState).length) return false;
let commonResult = true;

for (let i = 0; i < listOfDict.length; i++) {
let dicData = dictState[listOfDict[i]];

if (!dicData) throw ReferenceError(`Dic "${dicData}" doesn't exist at all!
Please, check if it's name is correct or add a it to dict store default state!
(for this check a "disctionariesDefaultState" variable in dictionaries/reducer.js)`);

console.log('dictState', dictState);
listOfDict.forEach((dicName) => {
let dicData = dictState[dicName];
// if (dicData && dicData.resources && (dicData.isLoading || !dicData.loaded || !dicData.resources.length)) {
if (!dicData || dicData.isLoading || !dicData.resources || !dicData.resources.length) {
commonResult = false;
return false;
}
});
}

return commonResult;
return true;
}

/**
Expand Down
136 changes: 136 additions & 0 deletions src/modules/dictionaries/helpres.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Created by nikolaykozhukharenko on 4/24/16.
*/

import chai, {expect} from 'chai';
chai.should();
import {isDictLoaded, createDataMap} from './helpers'

describe('dictionary helper', () => {

let listOfDict = ['dic_01', 'dic_02'];
let dictState = {
'dic_01': {
isLoading: false,
resources: [1,3,4]
},
'dic_02': {
isLoading: false,
resources: [5,7,9]
}
};

describe('isDictLoaded fn', () => {

it('should throw an error if wrong parameters were passed', () => {
expect(isDictLoaded.bind({}, false, 1)).to.throw(Error)
});

it('should return "false" if dictionaries state it empty', () => {
isDictLoaded([], {}).should.equal(false)
});

it('should throw an error if dic does not exists', () => {
expect(isDictLoaded.bind({}, ['DontExistDictName'], dictState))
.to.throw(ReferenceError)
});

it('should not throw an error if listOfDict eq to string', () => {
expect(isDictLoaded.bind({}, 'dic_01', dictState))
.to.not.throw(Error)
});

it('should return "true" if data for dictionaries exists', () => {
isDictLoaded(listOfDict, dictState).should.equal(true)
});

it('should return "false" if one of dict "isLoading" right now', () => {
isDictLoaded(listOfDict, {
'dic_01': {
isLoading: true,
resources: [1,3,5]
},
'dic_02': {
isLoading: false,
resources: [4,6,7]
}
}).should.equal(false)
});

it('should return "false" if one of dict do not have "resources" field ', () => {
isDictLoaded(listOfDict, {
'dic_01': {
isLoading: false
},
'dic_02': {
isLoading: false,
resources: [4,6,7]
}
}).should.equal(false)
});

it('should return "false" if one of dict has an empty "resources"', () => {
isDictLoaded(listOfDict, {
'dic_01': {
isLoading: false,
resources: [1,3,5]
},
'dic_02': {
isLoading: false,
resources: []
}
}).should.equal(false)
});

it('should return "true" when one dict (as a string) was passed ', () => {
isDictLoaded('dic_011', {
'dic_011': {
isLoading: false,
resources: [1,3,5]
},
'dic_02': {
isLoading: false,
resources: []
}
}).should.equal(true)
});
});


describe('isDictLoaded fn', () => {
it('should return an array', () => {
expect(createDataMap([
{
id: 0,
name: 'name_01'
}
])).to.be.instanceof(Array);
});

it('should return a map', () => {
expect(createDataMap([
{
id: 0,
name: 'name_01'
},
{
id: 1,
name: 'name_02'
}
])).to.eql(['name_01','name_02'])
});

it('should return a map when id order is different', () => {
expect(createDataMap([
{
id: 1,
name: 'name_01'
},
{
id: 3,
name: 'name_02'
}
])).to.eql([,'name_01',,'name_02'])
});
})
});
3 changes: 1 addition & 2 deletions src/modules/dictionaries/reducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import helpers, {createDataMap} from './helpers';
import {createDataMap} from './helpers';
import * as types from './constants';

export const dictInitialState = {
Expand Down Expand Up @@ -35,7 +35,6 @@ export default function dictionaries(state = disctionariesDefaultState, action =
});

case types.DICTIONARY_LOAD_FAIL:
console.log('action', action);
return Object.assign({}, state, {
[action.payload.collectionName]: {
isLoading: false,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/dictionaries/reducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import sinonChai from 'sinon-chai';
chai.use(sinonChai);
chai.should();

describe('ratingList reducer', () => {
describe('dictionary reducer', () => {

let helpersMock;
let reducer;
Expand Down
65 changes: 33 additions & 32 deletions src/modules/enrolments/list/EnrolmentsListPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,27 @@ import * as dictConst from '../../dictionaries/constants';
import {loadEnrolments, setFieldWidthEnrolments} from './../actions';
import loadDictionaries from '../../dictionaries/actions';
import {isDataForEnrolmentLoaded, decodeEnrolments, getEnrolmentIdByIndex} from '../helpers';
import {setTableDimensions} from '../../commons/tableHelpers';

import Loader from 'loader'

let buildCells = (decodedEnrolments, enrolmentsFieldNames) => {
return Object.keys(enrolmentsFieldNames).map((field) => {
return <Column
columnKey={field}
header={<Cell>{enrolmentsFieldNames[field].name}</Cell>}
cell={props => (
<Cell {...props}>
{decodedEnrolments[props.rowIndex][field]}
</Cell>
)
}
isResizable
width={enrolmentsFieldNames[field].width}
/>
});
};

class EnrolmentsListPage extends Component {
constructor(props) {
super(props);
Expand All @@ -31,40 +50,22 @@ class EnrolmentsListPage extends Component {
}

render() {
if (!isDataForEnrolmentLoaded()) {
return <Loader isLoading isPageLoader/>;
}

let {decodedEnrolments, enrolmentsFieldNames} = this.props;

let cells = Object.keys(enrolmentsFieldNames).map((field) => {
return <Column
columnKey={field}
header={<Cell>{enrolmentsFieldNames[field].name}</Cell>}
cell={props => (
<Cell {...props}>
{decodedEnrolments[props.rowIndex][field]}
</Cell>
)
}
isResizable
width={enrolmentsFieldNames[field].width}
/>
});

return (
<Table
rowsCount={decodedEnrolments.length}
rowHeight={50}
headerHeight={70}
onColumnResizeEndCallback={this._onColumnResizeEndCallback}
isColumnResizing={false}
onRowClick={this._onClickRow}
width={window.innerWidth-20}
height={window.innerHeight-80}
>
{cells}
</Table>
<Loader isLoading={!isDataForEnrolmentLoaded()} isPageLoader>
<Table
rowsCount={decodedEnrolments.length}
rowHeight={50}
headerHeight={70}
onColumnResizeEndCallback={this._onColumnResizeEndCallback}
isColumnResizing={false}
onRowClick={this._onClickRow}
{...setTableDimensions({width: 1050})}
>
{buildCells(decodedEnrolments, enrolmentsFieldNames)}
</Table>
</Loader>
);
}
}
Expand All @@ -78,7 +79,7 @@ export const getDecodedEnrolments = createSelector(
enrolmentList: enrolmentList,
enrolmentsFieldNames: enrolmentsFieldNames
})
)
);

const mapDispatchToEnrolments = (dispatch) => (
{ loadEnrolments: (params) => dispatch(loadEnrolments(params)),
Expand Down
2 changes: 1 addition & 1 deletion src/modules/rating/reducers/ratingList.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { expect } from 'chai';
import * as types from './../constants';
import reducer from './ratingList';
import { TIMEPERIODID_CHANGED, changeTimePeriodId } from './../../settings/duck';
import { changeTimePeriodId } from './../../settings/duck';
import { highlighEnrolment } from './../actions'

describe('ratingList reducer', () => {
Expand Down
1 change: 0 additions & 1 deletion src/modules/rating/reducers/specofferChooser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default function specofferChooser(state = specofferChooserInitialState, a
);

case LOCATION_CHANGE: // listen to query parameters changes
//if (action.payload.pathname !== '/rating') return state;
let {
departmentId = state.departmentId,
specofferId = state.specofferId } = action.payload.query;
Expand Down
Loading

0 comments on commit 44ac46b

Please sign in to comment.