aboutsummaryrefslogtreecommitdiffstats
path: root/spec/javascript/time_table
diff options
context:
space:
mode:
authorLuc Donnet2017-10-16 23:52:06 +0200
committerLuc Donnet2017-10-16 23:52:06 +0200
commitf480ad0739e5c0ec2c0c8bb890344b9c4777ba35 (patch)
treea19bc6b43449b8b978a53c33476fb3eb571d4dda /spec/javascript/time_table
parentb611a84ed724036c4929bd4c3eaa7e23ea314f45 (diff)
parent51a1ea5b141032121913f807a162d305828bec54 (diff)
downloadchouette-core-f480ad0739e5c0ec2c0c8bb890344b9c4777ba35.tar.bz2
Merge branch 'master' into staging
Diffstat (limited to 'spec/javascript/time_table')
-rw-r--r--spec/javascript/time_table/actions_spec.js237
-rw-r--r--spec/javascript/time_table/reducers/metas_spec.js77
-rw-r--r--spec/javascript/time_table/reducers/modal_spec.js328
-rw-r--r--spec/javascript/time_table/reducers/pagination_spec.js139
-rw-r--r--spec/javascript/time_table/reducers/status_spec.js50
-rw-r--r--spec/javascript/time_table/reducers/timetable_spec.js338
6 files changed, 1169 insertions, 0 deletions
diff --git a/spec/javascript/time_table/actions_spec.js b/spec/javascript/time_table/actions_spec.js
new file mode 100644
index 000000000..003b7f6b5
--- /dev/null
+++ b/spec/javascript/time_table/actions_spec.js
@@ -0,0 +1,237 @@
+import actions from '../../../app/javascript/time_tables/actions'
+const dispatch = function(){}
+const dayTypes = [true, true, true, true, true, true, true]
+const day = {
+ date : "2017-05-01",
+ day : "lundi",
+ excluded_date : false,
+ in_periods : true,
+ include_date : false,
+ mday : 1,
+ wday : 1,
+ wnumber : "18"
+}
+describe('actions', () => {
+ it('should create an action to update dayTypes', () => {
+ let obj = {}
+ const expectedAction = {
+ type: 'UPDATE_DAY_TYPES',
+ dayTypes: obj
+ }
+ expect(actions.updateDayTypes(obj)).toEqual(expectedAction)
+ })
+
+ it('should create an action to update comment', () => {
+ const expectedAction = {
+ type: 'UPDATE_COMMENT',
+ comment: 'test'
+ }
+ expect(actions.updateComment('test')).toEqual(expectedAction)
+ })
+
+ it('should create an action to update color', () => {
+ const expectedAction = {
+ type: 'UPDATE_COLOR',
+ color: '#ffffff'
+ }
+ expect(actions.updateColor('#ffffff')).toEqual(expectedAction)
+ })
+
+ it('should create an action to update selected tags', () => {
+ let selectedItem = {
+ id: 1,
+ name: 'test'
+ }
+ const expectedAction = {
+ type: 'UPDATE_SELECT_TAG',
+ selectedItem: selectedItem
+ }
+ expect(actions.select2Tags(selectedItem)).toEqual(expectedAction)
+ })
+
+ it('should create an action to update unselected tags', () => {
+ let selectedItem = {
+ id: 1,
+ name: 'test'
+ }
+ const expectedAction = {
+ type: 'UPDATE_UNSELECT_TAG',
+ selectedItem: selectedItem
+ }
+ expect(actions.unselect2Tags(selectedItem)).toEqual(expectedAction)
+ })
+
+ it('should create an action to go to previous page', () => {
+ let pagination = {
+ currentPage: '2017-01-01',
+ periode_range: [],
+ stateChanged: false
+ }
+ const expectedAction = {
+ type: 'GO_TO_PREVIOUS_PAGE',
+ dispatch,
+ pagination,
+ nextPage: false
+ }
+ expect(actions.goToPreviousPage(dispatch, pagination)).toEqual(expectedAction)
+ })
+
+ it('should create an action to go to next page', () => {
+ let pagination = {
+ currentPage: '2017-01-01',
+ periode_range: [],
+ stateChanged: false
+ }
+ const expectedAction = {
+ type: 'GO_TO_NEXT_PAGE',
+ dispatch,
+ pagination,
+ nextPage: true
+ }
+ expect(actions.goToNextPage(dispatch, pagination)).toEqual(expectedAction)
+ })
+
+ it('should create an action to change page', () => {
+ let page = '2017-05-04'
+ const expectedAction = {
+ type: 'CHANGE_PAGE',
+ dispatch,
+ page: page
+ }
+ expect(actions.changePage(dispatch, page)).toEqual(expectedAction)
+ })
+
+ it('should create an action to delete period', () => {
+ let index = 1
+ const expectedAction = {
+ type: 'DELETE_PERIOD',
+ index,
+ dayTypes
+ }
+ expect(actions.deletePeriod(index, dayTypes)).toEqual(expectedAction)
+ })
+
+ it('should create an action to open add period form', () => {
+ const expectedAction = {
+ type: 'OPEN_ADD_PERIOD_FORM',
+ }
+ expect(actions.openAddPeriodForm()).toEqual(expectedAction)
+ })
+
+ it('should create an action to open edit period form', () => {
+ let period = {
+ id : 1,
+ period_end : "2017-03-05",
+ period_start : "2017-02-23"
+ }
+ let index = 1
+ const expectedAction = {
+ type: 'OPEN_EDIT_PERIOD_FORM',
+ period,
+ index
+ }
+ expect(actions.openEditPeriodForm(period, index)).toEqual(expectedAction)
+ })
+
+ it('should create an action to close period form', () => {
+ const expectedAction = {
+ type: 'CLOSE_PERIOD_FORM',
+ }
+ expect(actions.closePeriodForm()).toEqual(expectedAction)
+ })
+
+ it('should create an action to update period form', () => {
+ let val = "11"
+ let group = "start"
+ let selectType = "day"
+ const expectedAction = {
+ type: 'UPDATE_PERIOD_FORM',
+ val,
+ group,
+ selectType
+ }
+ expect(actions.updatePeriodForm(val, group, selectType)).toEqual(expectedAction)
+ })
+
+ it('should create an action to validate period form', () => {
+ let modalProps = {}
+ let timeTablePeriods = []
+ let metas = {}
+ let timetableInDates = []
+ let error = ''
+ const expectedAction = {
+ type: 'VALIDATE_PERIOD_FORM',
+ modalProps,
+ timeTablePeriods,
+ metas,
+ timetableInDates,
+ error
+ }
+ expect(actions.validatePeriodForm(modalProps, timeTablePeriods, metas, timetableInDates, error)).toEqual(expectedAction)
+ })
+
+ it('should create an action to add an included date', () => {
+ let index = 1
+ let date = actions.formatDate(new Date)
+ const expectedAction = {
+ type: 'ADD_INCLUDED_DATE',
+ index,
+ dayTypes,
+ date
+ }
+ expect(actions.addIncludedDate(index, dayTypes, date)).toEqual(expectedAction)
+ })
+
+ it('should create an action to remove an included dat', () => {
+ let index = 1
+ let date = actions.formatDate(new Date)
+ const expectedAction = {
+ type: 'REMOVE_INCLUDED_DATE',
+ index,
+ dayTypes,
+ date
+ }
+ expect(actions.removeIncludedDate(index, dayTypes, date)).toEqual(expectedAction)
+ })
+
+ it('should create an action to add an excluded date in period', () => {
+ let index = 1
+ let date = actions.formatDate(new Date)
+ const expectedAction = {
+ type: 'ADD_EXCLUDED_DATE',
+ index,
+ dayTypes,
+ date
+ }
+ expect(actions.addExcludedDate(index, dayTypes, date)).toEqual(expectedAction)
+ })
+
+ it('should create an action to remove an excluded date from period', () => {
+ let index = 1
+ let date = actions.formatDate(new Date)
+ const expectedAction = {
+ type: 'REMOVE_EXCLUDED_DATE',
+ index,
+ dayTypes,
+ date
+ }
+ expect(actions.removeExcludedDate(index, dayTypes, date)).toEqual(expectedAction)
+ })
+
+ it('should create an action to open confirm modal', () => {
+ let callback = function(){}
+ const expectedAction = {
+ type: 'OPEN_CONFIRM_MODAL',
+ callback
+ }
+ expect(actions.openConfirmModal(callback)).toEqual(expectedAction)
+ })
+
+ it('should create an action to close modal', () => {
+ const expectedAction = {
+ type: 'CLOSE_MODAL',
+ }
+ expect(actions.closeModal()).toEqual(expectedAction)
+ })
+
+})
diff --git a/spec/javascript/time_table/reducers/metas_spec.js b/spec/javascript/time_table/reducers/metas_spec.js
new file mode 100644
index 000000000..374ad1814
--- /dev/null
+++ b/spec/javascript/time_table/reducers/metas_spec.js
@@ -0,0 +1,77 @@
+import metasReducer from '../../../../app/javascript/time_tables/reducers/metas'
+
+let state = {}
+
+describe('metas reducer', () => {
+ beforeEach(() => {
+ let tag = {
+ id: 0,
+ name: 'test'
+ }
+ state = {
+ comment: 'test',
+ day_types: [true, true, true, true, true, true, true],
+ color: 'blue',
+ initial_tags: [tag],
+ tags: [tag]
+ }
+ })
+
+ it('should return the initial state', () => {
+ expect(
+ metasReducer(undefined, {})
+ ).toEqual({})
+ })
+
+ it('should handle UPDATE_DAY_TYPES', () => {
+ const arr = [false, false, true, true, true, true, true]
+ expect(
+ metasReducer(state, {
+ type: 'UPDATE_DAY_TYPES',
+ dayTypes: arr
+ })
+ ).toEqual(Object.assign({}, state, {day_types: arr, calendar: null}))
+ })
+
+ it('should handle UPDATE_COMMENT', () => {
+ expect(
+ metasReducer(state, {
+ type: 'UPDATE_COMMENT',
+ comment: 'title'
+ })
+ ).toEqual(Object.assign({}, state, {comment: 'title'}))
+ })
+
+ it('should handle UPDATE_COLOR', () => {
+ expect(
+ metasReducer(state, {
+ type: 'UPDATE_COLOR',
+ color: '#ffffff'
+ })
+ ).toEqual(Object.assign({}, state, {color: '#ffffff'}))
+ })
+
+ it('should handle UPDATE_SELECT_TAG', () => {
+ expect(
+ metasReducer(state, {
+ type: 'UPDATE_SELECT_TAG',
+ selectedItem:{
+ id: 1,
+ name: 'great'
+ }
+ })
+ ).toEqual(Object.assign({}, state, {tags: [...state.tags, {id: 1, name:'great'}]}))
+ })
+
+ it('should handle UPDATE_UNSELECT_TAG', () => {
+ expect(
+ metasReducer(state, {
+ type: 'UPDATE_UNSELECT_TAG',
+ selectedItem:{
+ id: 0,
+ name: 'test'
+ }
+ })
+ ).toEqual(Object.assign({}, state, {tags: []}))
+ })
+})
diff --git a/spec/javascript/time_table/reducers/modal_spec.js b/spec/javascript/time_table/reducers/modal_spec.js
new file mode 100644
index 000000000..1794d2acd
--- /dev/null
+++ b/spec/javascript/time_table/reducers/modal_spec.js
@@ -0,0 +1,328 @@
+import modalReducer from '../../../../app/javascript/time_tables/reducers/modal'
+
+let state = {}
+
+describe('modal reducer', () => {
+ beforeEach(() => {
+ state = {
+ confirmModal: {},
+ modalProps: {
+ active: false,
+ begin: {
+ day: '01',
+ month: '01',
+ year: String(new Date().getFullYear())
+ },
+ end: {
+ day: '01',
+ month: '01',
+ year: String(new Date().getFullYear())
+ },
+ index: false,
+ error: ''
+ },
+ type: ""
+ }
+ })
+
+ it('should return the initial state', () => {
+ expect(
+ modalReducer(undefined, {})
+ ).toEqual({})
+ })
+
+ it('should handle OPEN_CONFIRM_MODAL', () => {
+ let callback = function(){}
+ expect(
+ modalReducer(state, {
+ type: 'OPEN_CONFIRM_MODAL',
+ callback
+ })
+ ).toEqual(Object.assign({}, state, {type: "confirm", confirmModal: { callback: callback }}))
+ })
+
+ it('should handle CLOSE_PERIOD_FORM', () => {
+ let newModalProps = Object.assign({}, state.modalProps, {active: false})
+ expect(
+ modalReducer(state, {
+ type: 'CLOSE_PERIOD_FORM'
+ })
+ ).toEqual(Object.assign({}, state, {modalProps: newModalProps}))
+ })
+
+ it('should handle OPEN_EDIT_PERIOD_FORM', () => {
+ let period = {
+ id : 1,
+ period_end : "2017-03-05",
+ period_start : "2017-02-23"
+ }
+ let period_start = period.period_start.split('-')
+ let period_end = period.period_end.split('-')
+
+ let index = 1
+
+ let newModalProps = {
+ active: true,
+ begin: {
+ day: period_start[2],
+ month: period_start[1],
+ year: period_start[0]
+ },
+ end: {
+ day: period_end[2],
+ month: period_end[1],
+ year: period_end[0]
+ },
+ index: index,
+ error: ''
+ }
+ expect(
+ modalReducer(state, {
+ type: 'OPEN_EDIT_PERIOD_FORM',
+ period,
+ index
+ })
+ ).toEqual(Object.assign({}, state, {modalProps: newModalProps}))
+ })
+
+ it('should handle OPEN_ADD_PERIOD_FORM', () => {
+ let emptyDate = {
+ day: '01',
+ month: '01',
+ year: String(new Date().getFullYear())
+ }
+ let newModalProps = Object.assign({}, state.modalProps, {
+ active: true,
+ begin: emptyDate,
+ end: emptyDate,
+ index: false,
+ error: ""
+ })
+
+ expect(
+ modalReducer(state, {
+ type: 'OPEN_ADD_PERIOD_FORM'
+ })
+ ).toEqual(Object.assign({}, state, {modalProps: newModalProps}))
+ })
+
+ it('should handle UPDATE_PERIOD_FORM', () => {
+ let val = "11"
+ let group = "begin"
+ let selectType = "day"
+
+ let newModalProps = {
+ active: false,
+ begin: {
+ day: val,
+ month: '01',
+ year: String(new Date().getFullYear())
+ },
+ end: {
+ day: '01',
+ month: '01',
+ year: String(new Date().getFullYear())
+ },
+ index: false,
+ error: ''
+ }
+
+ expect(
+ modalReducer(state, {
+ type: 'UPDATE_PERIOD_FORM',
+ val,
+ group,
+ selectType
+ })
+ ).toEqual(Object.assign({}, state, {modalProps: newModalProps}))
+ })
+
+ it('should handle VALIDATE_PERIOD_FORM and throw error if period starts after the end', () => {
+ let modProps = {
+ active: false,
+ begin: {
+ day: '13',
+ month: '01',
+ year: String(new Date().getFullYear())
+ },
+ end: {
+ day: '01',
+ month: '01',
+ year: String(new Date().getFullYear())
+ },
+ index: false,
+ error: ''
+ }
+ let newModalProps = {
+ active: true,
+ begin: {
+ day: '01',
+ month: '01',
+ year: String(new Date().getFullYear())
+ },
+ end: {
+ day: '01',
+ month: '01',
+ year: String(new Date().getFullYear())
+ },
+ index: false,
+ error: 'La date de départ doit être antérieure à la date de fin'
+ }
+
+ let ttperiods = []
+ let ttdates = []
+ let metas = []
+
+ expect(
+ modalReducer(state, {
+ type: 'VALIDATE_PERIOD_FORM',
+ modalProps : modProps,
+ timeTablePeriods: ttperiods,
+ metas: metas,
+ timetableInDates: ttdates,
+ error: 'La date de départ doit être antérieure à la date de fin'
+ })
+ ).toEqual(Object.assign({}, state, {modalProps: newModalProps}))
+ })
+
+ it('should handle VALIDATE_PERIOD_FORM and throw error if periods overlap', () => {
+ let state2 = {
+ confirmModal: {},
+ modalProps: {
+ active: false,
+ begin: {
+ day: '03',
+ month: '05',
+ year: '2017'
+ },
+ end: {
+ day: '09',
+ month: '05',
+ year: '2017'
+ },
+ index: false,
+ error: ''
+ },
+ type: ''
+ }
+ let modProps2 = {
+ active: false,
+ begin: {
+ day: '03',
+ month: '05',
+ year: '2017'
+ },
+ end: {
+ day: '09',
+ month: '05',
+ year: '2017'
+ },
+ index: false,
+ error: ''
+ }
+ let ttperiods2 = [
+ {id: 261, period_start: '2017-02-23', period_end: '2017-03-05'},
+ {id: 262, period_start: '2017-03-15', period_end: '2017-03-25'},
+ {id: 264, period_start: '2017-04-24', period_end: '2017-05-04'},
+ {id: 265, period_start: '2017-05-14', period_end: '2017-05-24'}
+ ]
+
+ let ttdates2 = []
+
+ let newModalProps2 = {
+ active: true,
+ begin: {
+ day: '03',
+ month: '05',
+ year: '2017'
+ },
+ end: {
+ day: '09',
+ month: '05',
+ year: '2017'
+ },
+ index: false,
+ error: "Les périodes ne peuvent pas se chevaucher"
+ }
+
+ expect(
+ modalReducer(state2, {
+ type: 'VALIDATE_PERIOD_FORM',
+ modalProps : modProps2,
+ timeTablePeriods: ttperiods2,
+ timetableInDates: ttdates2,
+ error: "Les périodes ne peuvent pas se chevaucher"
+ })
+ ).toEqual(Object.assign({}, state2, {modalProps: newModalProps2}))
+ })
+
+ it('should handle VALIDATE_PERIOD_FORM and throw error if period overlaps date', () => {
+ let state3 = {
+ confirmModal: {},
+ modalProps: {
+ active: false,
+ begin: {
+ day: '01',
+ month: '08',
+ year: '2017'
+ },
+ end: {
+ day: '09',
+ month: '08',
+ year: '2017'
+ },
+ index: false,
+ error: ''
+ },
+ type: ''
+ }
+ let modProps3 = {
+ active: true,
+ begin: {
+ day: '01',
+ month: '08',
+ year: '2017'
+ },
+ end: {
+ day: '09',
+ month: '08',
+ year: '2017'
+ },
+ index: false,
+ error: ''
+ }
+
+ let ttperiods3 = []
+ let ttdates3 = [{date: "2017-08-04", include_date: true}]
+ let metas = {
+ day_types: [true,true,true,true,true,true,true]
+ }
+
+ let newModalProps3 = {
+ active: true,
+ begin: {
+ day: '01',
+ month: '08',
+ year: '2017'
+ },
+ end: {
+ day: '09',
+ month: '08',
+ year: '2017'
+ },
+ index: false,
+ error: "Une période ne peut chevaucher une date dans un calendrier"
+ }
+
+ expect(
+ modalReducer(state3, {
+ type: 'VALIDATE_PERIOD_FORM',
+ modalProps : modProps3,
+ timeTablePeriods: ttperiods3,
+ timetableInDates: ttdates3,
+ metas: metas,
+ error: "Une période ne peut chevaucher une date dans un calendrier"
+ })
+ ).toEqual(Object.assign({}, state3, {modalProps: newModalProps3}))
+ })
+})
diff --git a/spec/javascript/time_table/reducers/pagination_spec.js b/spec/javascript/time_table/reducers/pagination_spec.js
new file mode 100644
index 000000000..5aa8d27a2
--- /dev/null
+++ b/spec/javascript/time_table/reducers/pagination_spec.js
@@ -0,0 +1,139 @@
+import paginationReducer from '../../../../app/javascript/time_tables/reducers/pagination'
+
+const dispatch = function(){}
+
+let pagination = {
+ currentPage: "1982-02-15",
+ periode_range: ["1982-02-01", "1982-02-02", "1982-02-03"],
+ stateChanged: false
+}
+
+let state = {}
+
+describe('pagination reducer', () => {
+ beforeEach(() => {
+ state = {
+ currentPage: "",
+ periode_range: [],
+ stateChanged: false
+ }
+ })
+
+ it('should return the initial state', () => {
+ expect(
+ paginationReducer(undefined, {})
+ ).toEqual({})
+ })
+
+ it('should handle RECEIVE_TIME_TABLES', () => {
+ let json = [{
+ current_periode_range: "1982-02-15",
+ periode_range: ["1982-02-01", "1982-02-02", "1982-02-03"]
+ }]
+ expect(
+ paginationReducer(state, {
+ type: 'RECEIVE_TIME_TABLES',
+ json
+ })
+ ).toEqual(Object.assign({}, state, {currentPage: json.current_periode_range, periode_range: json.periode_range}))
+ })
+
+ it('should handle GO_TO_PREVIOUS_PAGE', () => {
+ let nextPage = nextPage ? 1 : -1
+ let newPage = pagination.periode_range[pagination.periode_range.indexOf(pagination.currentPage) + nextPage]
+
+ expect(
+ paginationReducer(state, {
+ type: 'GO_TO_PREVIOUS_PAGE',
+ dispatch,
+ pagination,
+ nextPage: false
+ })
+ ).toEqual(Object.assign({}, state, {currentPage : newPage, stateChanged: false}))
+ })
+ it('should handle GO_TO_NEXT_PAGE', () => {
+ let nextPage = nextPage ? 1 : -1
+ let newPage = pagination.periode_range[pagination.periode_range.indexOf(pagination.currentPage) + nextPage]
+
+ expect(
+ paginationReducer(state, {
+ type: 'GO_TO_NEXT_PAGE',
+ dispatch,
+ pagination,
+ nextPage: false
+ })
+ ).toEqual(Object.assign({}, state, {currentPage : newPage, stateChanged: false}))
+ })
+
+ it('should handle CHANGE_PAGE', () => {
+ let page = "1982-02-15"
+ expect(
+ paginationReducer(state, {
+ type: 'CHANGE_PAGE',
+ dispatch,
+ page
+ })
+ ).toEqual(Object.assign({}, state, {currentPage : page, stateChanged: false}))
+ })
+
+ it('should handle ADD_INCLUDED_DATE', () => {
+ expect(
+ paginationReducer(state, {
+ type: 'ADD_INCLUDED_DATE'
+ })
+ ).toEqual(Object.assign({}, state, {stateChanged: true}))
+ })
+
+ it('should handle REMOVE_INCLUDED_DATE', () => {
+ expect(
+ paginationReducer(state, {
+ type: 'REMOVE_INCLUDED_DATE'
+ })
+ ).toEqual(Object.assign({}, state, {stateChanged: true}))
+ })
+
+ it('should handle ADD_EXCLUDED_DATE', () => {
+ expect(
+ paginationReducer(state, {
+ type: 'ADD_EXCLUDED_DATE'
+ })
+ ).toEqual(Object.assign({}, state, {stateChanged: true}))
+ })
+
+ it('should handle REMOVE_EXCLUDED_DATE', () => {
+ expect(
+ paginationReducer(state, {
+ type: 'REMOVE_EXCLUDED_DATE'
+ })
+ ).toEqual(Object.assign({}, state, {stateChanged: true}))
+ })
+
+ it('should handle DELETE_PERIOD', () => {
+ expect(
+ paginationReducer(state, {
+ type: 'DELETE_PERIOD'
+ })
+ ).toEqual(Object.assign({}, state, {stateChanged: true}))
+ })
+ it('should handle VALIDATE_PERIOD_FORM', () => {
+ expect(
+ paginationReducer(state, {
+ type: 'VALIDATE_PERIOD_FORM'
+ })
+ ).toEqual(Object.assign({}, state, {stateChanged: true}))
+ })
+ it('should handle UPDATE_COMMENT', () => {
+ expect(
+ paginationReducer(state, {
+ type: 'UPDATE_COMMENT'
+ })
+ ).toEqual(Object.assign({}, state, {stateChanged: true}))
+ })
+ it('should handle UPDATE_COLOR', () => {
+ expect(
+ paginationReducer(state, {
+ type: 'UPDATE_COLOR'
+ })
+ ).toEqual(Object.assign({}, state, {stateChanged: true}))
+ })
+})
diff --git a/spec/javascript/time_table/reducers/status_spec.js b/spec/javascript/time_table/reducers/status_spec.js
new file mode 100644
index 000000000..63e4033f9
--- /dev/null
+++ b/spec/javascript/time_table/reducers/status_spec.js
@@ -0,0 +1,50 @@
+import statusReducer from '../../../../app/javascript/time_tables/reducers/status'
+
+let state = {}
+
+describe('status reducer', () => {
+ beforeEach(() => {
+ state = {
+ actionType: "edit",
+ fetchSuccess: true,
+ isFetching: false
+ }
+ })
+
+ it('should return the initial state', () => {
+ expect(
+ statusReducer(undefined, {})
+ ).toEqual({})
+ })
+
+ it('should handle UNAVAILABLE_SERVER', () => {
+ expect(
+ statusReducer(state, {
+ type: 'UNAVAILABLE_SERVER'
+ })
+ ).toEqual(Object.assign({}, state, {fetchSuccess: false}))
+ })
+
+ it('should handle FETCH_API', () => {
+ expect(
+ statusReducer(state, {
+ type: 'FETCH_API'
+ })
+ ).toEqual(Object.assign({}, state, {isFetching: true}))
+ })
+
+ it('should handle RECEIVE_TIME_TABLES', () => {
+ expect(
+ statusReducer(state, {
+ type: 'RECEIVE_TIME_TABLES'
+ })
+ ).toEqual(Object.assign({}, state, {fetchSuccess: true, isFetching: false}))
+ })
+ it('should handle RECEIVE_MONTH', () => {
+ expect(
+ statusReducer(state, {
+ type: 'RECEIVE_MONTH'
+ })
+ ).toEqual(Object.assign({}, state, {fetchSuccess: true, isFetching: false}))
+ })
+})
diff --git a/spec/javascript/time_table/reducers/timetable_spec.js b/spec/javascript/time_table/reducers/timetable_spec.js
new file mode 100644
index 000000000..f0f9eaa8c
--- /dev/null
+++ b/spec/javascript/time_table/reducers/timetable_spec.js
@@ -0,0 +1,338 @@
+require('whatwg-fetch')
+import timetableReducer from '../../../../app/javascript/time_tables/reducers/timetable'
+
+let state = {}
+const dispatch = function(){}
+let arrDayTypes = [true, true, true, true, true, true, true]
+let strDayTypes = 'LuMaMeJeVeSaDi'
+let time_table_periods = [{"id":261,"period_start":"2017-02-23","period_end":"2017-03-05"},{"id":262,"period_start":"2017-03-15","period_end":"2017-03-25"},{"id":263,"period_start":"2017-04-04","period_end":"2017-04-14"},{"id":264,"period_start":"2017-04-24","period_end":"2017-05-04"},{"id":265,"period_start":"2017-05-14","period_end":"2017-05-24"}]
+let time_table_dates = []
+let current_periode_range = "2017-05-01"
+let periode_range = ["2014-05-01","2014-06-01","2014-07-01","2014-08-01","2014-09-01","2014-10-01","2014-11-01","2014-12-01","2015-01-01","2015-02-01","2015-03-01","2015-04-01","2015-05-01","2015-06-01","2015-07-01","2015-08-01","2015-09-01","2015-10-01","2015-11-01","2015-12-01","2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01","2016-06-01","2016-07-01","2016-08-01","2016-09-01","2016-10-01","2016-11-01","2016-12-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01","2017-09-01","2017-10-01","2017-11-01","2017-12-01","2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01","2018-09-01","2018-10-01","2018-11-01","2018-12-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01","2019-05-01","2019-06-01","2019-07-01","2019-08-01","2019-09-01","2019-10-01","2019-11-01","2019-12-01","2020-01-01","2020-02-01","2020-03-01","2020-04-01","2020-05-01"]
+let current_month = [{"day":"lundi","date":"2017-05-01","wday":1,"wnumber":"18","mday":1,"include_date":false,"excluded_date":false},{"day":"mardi","date":"2017-05-02","wday":2,"wnumber":"18","mday":2,"include_date":false,"excluded_date":false},{"day":"mercredi","date":"2017-05-03","wday":3,"wnumber":"18","mday":3,"include_date":false,"excluded_date":false},{"day":"jeudi","date":"2017-05-04","wday":4,"wnumber":"18","mday":4,"include_date":false,"excluded_date":false},{"day":"vendredi","date":"2017-05-05","wday":5,"wnumber":"18","mday":5,"include_date":false,"excluded_date":false},{"day":"samedi","date":"2017-05-06","wday":6,"wnumber":"18","mday":6,"include_date":false,"excluded_date":false},{"day":"dimanche","date":"2017-05-07","wday":0,"wnumber":"18","mday":7,"include_date":false,"excluded_date":false},{"day":"lundi","date":"2017-05-08","wday":1,"wnumber":"19","mday":8,"include_date":false,"excluded_date":false},{"day":"mardi","date":"2017-05-09","wday":2,"wnumber":"19","mday":9,"include_date":false,"excluded_date":false},{"day":"mercredi","date":"2017-05-10","wday":3,"wnumber":"19","mday":10,"include_date":false,"excluded_date":false},{"day":"jeudi","date":"2017-05-11","wday":4,"wnumber":"19","mday":11,"include_date":false,"excluded_date":false},{"day":"vendredi","date":"2017-05-12","wday":5,"wnumber":"19","mday":12,"include_date":false,"excluded_date":false},{"day":"samedi","date":"2017-05-13","wday":6,"wnumber":"19","mday":13,"include_date":false,"excluded_date":false},{"day":"dimanche","date":"2017-05-14","wday":0,"wnumber":"19","mday":14,"include_date":false,"excluded_date":false},{"day":"lundi","date":"2017-05-15","wday":1,"wnumber":"20","mday":15,"include_date":false,"excluded_date":false},{"day":"mardi","date":"2017-05-16","wday":2,"wnumber":"20","mday":16,"include_date":false,"excluded_date":false},{"day":"mercredi","date":"2017-05-17","wday":3,"wnumber":"20","mday":17,"include_date":false,"excluded_date":false},{"day":"jeudi","date":"2017-05-18","wday":4,"wnumber":"20","mday":18,"include_date":false,"excluded_date":false},{"day":"vendredi","date":"2017-05-19","wday":5,"wnumber":"20","mday":19,"include_date":false,"excluded_date":false},{"day":"samedi","date":"2017-05-20","wday":6,"wnumber":"20","mday":20,"include_date":false,"excluded_date":false},{"day":"dimanche","date":"2017-05-21","wday":0,"wnumber":"20","mday":21,"include_date":false,"excluded_date":false},{"day":"lundi","date":"2017-05-22","wday":1,"wnumber":"21","mday":22,"include_date":false,"excluded_date":false},{"day":"mardi","date":"2017-05-23","wday":2,"wnumber":"21","mday":23,"include_date":false,"excluded_date":false},{"day":"mercredi","date":"2017-05-24","wday":3,"wnumber":"21","mday":24,"include_date":false,"excluded_date":false},{"day":"jeudi","date":"2017-05-25","wday":4,"wnumber":"21","mday":25,"include_date":false,"excluded_date":false},{"day":"vendredi","date":"2017-05-26","wday":5,"wnumber":"21","mday":26,"include_date":false,"excluded_date":false},{"day":"samedi","date":"2017-05-27","wday":6,"wnumber":"21","mday":27,"include_date":false,"excluded_date":false},{"day":"dimanche","date":"2017-05-28","wday":0,"wnumber":"21","mday":28,"include_date":false,"excluded_date":false},{"day":"lundi","date":"2017-05-29","wday":1,"wnumber":"22","mday":29,"include_date":false,"excluded_date":false},{"day":"mardi","date":"2017-05-30","wday":2,"wnumber":"22","mday":30,"include_date":false,"excluded_date":false},{"day":"mercredi","date":"2017-05-31","wday":3,"wnumber":"22","mday":31,"include_date":false,"excluded_date":false}]
+
+let newCurrentMonth = [{"day":"lundi","date":"2017-05-01","wday":1,"wnumber":"18","mday":1,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"mardi","date":"2017-05-02","wday":2,"wnumber":"18","mday":2,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"mercredi","date":"2017-05-03","wday":3,"wnumber":"18","mday":3,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"jeudi","date":"2017-05-04","wday":4,"wnumber":"18","mday":4,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"vendredi","date":"2017-05-05","wday":5,"wnumber":"18","mday":5,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"samedi","date":"2017-05-06","wday":6,"wnumber":"18","mday":6,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"dimanche","date":"2017-05-07","wday":0,"wnumber":"18","mday":7,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"lundi","date":"2017-05-08","wday":1,"wnumber":"19","mday":8,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"mardi","date":"2017-05-09","wday":2,"wnumber":"19","mday":9,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"mercredi","date":"2017-05-10","wday":3,"wnumber":"19","mday":10,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"jeudi","date":"2017-05-11","wday":4,"wnumber":"19","mday":11,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"vendredi","date":"2017-05-12","wday":5,"wnumber":"19","mday":12,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"samedi","date":"2017-05-13","wday":6,"wnumber":"19","mday":13,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"dimanche","date":"2017-05-14","wday":0,"wnumber":"19","mday":14,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"lundi","date":"2017-05-15","wday":1,"wnumber":"20","mday":15,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"mardi","date":"2017-05-16","wday":2,"wnumber":"20","mday":16,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"mercredi","date":"2017-05-17","wday":3,"wnumber":"20","mday":17,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"jeudi","date":"2017-05-18","wday":4,"wnumber":"20","mday":18,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"vendredi","date":"2017-05-19","wday":5,"wnumber":"20","mday":19,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"samedi","date":"2017-05-20","wday":6,"wnumber":"20","mday":20,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"dimanche","date":"2017-05-21","wday":0,"wnumber":"20","mday":21,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"lundi","date":"2017-05-22","wday":1,"wnumber":"21","mday":22,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"mardi","date":"2017-05-23","wday":2,"wnumber":"21","mday":23,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"mercredi","date":"2017-05-24","wday":3,"wnumber":"21","mday":24,"include_date":false,"excluded_date":false,"in_periods":true},{"day":"jeudi","date":"2017-05-25","wday":4,"wnumber":"21","mday":25,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"vendredi","date":"2017-05-26","wday":5,"wnumber":"21","mday":26,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"samedi","date":"2017-05-27","wday":6,"wnumber":"21","mday":27,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"dimanche","date":"2017-05-28","wday":0,"wnumber":"21","mday":28,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"lundi","date":"2017-05-29","wday":1,"wnumber":"22","mday":29,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"mardi","date":"2017-05-30","wday":2,"wnumber":"22","mday":30,"include_date":false,"excluded_date":false,"in_periods":false},{"day":"mercredi","date":"2017-05-31","wday":3,"wnumber":"22","mday":31,"include_date":false,"excluded_date":false,"in_periods":false}]
+
+let json = {
+ current_month: current_month,
+ current_periode_range: current_periode_range,
+ periode_range: periode_range,
+ time_table_periods: time_table_periods,
+ day_types: strDayTypes,
+ time_table_dates: time_table_dates
+}
+
+
+
+describe('timetable reducer with empty state', () => {
+ beforeEach(() => {
+ state = {
+ current_month: [],
+ current_periode_range: "",
+ periode_range: [],
+ time_table_periods: [],
+ time_table_dates: []
+ }
+ })
+
+ it('should return the initial state', () => {
+ expect(
+ timetableReducer(undefined, {})
+ ).toEqual({})
+ })
+
+ it('should handle RECEIVE_TIME_TABLES', () => {
+ let newState = {
+ current_month: newCurrentMonth,
+ current_periode_range: current_periode_range,
+ periode_range: periode_range,
+ time_table_periods: time_table_periods,
+ time_table_dates: []
+ }
+ expect(
+ timetableReducer(state, {
+ type: 'RECEIVE_TIME_TABLES',
+ json
+ })
+ ).toEqual(newState)
+ })
+})
+
+describe('timetable reducer with filled state', () => {
+ beforeEach(() => {
+ state = {
+ current_month: newCurrentMonth,
+ current_periode_range: current_periode_range,
+ periode_range: periode_range,
+ time_table_periods: time_table_periods,
+ time_table_dates: time_table_dates
+ }
+ })
+
+ it('should handle RECEIVE_MONTH', () => {
+ expect(
+ timetableReducer(state, {
+ type: 'RECEIVE_MONTH',
+ json: {
+ days: current_month,
+ day_types: strDayTypes
+ }
+ })
+ ).toEqual(state)
+ })
+
+
+ it('should handle GO_TO_PREVIOUS_PAGE', () => {
+ let pagination = {
+ periode_range: periode_range,
+ currentPage: current_periode_range
+ }
+ expect(
+ timetableReducer(state, {
+ type: 'GO_TO_PREVIOUS_PAGE',
+ dispatch,
+ pagination,
+ nextPage: false
+ })
+ ).toEqual(Object.assign({}, state, {current_periode_range: '2017-04-01'}))
+ })
+
+ it('should handle GO_TO_NEXT_PAGE', () => {
+ let pagination = {
+ periode_range: periode_range,
+ currentPage: current_periode_range
+ }
+ expect(
+ timetableReducer(state, {
+ type: 'GO_TO_NEXT_PAGE',
+ dispatch,
+ pagination,
+ nextPage: true
+ })
+ ).toEqual(Object.assign({}, state, {current_periode_range: '2017-06-01'}))
+ })
+
+ it('should handle CHANGE_PAGE', () => {
+ const actions = {
+ fetchTimeTables: function(){}
+ }
+ let newPage = '2017-05-01'
+ expect(
+ timetableReducer(state, {
+ type: 'CHANGE_PAGE',
+ dispatch,
+ page: newPage
+ })
+ ).toEqual(Object.assign({}, state, {current_periode_range: newPage}))
+ })
+
+ it('should handle DELETE_PERIOD and remove excluded days that were in period', () => {
+ state.time_table_dates.push({date: "2017-05-01", in_out: false})
+ state.current_month[0].excluded_date = true
+ state.time_table_periods[3].deleted = true
+
+ let begin = new Date(state.time_table_periods[3].period_start)
+ let end = new Date(state.time_table_periods[3].period_end)
+
+ let newState = Object.assign({}, state, {
+ time_table_dates: [],
+ current_month: state.current_month.map((d, i) => {
+ if (new Date(d.date) >= begin && new Date(d.date) <= end) {
+ d.excluded_date = false
+ d.in_periods = false
+ }
+ return d
+ })
+ })
+ expect(
+ timetableReducer(state, {
+ type: 'DELETE_PERIOD',
+ index: 3,
+ dayTypes: arrDayTypes
+ })
+ ).toEqual(newState)
+ })
+
+ it('should handle ADD_INCLUDED_DATE', () => {
+ let newDates = state.time_table_dates.concat({date: "2017-05-05", in_out: true})
+
+ let newCM = newCurrentMonth.map((d,i) => {
+ if (i == 4) d.include_date = true
+ return d
+ })
+
+ let newState = Object.assign({}, state, {time_table_dates: newDates, current_month: newCM})
+
+ expect(
+ timetableReducer(state, {
+ type: 'ADD_INCLUDED_DATE',
+ index: 4,
+ dayTypes: arrDayTypes,
+ date: "2017-05-05"
+ })
+ ).toEqual(newState)
+ })
+
+ it('should handle REMOVE_INCLUDED_DATE', () => {
+ state.current_month[4].include_date = true
+ state.time_table_dates.push({date: "2017-05-05", in_out: true})
+
+ let newCM = newCurrentMonth.map((d,i) => {
+ if (i == 4) d.include_date = false
+ return d
+ })
+
+ let newDates = state.time_table_dates.filter(d => d.date != "2017-05-05" && d.in_out != true )
+ let newState = Object.assign({}, state, {time_table_dates: newDates, current_month: newCM})
+ expect(
+ timetableReducer(state, {
+ type: 'REMOVE_INCLUDED_DATE',
+ index: 4,
+ dayTypes: arrDayTypes,
+ date: "2017-05-05"
+ })
+ ).toEqual(newState)
+ })
+
+ it('should handle ADD_EXCLUDED_DATE', () => {
+ let newDates = state.time_table_dates.concat({date: "2017-05-01", in_out: false})
+
+ let newCM = newCurrentMonth.map((d,i) => {
+ if (i == 0){
+ d.include_date = false
+ d.excluded_date = true
+ }
+ return d
+ })
+
+ let newState = Object.assign({}, state, {time_table_dates: newDates, current_month: newCM})
+
+ expect(
+ timetableReducer(state, {
+ type: 'ADD_EXCLUDED_DATE',
+ index: 0,
+ dayTypes: arrDayTypes,
+ date: "2017-05-01"
+ })
+ ).toEqual(newState)
+ })
+
+ it('should handle REMOVE_EXCLUDED_DATE', () => {
+ state.time_table_dates = [{date: "2017-05-01", in_out: false}]
+ state.current_month[0].excluded_date = false
+ let newState = Object.assign({}, state, {time_table_dates: []})
+ expect(
+ timetableReducer(state, {
+ type: 'REMOVE_EXCLUDED_DATE',
+ index: 0,
+ dayTypes: arrDayTypes,
+ date: "2017-05-01"
+ })
+ ).toEqual(newState)
+ })
+
+ it('should handle UPDATE_DAY_TYPES and remove out_day that are out of day types', () => {
+ state.time_table_dates = [{date: "2017-05-01", in_out: false}]
+ let newArrDayTypes = Array.from(arrDayTypes, (dt, i) => {
+ if (i == 1) dt = false
+ return dt
+ })
+ expect(
+ timetableReducer(state, {
+ type: 'UPDATE_DAY_TYPES',
+ dayTypes: newArrDayTypes
+ }).time_table_dates
+ ).toEqual([])
+ })
+
+ it('should handle UPDATE_DAY_TYPES and remove in_day that are in day types and in period', () => {
+ state.time_table_dates = [{ date: "2017-05-16", in_out: true }]
+ expect(
+ timetableReducer(state, {
+ type: 'UPDATE_DAY_TYPES',
+ dayTypes: arrDayTypes
+ }).time_table_dates
+ ).toEqual([])
+ })
+
+ it('should handle VALIDATE_PERIOD_FORM and add period if modalProps index = false', () => {
+ let newPeriods = state.time_table_periods.concat({"period_start": "2018-05-15", "period_end": "2018-05-24"})
+ let newState = Object.assign({}, state, {time_table_periods: newPeriods, time_table_dates: []})
+ let modalProps = {
+ active: false,
+ begin: {
+ day: '15',
+ month: '05',
+ year: '2018'
+ },
+ end: {
+ day: '24',
+ month: '05',
+ year: '2018'
+ },
+ error: '',
+ index: false
+ }
+ expect(
+ timetableReducer(state, {
+ type: 'VALIDATE_PERIOD_FORM',
+ modalProps: modalProps,
+ timeTablePeriods: state.time_table_periods,
+ metas: {
+ day_types: arrDayTypes
+ },
+ timetableInDates: state.time_table_dates.filter(d => d.in_out == true),
+ error: modalProps.error
+ })
+ ).toEqual(newState)
+ })
+
+ it('should handle VALIDATE_PERIOD_FORM and update period if modalProps index != false', () => {
+
+ let begin = new Date(state.time_table_periods[0].period_start)
+ let end = new Date(state.time_table_periods[0].period_end)
+ let newCM = newCurrentMonth.map((d) => {
+ if (new Date (d.date) >= begin && new Date(d.date) <= end) {
+ d.in_periods = false
+ d.excluded_date = false
+ }
+ return d
+ })
+
+ let newPeriods = state.time_table_periods.map( (p,i) => {
+ if (i == 0) {
+ p.period_start = "2018-05-15"
+ p.period_end = "2018-05-24"
+ }
+ return p
+ })
+ let newState = Object.assign({}, state, {time_table_periods: newPeriods})
+
+ let modalProps = {
+ active: false,
+ begin: {
+ day: '15',
+ month: '05',
+ year: '2018'
+ },
+ end: {
+ day: '24',
+ month: '05',
+ year: '2018'
+ },
+ error: '',
+ index: 0
+ }
+ expect(
+ timetableReducer(state, {
+ type: 'VALIDATE_PERIOD_FORM',
+ modalProps: modalProps,
+ timeTablePeriods: state.time_table_periods,
+ metas: {
+ day_types: arrDayTypes
+ },
+ timetableInDates: state.time_table_dates.filter(d => d.in_out == true)
+ })
+ ).toEqual(newState)
+ })
+})