Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

add jest unit tests for date actions and reducers

Edward Plato490ff5c16481
diff --git a/web/js/containers/timeline/timeline.js b/web/js/containers/timeline/timeline.js
index 4f042fb3c..2a407ebf0 100644
--- a/web/js/containers/timeline/timeline.js
+++ b/web/js/containers/timeline/timeline.js
@@ -35,7 +35,6 @@ import {
lastDate as layersLastDateTime
} from '../../modules/layers/selectors';
import {
- getPosition,
selectDate,
changeTimeScale,
selectInterval,
@@ -987,9 +986,6 @@ function mapStateToProps(state) {
}
const mapDispatchToProps = dispatch => ({
- getPosition: val => {
- dispatch(getPosition(val));
- },
// updates the relative application now to allow up to date coverage
updateAppNow: date => {
dispatch(updateAppNow(date));
diff --git a/web/js/modules/date/actions.js b/web/js/modules/date/actions.js
index c31a7518a..b797d6578 100644
--- a/web/js/modules/date/actions.js
+++ b/web/js/modules/date/actions.js
@@ -1,5 +1,4 @@
import {
- GET_TIMELINE_POSITION,
CHANGE_TIME_SCALE,
CHANGE_CUSTOM_INTERVAL,
CHANGE_INTERVAL,
@@ -19,12 +18,6 @@ export function updateAppNow(date) {
value: date
};
}
-export function getPosition(value) {
- return {
- type: GET_TIMELINE_POSITION,
- value
- };
-}
export function selectDate(value) {
return (dispatch, getState) => {
const compareState = getState().compare;
diff --git a/web/js/modules/date/actions.test.js b/web/js/modules/date/actions.test.js
new file mode 100644
index 000000000..31b4532f9
--- /dev/null
+++ b/web/js/modules/date/actions.test.js
@@ -0,0 +1,95 @@
+import configureMockStore from 'redux-mock-store';
+import thunk from 'redux-thunk';
+import {
+ changeTimeScale,
+ updateAppNow,
+ selectDate,
+ changeCustomInterval,
+ selectInterval
+} from './actions';
+import {
+ CHANGE_TIME_SCALE,
+ CHANGE_CUSTOM_INTERVAL,
+ CHANGE_INTERVAL,
+ SELECT_DATE,
+ UPDATE_APP_NOW
+} from './constants';
+
+const middlewares = [thunk];
+const mockStore = configureMockStore(middlewares);
+
+// test variables
+const mockDate = new Date();
+const customInterval = 3;
+const delta = 5;
+const interval = 3;
+
+describe('Date timescale changes', () => {
+ test('changeTimeScale action returns ' + CHANGE_TIME_SCALE + 'as type and ' +
+ interval + ' as value', () => {
+ const expectedAction = {
+ type: CHANGE_TIME_SCALE,
+ value: interval
+ };
+ expect(changeTimeScale(interval)).toEqual(expectedAction);
+ });
+ test('updateAppNow action returns ' + UPDATE_APP_NOW + 'as type and ' +
+ mockDate + ' as value', () => {
+ const expectedAction = {
+ type: UPDATE_APP_NOW,
+ value: mockDate
+ };
+ expect(updateAppNow(mockDate)).toEqual(expectedAction);
+ });
+ test('selectDate action returns ' + SELECT_DATE + 'as type and selected ' +
+ 'as activeString and ' + mockDate + ' as value', () => {
+ const expectedAction = {
+ type: SELECT_DATE,
+ activeString: 'selected',
+ value: mockDate
+ };
+ const store = mockStore({
+ date: {},
+ compare: {
+ isCompareA: true
+ }
+ });
+ store.dispatch(selectDate(mockDate));
+ expect(store.getActions()[0]).toEqual(expectedAction);
+ });
+ test('selectDate action returns ' + SELECT_DATE + 'as type and selectedB ' +
+ 'as activeString and ' + mockDate + ' as value', () => {
+ const expectedAction = {
+ type: SELECT_DATE,
+ activeString: 'selectedB',
+ value: mockDate
+ };
+ const store = mockStore({
+ date: {},
+ compare: {
+ isCompareA: false
+ }
+ });
+ store.dispatch(selectDate(mockDate));
+ expect(store.getActions()[0]).toEqual(expectedAction);
+ });
+ test('changeCustomInterval action returns ' + CHANGE_CUSTOM_INTERVAL + ' as type and ' +
+ customInterval + ' as value and ' + delta + ' as delta', () => {
+ const expectedAction = {
+ type: CHANGE_CUSTOM_INTERVAL,
+ value: customInterval,
+ delta: delta
+ };
+ expect(changeCustomInterval(delta, customInterval)).toEqual(expectedAction);
+ });
+ test('selectInterval action returns ' + CHANGE_INTERVAL + ' as type and ' +
+ interval + ' as value and ' + delta + ' as delta and true as customSelected', () => {
+ const expectedAction = {
+ type: CHANGE_INTERVAL,
+ value: interval,
+ delta: delta,
+ customSelected: true
+ };
+ expect(selectInterval(delta, interval, true)).toEqual(expectedAction);
+ });
+});
diff --git a/web/js/modules/date/constants.js b/web/js/modules/date/constants.js
index 04e7f0ccb..3dac7bc73 100644
--- a/web/js/modules/date/constants.js
+++ b/web/js/modules/date/constants.js
@@ -2,7 +2,6 @@ export const CHANGE_CUSTOM_INTERVAL = 'DATE/CHANGE_CUSTOM_INTERVAL';
export const CHANGE_INTERVAL = 'DATE/CHANGE_INTERVAL';
export const CHANGE_TIME_SCALE = 'DATE/CHANGE_TIME_SCALE';
export const SELECT_DATE = 'DATE/SELECT_DATE';
-export const GET_TIMELINE_POSITION = 'DATE/GET_TIMELINE_POSITION';
export const UPDATE_APP_NOW = 'DATE/UPDATE_APP_NOW';
export const timeScaleFromNumberKey = {
diff --git a/web/js/modules/date/reducers.js b/web/js/modules/date/reducers.js
index 38f5865fc..2f8b880eb 100644
--- a/web/js/modules/date/reducers.js
+++ b/web/js/modules/date/reducers.js
@@ -1,5 +1,4 @@
import {
- GET_TIMELINE_POSITION,
CHANGE_TIME_SCALE,
CHANGE_CUSTOM_INTERVAL,
CHANGE_INTERVAL,
@@ -9,25 +8,24 @@ import {
import util from '../../util/util';
import { assign as lodashAssign } from 'lodash';
-const defaultState = {
+export const dateReducerState = {
selectedZoom: 3,
interval: 3,
delta: 1,
customSelected: false,
customDelta: undefined,
- customInterval: undefined,
- position: 0
+ customInterval: undefined
};
export function getInitialState(config) {
- return lodashAssign({}, defaultState, {
+ return lodashAssign({}, dateReducerState, {
selected: config.pageLoadTime,
selectedB: util.dateAdd(config.pageLoadTime, 'day', -7),
appNow: config.pageLoadTime
});
}
-export function dateReducer(state = defaultState, action) {
+export function dateReducer(state = dateReducerState, action) {
switch (action.type) {
case CHANGE_TIME_SCALE:
return lodashAssign({}, state, {
@@ -49,10 +47,6 @@ export function dateReducer(state = defaultState, action) {
return lodashAssign({}, state, {
[action.activeString]: action.value
});
- case GET_TIMELINE_POSITION:
- return lodashAssign({}, state, {
- position: action.value
- });
case UPDATE_APP_NOW:
return lodashAssign({}, state, {
appNow: action.value
diff --git a/web/js/modules/date/reducers.test.js b/web/js/modules/date/reducers.test.js
new file mode 100644
index 000000000..8982cda87
--- /dev/null
+++ b/web/js/modules/date/reducers.test.js
@@ -0,0 +1,127 @@
+import {
+ dateReducer,
+ dateReducerState
+} from './reducers';
+import {
+ CHANGE_TIME_SCALE,
+ CHANGE_CUSTOM_INTERVAL,
+ CHANGE_INTERVAL,
+ SELECT_DATE,
+ UPDATE_APP_NOW
+} from './constants';
+
+// test variables
+const mockDate = new Date();
+const selectedZoom = 2;
+
+describe('dateReducer', () => {
+ test('should return the initial state', () => {
+ expect(dateReducer(undefined, {})).toEqual(
+ dateReducerState
+ );
+ });
+ test(
+ CHANGE_TIME_SCALE +
+ 'action type and ' + selectedZoom + ' as selectedZoom ' +
+ 'should return new state',
+ () => {
+ expect(
+ dateReducer(dateReducerState, {
+ type: CHANGE_TIME_SCALE,
+ value: selectedZoom
+ })
+ ).toEqual({
+ ...dateReducerState,
+ selectedZoom: selectedZoom
+ });
+ }
+ );
+ test(
+ CHANGE_CUSTOM_INTERVAL +
+ 'action type and 4 as customInterval and 10 as customDelta ' +
+ 'should return new state',
+ () => {
+ expect(
+ dateReducer(dateReducerState, {
+ type: CHANGE_CUSTOM_INTERVAL,
+ value: 4,
+ delta: 10
+ })
+ ).toEqual({
+ ...dateReducerState,
+ customInterval: 4,
+ customDelta: 10,
+ customSelected: true
+ });
+ }
+ );
+ test(
+ CHANGE_INTERVAL +
+ 'action type and 1 as delta and 2 as interval ' +
+ 'should return new state',
+ () => {
+ expect(
+ dateReducer(dateReducerState, {
+ type: CHANGE_INTERVAL,
+ value: 2,
+ delta: 1,
+ customSelected: false
+ })
+ ).toEqual({
+ ...dateReducerState,
+ interval: 2,
+ delta: 1,
+ customSelected: false
+ });
+ }
+ );
+ test(
+ SELECT_DATE +
+ 'action type and ' + mockDate + ' as value and selected ' +
+ 'as activeString should return new state',
+ () => {
+ expect(
+ dateReducer(dateReducerState, {
+ type: SELECT_DATE,
+ value: mockDate,
+ activeString: 'selected'
+ })
+ ).toEqual({
+ ...dateReducerState,
+ selected: mockDate
+ });
+ }
+ );
+ test(
+ SELECT_DATE +
+ 'action type and ' + mockDate + ' as value and selectedB ' +
+ 'as activeString should return new state',
+ () => {
+ expect(
+ dateReducer(dateReducerState, {
+ type: SELECT_DATE,
+ value: mockDate,
+ activeString: 'selectedB'
+ })
+ ).toEqual({
+ ...dateReducerState,
+ selectedB: mockDate
+ });
+ }
+ );
+ test(
+ UPDATE_APP_NOW +
+ 'action type and ' + mockDate + ' as value should return new state',
+ () => {
+ expect(
+ dateReducer(dateReducerState, {
+ type: UPDATE_APP_NOW,
+ value: mockDate
+ })
+ ).toEqual({
+ ...dateReducerState,
+ appNow: mockDate
+ });
+ }
+ );
+});