diff options
| author | Thomas Haddad | 2017-01-11 15:28:08 +0100 |
|---|---|---|
| committer | Thomas Haddad | 2017-01-11 15:30:21 +0100 |
| commit | e19f37f690dd932f8f45b0c847e9fa5af197b7bd (patch) | |
| tree | f4d700ee63212b2d4af5b28fb8453dc37fa62a55 | |
| parent | 19a6aedb8dfdb37b30c320fc17719f6e2ce89876 (diff) | |
| download | chouette-core-e19f37f690dd932f8f45b0c847e9fa5af197b7bd.tar.bz2 | |
Refactor Navigate container/component/specs
Signed-off-by: Thomas Shawarma Haddad <thomas.haddad@af83.com>
7 files changed, 57 insertions, 50 deletions
diff --git a/app/assets/javascripts/es6_browserified/journey_patterns/actions/index.js b/app/assets/javascripts/es6_browserified/journey_patterns/actions/index.js index 24fd37a34..4031f9bf1 100644 --- a/app/assets/javascripts/es6_browserified/journey_patterns/actions/index.js +++ b/app/assets/javascripts/es6_browserified/journey_patterns/actions/index.js @@ -11,18 +11,16 @@ const actions = { type: 'LOAD_FIRST_PAGE', dispatch }), - goToPreviousPage : (dispatch, currentPage) => ({ + goToPreviousPage : (dispatch, pagination) => ({ type: 'GO_TO_PREVIOUS_PAGE', dispatch, - currentPage, + pagination, nextPage : false }), - goToNextPage : (dispatch, currentPage, totalCount, perPage) => ({ + goToNextPage : (dispatch, pagination) => ({ type: 'GO_TO_NEXT_PAGE', dispatch, - currentPage, - totalCount, - perPage, + pagination, nextPage : true }), updateCheckboxValue : (e, index) => ({ diff --git a/app/assets/javascripts/es6_browserified/journey_patterns/components/Navigate.js b/app/assets/javascripts/es6_browserified/journey_patterns/components/Navigate.js index 826781890..077e8ab0d 100644 --- a/app/assets/javascripts/es6_browserified/journey_patterns/components/Navigate.js +++ b/app/assets/javascripts/es6_browserified/journey_patterns/components/Navigate.js @@ -3,9 +3,9 @@ var Component = require('react').Component var PropTypes = require('react').PropTypes var actions = require('../actions') -let Navigate = ({ dispatch, journeyPatterns, page, stateChanged, totalCount, perPage }) => { +let Navigate = ({ dispatch, journeyPatterns, pagination }) => { let firstPage = 1 - let lastPage = Math.ceil(totalCount / window.journeyPatternsPerPage) + let lastPage = Math.ceil(pagination.totalCount / window.journeyPatternsPerPage) return ( <form className='btn-group btn-group-sm' onSubmit={e => { @@ -14,23 +14,23 @@ let Navigate = ({ dispatch, journeyPatterns, page, stateChanged, totalCount, per <button onClick={e => { e.preventDefault() - dispatch(actions.checkConfirmModal(e, actions.goToPreviousPage(dispatch, page), stateChanged)) + dispatch(actions.checkConfirmModal(e, actions.goToPreviousPage(dispatch, pagination), pagination.stateChanged)) }} type="submit" data-toggle='' data-target='#ConfirmModal' - className={ (page == firstPage ? "hidden" : "") + " btn btn-default" }> + className={ (pagination.page == firstPage ? "hidden" : "") + " btn btn-default" }> <span className="fa fa-chevron-left"></span> </button> <button onClick={e => { e.preventDefault() - dispatch(actions.checkConfirmModal(e, actions.goToNextPage(dispatch, page, totalCount, perPage), stateChanged)) + dispatch(actions.checkConfirmModal(e, actions.goToNextPage(dispatch, pagination), pagination.stateChanged)) }} type="submit" data-toggle='' data-target='#ConfirmModal' - className={ (page == lastPage ? "hidden" : "") + " btn btn-default" }> + className={ (pagination.page == lastPage ? "hidden" : "") + " btn btn-default" }> <span className="fa fa-chevron-right"></span> </button> </form> @@ -38,11 +38,8 @@ let Navigate = ({ dispatch, journeyPatterns, page, stateChanged, totalCount, per } Navigate.propTypes = { - page: PropTypes.number.isRequired, - totalCount: PropTypes.number.isRequired, - stateChanged: PropTypes.bool.isRequired, journeyPatterns: PropTypes.array.isRequired, - perPage: PropTypes.number.isRequired, + pagination: PropTypes.object.isRequired, dispatch: PropTypes.func.isRequired } diff --git a/app/assets/javascripts/es6_browserified/journey_patterns/containers/Navigate.js b/app/assets/javascripts/es6_browserified/journey_patterns/containers/Navigate.js index a5e29ccc3..42bba6d2e 100644 --- a/app/assets/javascripts/es6_browserified/journey_patterns/containers/Navigate.js +++ b/app/assets/javascripts/es6_browserified/journey_patterns/containers/Navigate.js @@ -6,10 +6,7 @@ var NavigateComponent = require('../components/Navigate') const mapStateToProps = (state) => { return { journeyPatterns: state.journeyPatterns, - page: state.pagination.page, - totalCount: state.pagination.totalCount, - perPage: state.pagination.perPage, - stateChanged: state.pagination.stateChanged + pagination: state.pagination } } diff --git a/app/assets/javascripts/es6_browserified/journey_patterns/reducers/journeyPatterns.js b/app/assets/javascripts/es6_browserified/journey_patterns/reducers/journeyPatterns.js index 985725ade..ea2b1381e 100644 --- a/app/assets/javascripts/es6_browserified/journey_patterns/reducers/journeyPatterns.js +++ b/app/assets/javascripts/es6_browserified/journey_patterns/reducers/journeyPatterns.js @@ -37,16 +37,17 @@ const journeyPatterns = (state = [], action) => { return [...action.json] case 'LOAD_FIRST_PAGE': actions.fetchJourneyPatterns(action.dispatch) + return state case 'GO_TO_PREVIOUS_PAGE': $('#ConfirmModal').modal('hide') - if(action.currentPage > 1){ - actions.fetchJourneyPatterns(action.dispatch, action.currentPage, action.nextPage) + if(action.pagination.page > 1){ + actions.fetchJourneyPatterns(action.dispatch, action.pagination.page, action.nextPage) } return state case 'GO_TO_NEXT_PAGE': $('#ConfirmModal').modal('hide') - if (action.totalCount - (action.currentPage * window.journeyPatternsPerPage) > 0){ - actions.fetchJourneyPatterns(action.dispatch, action.currentPage, action.nextPage) + if (action.pagination.totalCount - (action.pagination.page * action.pagination.perPage) > 0){ + actions.fetchJourneyPatterns(action.dispatch, action.pagination.page, action.nextPage) } return state case 'UPDATE_CHECKBOX_VALUE': diff --git a/app/assets/javascripts/es6_browserified/journey_patterns/reducers/pagination.js b/app/assets/javascripts/es6_browserified/journey_patterns/reducers/pagination.js index 02ccdea1f..48d95fdea 100644 --- a/app/assets/javascripts/es6_browserified/journey_patterns/reducers/pagination.js +++ b/app/assets/javascripts/es6_browserified/journey_patterns/reducers/pagination.js @@ -3,15 +3,15 @@ const pagination = (state = {}, action) => { case 'RECEIVE_JOURNEY_PATTERNS': return Object.assign({}, state, {stateChanged: false}) case 'GO_TO_PREVIOUS_PAGE': - if (action.currentPage > 1){ + if (action.pagination.page > 1){ toggleOnConfirmModal() - return Object.assign({}, state, {page : action.currentPage - 1, stateChanged: false}) + return Object.assign({}, state, {page : action.pagination.page - 1, stateChanged: false}) } return state case 'GO_TO_NEXT_PAGE': - if (state.totalCount - (action.currentPage * action.perPage) > 0){ + if (state.totalCount - (action.pagination.page * action.pagination.perPage) > 0){ toggleOnConfirmModal() - return Object.assign({}, state, {page : action.currentPage + 1, stateChanged: false}) + return Object.assign({}, state, {page : action.pagination.page + 1, stateChanged: false}) } return state case 'UPDATE_CHECKBOX_VALUE': diff --git a/spec/javascripts/journey_patterns/actions_spec.js b/spec/javascripts/journey_patterns/actions_spec.js index 4b7cb1f79..b4e1a0845 100644 --- a/spec/javascripts/journey_patterns/actions_spec.js +++ b/spec/javascripts/journey_patterns/actions_spec.js @@ -23,20 +23,38 @@ describe('when landing on page', () => { expect(actions.loadFirstPage(dispatch)).toEqual(expectedAction) }) }) +describe('when previous navigation button is clicked', () => { + it('should create an action to go to previous page', () => { + const nextPage = false + const pagination = { + totalCount: 25, + perPage: 12, + page:1 + } + const expectedAction = { + type: 'GO_TO_PREVIOUS_PAGE', + dispatch, + pagination, + nextPage + } + expect(actions.goToPreviousPage(dispatch, pagination)).toEqual(expectedAction) + }) +}) describe('when next navigation button is clicked', () => { it('should create an action to go to next page', () => { const nextPage = true - const totalCount = 25 - const perPage = 12 + const pagination = { + totalCount: 25, + perPage: 12, + page:1 + } const expectedAction = { type: 'GO_TO_NEXT_PAGE', dispatch, - currentPage, - totalCount, - perPage, + pagination, nextPage } - expect(actions.goToNextPage(dispatch, currentPage, totalCount, perPage)).toEqual(expectedAction) + expect(actions.goToNextPage(dispatch, pagination)).toEqual(expectedAction) }) }) describe('when clicking on a journey pattern checkbox', () => { diff --git a/spec/javascripts/journey_patterns/reducers/pagination_spec.js b/spec/javascripts/journey_patterns/reducers/pagination_spec.js index 8107794f6..4800451e9 100644 --- a/spec/javascripts/journey_patterns/reducers/pagination_spec.js +++ b/spec/javascripts/journey_patterns/reducers/pagination_spec.js @@ -1,14 +1,13 @@ var reducer = require('es6_browserified/journey_patterns/reducers/pagination') -const totalCount = 25 const diff = 1 -const perPage = 12 let state = { page : 2, - totalCount : totalCount, - stateChanged: false + totalCount : 25, + stateChanged: false, + perPage: 12 } -let currentPage = 2 +let pagination = Object.assign({}, state) const dispatch = function(){} describe('pagination reducer, given parameters allowing page change', () => { @@ -24,9 +23,7 @@ describe('pagination reducer, given parameters allowing page change', () => { reducer(state, { type: 'GO_TO_NEXT_PAGE', dispatch, - currentPage, - totalCount, - perPage, + pagination, nextPage : true }) ).toEqual(Object.assign({}, state, {page : state.page + 1, stateChanged: false})) @@ -37,7 +34,7 @@ describe('pagination reducer, given parameters allowing page change', () => { reducer(state, { type: 'GO_TO_PREVIOUS_PAGE', dispatch, - currentPage, + pagination, nextPage : false }) ).toEqual(Object.assign({}, state, {page : state.page - 1, stateChanged: false})) @@ -49,7 +46,7 @@ describe('pagination reducer, given parameters not allowing to go to previous pa beforeEach(()=>{ state.page = 1 - currentPage = 1 + pagination.page = 1 }) it('should return GO_TO_PREVIOUS_PAGE and not change state', () => { @@ -57,7 +54,7 @@ describe('pagination reducer, given parameters not allowing to go to previous pa reducer(state, { type: 'GO_TO_PREVIOUS_PAGE', dispatch, - currentPage, + pagination, nextPage : false }) ).toEqual(state) @@ -68,7 +65,7 @@ describe('pagination reducer, given parameters not allowing to go to next page', beforeEach(()=>{ state.page = 3 - currentPage = 3 + pagination.page = 3 }) it('should return GO_TO_NEXT_PAGE and not change state', () => { @@ -76,9 +73,8 @@ describe('pagination reducer, given parameters not allowing to go to next page', reducer(state, { type: 'GO_TO_NEXT_PAGE', dispatch, - currentPage, - totalCount, - nextPage : false + pagination, + nextPage : true }) ).toEqual(state) }) @@ -92,6 +88,6 @@ describe('pagination reducer, given parameters changing totalCount', () => { type: 'UPDATE_TOTAL_COUNT', diff }) - ).toEqual(Object.assign({}, state, {totalCount: totalCount - diff})) + ).toEqual(Object.assign({}, state, {totalCount: state.totalCount - diff})) }) }) |
