Interactive interface for browsing global, full-resolution satellite imagery
Add layer reducer unit tests
Benjamin Kinga776744087e9
diff --git a/web/js/fixtures.js b/web/js/fixtures.jsindex 93398049b..99bdaab25 100644--- a/web/js/fixtures.js+++ b/web/js/fixtures.js@@ -1,13 +1,6 @@import proj4 from 'proj4';import { register } from 'ol/proj/proj4';-// import { dateModel } from './date/model';-// import { layersModel } from './layers/model';-// import { mapModel } from './map/model';-// import { palettesModel } from './palettes/model';-// import { projectionModel } from './projection/model';-// import { compareModel } from './compare/model';-var fixtures = {red: 'ff0000ff',light_red: 'fff0f0ff',@@ -22,7 +15,8 @@ var fixtures = {fixtures.config = function() {return {defaults: {- projection: 'geographic'+ projection: 'geographic',+ startingLayers: [{ id: 'terra-cr' }, { id: 'aqua-cr', hidden: 'true' }]},projections: {geographic: {@@ -173,18 +167,6 @@ fixtures.config = function() {};};-// fixtures.models = function(config) {-// var models = {};--// models.proj = projectionModel(config);-// models.layers = layersModel(models, config);-// models.palettes = palettesModel(models, config);-// models.map = mapModel(models, config);-// models.compare = compareModel(models, config);-// models.date = dateModel(models, config);-// return models;-// };-export function registerProjections() {proj4.defs('EPSG:3413',diff --git a/web/js/modules/layers/actions.js b/web/js/modules/layers/actions.jsindex a9c3ae8ef..c815d825b 100644--- a/web/js/modules/layers/actions.js+++ b/web/js/modules/layers/actions.js@@ -1,4 +1,4 @@-import { findIndex as lodashFindIndex, each as lodashEach } from 'lodash';+import { findIndex as lodashFindIndex } from 'lodash';import {addLayer as addLayerSelector,resetLayers as resetLayersSelector,@@ -84,7 +84,7 @@ export function initSecondLayerGroup() {export function reorderLayers(layerArray) {return {type: REORDER_LAYER_GROUP,- layerArray: layerArray+ layers: layerArray};}diff --git a/web/js/modules/layers/reducers.js b/web/js/modules/layers/reducers.jsindex d2e96e68c..be1376bc7 100644--- a/web/js/modules/layers/reducers.js+++ b/web/js/modules/layers/reducers.js@@ -44,6 +44,7 @@ export function layerReducer(state = initialState, action) {switch (action.type) {case RESET_LAYERS:case ADD_LAYER:+ case REORDER_LAYER_GROUP:case ADD_LAYERS_FOR_EVENT:return lodashAssign({}, state, {[layerGroupStr]: action.layers@@ -53,10 +54,6 @@ export function layerReducer(state = initialState, action) {return lodashAssign({}, state, {activeB: lodashCloneDeep(state.active)});- case REORDER_LAYER_GROUP:- return lodashAssign({}, state, {- [layerGroupStr]: action.layerArray- });case ON_LAYER_HOVER:return lodashAssign({}, state, {hoveredLayer: action.active ? action.id : ''diff --git a/web/js/modules/layers/reducers.test.js b/web/js/modules/layers/reducers.test.jsnew file mode 100644index 000000000..4a8bca6cc--- /dev/null+++ b/web/js/modules/layers/reducers.test.js@@ -0,0 +1,128 @@+import { layerReducer, getInitialState } from './reducers';+import fixtures from '../../fixtures';+import * as CONSTANTS from './constants';+import {+ SET_CUSTOM as SET_CUSTOM_PALETTE,+ CLEAR_CUSTOM as CLEAR_CUSTOM_PALETTE,+ SET_RANGE_AND_SQUASH+} from '../palettes/constants';+import update from 'immutability-helper';++const config = fixtures.config();+const initalState = getInitialState(config);++describe('Initial layer state test', () => {+ test('initial state loads aqua-cr and terra-cr layers', () => {+ expect(initalState.active.length).toEqual(2);+ expect(initalState.active[0].id).toEqual('aqua-cr');+ expect(initalState.active[1].id).toEqual('terra-cr');+ });+});++describe('layer Reducer tests', () => {+ test('ADD_LAYER action updates active layer array with new layer array', () => {+ const newState = update(initalState, {+ active: { $set: ['updated-layer-array'] }+ });+ expect(+ layerReducer(initalState, {+ type: CONSTANTS.ADD_LAYER,+ activeString: 'active',+ layers: ['updated-layer-array']+ })+ ).toEqual(newState);+ });+ test('RESET_LAYERS action updates active layer array with new layer array', () => {+ const newState = update(initalState, {+ active: { $set: ['updated-layer-array'] }+ });+ expect(+ layerReducer(initalState, {+ type: CONSTANTS.RESET_LAYERS,+ activeString: 'active',+ layers: ['updated-layer-array']+ })+ ).toEqual(newState);+ });+ test('REORDER_LAYER_GROUP action updates active layer array with new layer array', () => {+ const newState = update(initalState, {+ active: { $set: ['updated-layer-array'] }+ });+ expect(+ layerReducer(initalState, {+ type: CONSTANTS.REORDER_LAYER_GROUP,+ activeString: 'active',+ layers: ['updated-layer-array']+ })+ ).toEqual(newState);+ });+ test('REMOVE_LAYER action removes terra-cr from layer array', () => {+ const response = layerReducer(initalState, {+ type: CONSTANTS.REMOVE_LAYER,+ activeString: 'active',+ index: 1+ });+ expect(response.active.length).toEqual(1);+ expect(response.active[0].id).toEqual('aqua-cr');+ });+ test('INIT_SECOND_LAYER_GROUP copies current layer state', () => {+ const response = layerReducer(initalState, {+ type: CONSTANTS.INIT_SECOND_LAYER_GROUP+ });+ expect(initalState.activeB).toEqual([]);+ expect(initalState.active).toEqual(response.activeB);+ });+ test('TOGGLE_LAYER_VISIBILITY action toggles layer state visibility', () => {+ const response = layerReducer(initalState, {+ type: CONSTANTS.TOGGLE_LAYER_VISIBILITY,+ index: 1,+ visible: false,+ activeString: 'active'+ });+ expect(initalState.active[1].visible).toBeTruthy();+ expect(response.active[1].visible).toBeFalsy();+ });+ test('SET_RANGE_AND_SQUASH action updates palette-related props', () => {+ const response = layerReducer(initalState, {+ type: SET_RANGE_AND_SQUASH,+ props: { squash: true, min: 0.3 },+ layerId: 'terra-cr',+ activeString: 'active'+ });+ expect(initalState.active[1].squash).toEqual(undefined);+ expect(response.active[1].squash).toBeTruthy();+ expect(response.active[1].min).toEqual(0.3);+ });+ test('CLEAR_CUSTOM_PALETTE action removed custom value', () => {+ const customInitial = update(initalState, {+ active: { 1: { custom: { $set: 'custom-id' } } }+ });+ const response = layerReducer(customInitial, {+ type: CLEAR_CUSTOM_PALETTE,+ layerId: 'terra-cr',+ activeString: 'active'+ });+ expect(customInitial.active[1].custom).toEqual('custom-id');+ expect(response.active[1].custom).toEqual(undefined);+ });+ test('SET_CUSTOM_PALETTE action removed custom value', () => {+ const response = layerReducer(initalState, {+ type: SET_CUSTOM_PALETTE,+ layerId: 'terra-cr',+ activeString: 'active',+ paletteId: 'custom-id'+ });++ expect(initalState.active[1].custom).toEqual(undefined);+ expect(response.active[1].custom).toEqual(['custom-id']);+ });+ test('REMOVE_LAYER action removes terra-cr from layer array', () => {+ const response = layerReducer(initalState, {+ type: CONSTANTS.UPDATE_OPACITY,+ activeString: 'active',+ opacity: 0.4,+ index: 1+ });+ expect(response.active[1].opacity).toEqual(0.4);+ });+});