Interactive interface for browsing global, full-resolution satellite imagery
Add modal module action/reducer tests #1576
Benjamin Kingb1a80fc7733e
diff --git a/web/js/containers/info.js b/web/js/containers/info.jsindex 75c3d279f..a17232f66 100644--- a/web/js/containers/info.js+++ b/web/js/containers/info.js@@ -2,7 +2,7 @@ import React, { Component } from 'react';import PropTypes from 'prop-types';import { connect } from 'react-redux';import {- requestLoadedPage,+ requestTemplate,renderTemplate,openCustomContent} from '../modules/modal/actions';@@ -145,10 +145,10 @@ const mapDispatchToProps = dispatch => ({window.open('pages/about.html?v=@BUILD_NONCE@', '_blank');} else {dispatch(- requestLoadedPage(+ requestTemplate(ABOUT_PAGE_REQUEST,'pages/about.html?v=@BUILD_NONCE@',- 'html'+ 'text/html'));dispatch(renderTemplate('About', 'modalAboutPage'));diff --git a/web/js/containers/share.js b/web/js/containers/share.jsindex f531fa11f..ce51e8036 100644--- a/web/js/containers/share.js+++ b/web/js/containers/share.js@@ -198,7 +198,7 @@ function mapStateToProps(state) {}const mapDispatchToProps = dispatch => ({requestShortLink: (location, signal) => {- return dispatch(requestShortLink(location, 'json', signal));+ return dispatch(requestShortLink(location, 'application/json', signal));}});diff --git a/web/js/modules/core/actions.js b/web/js/modules/core/actions.jsindex 76a307284..ad2b5b06e 100644--- a/web/js/modules/core/actions.js+++ b/web/js/modules/core/actions.js@@ -1,9 +1,11 @@-export function requestAction(dispatch, actionName, url, fileType, signal) {+export function requestAction(dispatch, actionName, url, mimeType, signal) {dispatch(startRequest(actionName));return new Promise(function(resolve, reject) {return fetch(url, { signal }).then(function(response) {- return fileType === 'json' ? response.json() : response.text();+ return mimeType === 'application/json'+ ? response.json()+ : response.text();}).then(function(data) {dispatch(fetchSuccess(actionName, data));diff --git a/web/js/modules/link/actions.test.js b/web/js/modules/link/actions.test.jsindex acdbde925..f80972926 100644--- a/web/js/modules/link/actions.test.js+++ b/web/js/modules/link/actions.test.js@@ -36,9 +36,11 @@ describe('Short Link request action', () => {}];const store = mockStore({ shortLink: {} });- return store.dispatch(actions.requestShortLink(loc, 'json')).then(() => {- expect(store.getActions()).toEqual(expectedActions);- });+ return store+ .dispatch(actions.requestShortLink(loc, 'application/json'))+ .then(() => {+ expect(store.getActions()).toEqual(expectedActions);+ });});test('creates ' + constants.REQUEST_SHORT_LINK_FAILURE + ' Action', () => {@@ -54,9 +56,11 @@ describe('Short Link request action', () => {}];const store = mockStore({ shortLink: {} });- return store.dispatch(actions.requestShortLink(loc, 'json')).catch(() => {- expect(store.getActions()).toEqual(expectedActions);- });+ return store+ .dispatch(actions.requestShortLink(loc, 'application/json'))+ .catch(() => {+ expect(store.getActions()).toEqual(expectedActions);+ });});});describe('updatePermalink action', () => {diff --git a/web/js/modules/modal/actions.js b/web/js/modules/modal/actions.jsindex d4af0830b..0dacf3cf4 100644--- a/web/js/modules/modal/actions.js+++ b/web/js/modules/modal/actions.js@@ -20,12 +20,12 @@ export function openCustomContent(key, params) {export function renderTemplate(headerText, template) {return {type: RENDER_TEMPLATE,- key: template,+ key: util.encodeId(template),template: template,headerText: headerText};}-export function requestLoadedPage(pageActionName, location, type) {+export function requestTemplate(pageActionName, location, type) {return dispatch => {return requestAction(dispatch, pageActionName, location);};diff --git a/web/js/modules/modal/actions.test.js b/web/js/modules/modal/actions.test.jsnew file mode 100644index 000000000..ba62423d2--- /dev/null+++ b/web/js/modules/modal/actions.test.js@@ -0,0 +1,130 @@+import configureMockStore from 'redux-mock-store';+import thunk from 'redux-thunk';+import fetchMock from 'fetch-mock';+import {+ openBasicContent,+ openCustomContent,+ renderTemplate,+ requestTemplate,+ onToggle+} from './actions';+import * as constants from './constants';+import util from '../../util/util';++const middlewares = [thunk];+const mockStore = configureMockStore(middlewares);+const ERROR_MESSAGE = 'There was an error';++describe('Modal open actions', () => {+ test(+ 'openBasicContent action returns ' ++ constants.OPEN_BASIC ++ 'header text, body text and encode key',+ () => {+ const expectedAction = {+ type: constants.OPEN_BASIC,+ headerText: constants.TEST_HEADER,+ bodyText: constants.TEST_BODY,+ key: util.encodeId('__BASIC_MODAL__' + constants.TEST_HEADER)+ };+ expect(+ openBasicContent(constants.TEST_HEADER, constants.TEST_BODY)+ ).toEqual(expectedAction);+ }+ );++ test('onToggle action returns ' + constants.TOGGLE + 'as type', () => {+ const expectedAction = {+ type: constants.TOGGLE+ };+ expect(onToggle()).toEqual(expectedAction);+ });++ test(+ 'openCustomContent action returns ' ++ constants.OPEN_CUSTOM ++ ' action type',+ () => {+ const customsKey = 'CUSTOM_MODAL_KEY';+ const customsParams = {+ offsetRight: '70px',+ headerText: constants.TEST_HEADER,+ modalClassName:+ 'toolbar-snapshot-modal toolbar-modal toolbar-medium-modal'+ };+ const expectedAction = {+ type: constants.OPEN_CUSTOM,+ key: customsKey,+ customProps: customsParams+ };+ expect(openCustomContent(customsKey, customsParams)).toEqual(+ expectedAction+ );+ }+ );++ test(+ 'renderTemplate action returns ' + constants.RENDER_TEMPLATE + ' type, ',+ () => {+ const templateModalKey = 'somePageName';++ const expectedAction = {+ type: constants.RENDER_TEMPLATE,+ key: util.encodeId(templateModalKey),+ template: templateModalKey,+ headerText: constants.TEST_HEADER+ };+ expect(renderTemplate(constants.TEST_HEADER, templateModalKey)).toEqual(+ expectedAction+ );+ }+ );+});+describe('Template fetching', () => {+ afterEach(() => {+ fetchMock.restore();+ });+ test('triggers start and success action types', () => {+ const loc = 'mock/';+ fetchMock.getOnce(loc, {+ body: constants.ABOUT_MOCK_RESPONSE,+ headers: {+ 'content-type': 'text/html',+ 'Access-Control-Allow-Origin': '*',+ 'Access-Control-Allow-Headers': 'Content-Type'+ }+ });+ const expectedActions = [+ { type: constants.ABOUT_PAGE_REQUEST_START },+ {+ type: constants.ABOUT_PAGE_REQUEST_SUCCESS,+ response: constants.ABOUT_MOCK_RESPONSE+ }+ ];+ const store = mockStore({ modal: {} });+ return store+ .dispatch(requestTemplate(constants.ABOUT_PAGE_REQUEST, loc, 'text/html'))+ .then(() => {+ expect(store.getActions()).toEqual(expectedActions);+ });+ });+ test('creates ' + constants.ABOUT_PAGE_REQUEST_FAILURE + ' Action', () => {+ const loc = 'mock/';+ fetchMock.mock(loc, {+ throws: ERROR_MESSAGE+ });+ const expectedActions = [+ { type: constants.ABOUT_PAGE_REQUEST_START },+ {+ type: constants.ABOUT_PAGE_REQUEST_FAILURE,+ error: ERROR_MESSAGE+ }+ ];+ const store = mockStore({ modal: {} });+ return store+ .dispatch(requestTemplate(constants.ABOUT_PAGE_REQUEST, loc, 'text/html'))+ .catch(() => {+ expect(store.getActions()).toEqual(expectedActions);+ });+ });+});diff --git a/web/js/modules/modal/constants.js b/web/js/modules/modal/constants.jsindex 856b15632..bd3249d65 100644--- a/web/js/modules/modal/constants.js+++ b/web/js/modules/modal/constants.js@@ -4,3 +4,11 @@ export const OPEN_BASIC = 'OPEN_MODAL_WITH_BASIC_CONTENT';export const RENDER_TEMPLATE = 'OPEN_MODAL_CONTAINING_LOADED_PAGE';export const ABOUT_PAGE_REQUEST = 'MODAL_ABOUT_PAGE_REQUEST';+export const ABOUT_PAGE_REQUEST_START = 'MODAL_ABOUT_PAGE_REQUEST_START';+export const ABOUT_PAGE_REQUEST_FAILURE = 'MODAL_ABOUT_PAGE_REQUEST_FAILURE';+export const ABOUT_PAGE_REQUEST_SUCCESS = 'MODAL_ABOUT_PAGE_REQUEST_SUCCESS';++export const ABOUT_MOCK_RESPONSE = '<html>MOCK</html>';+export const TEST_KEY = 'some-key-0000';+export const TEST_HEADER = 'TEST HEADER TEXT';+export const TEST_BODY = 'THIS IS TEST BODY TEXT';diff --git a/web/js/modules/modal/reducers.js b/web/js/modules/modal/reducers.jsindex 9b4e44503..579ca724a 100644--- a/web/js/modules/modal/reducers.js+++ b/web/js/modules/modal/reducers.js@@ -8,7 +8,7 @@ import {import { requestReducer } from '../core/reducers';import { assign as lodashAssign } from 'lodash';-const modalState = {+export const modalState = {headerText: '',bodyText: '',isOpen: false,@@ -35,8 +35,8 @@ export function modalReducer(state = modalState, action) {isOpen: action.key === state.key ? !state.isOpen : true,isCustom: false,id: action.key,- headerText: action.headerText,- bodyText: action.bodyText,+ headerText: action.headerText || '',+ bodyText: action.bodyText || '',customProps: {}});case OPEN_CUSTOM:@@ -45,16 +45,16 @@ export function modalReducer(state = modalState, action) {isCustom: true,customProps: action.customProps,id: action.key,- headerText: action.headerText,- bodyText: action.bodyText+ headerText: action.headerText || '',+ bodyText: action.bodyText || ''});case RENDER_TEMPLATE:return lodashAssign({}, state, {isOpen: action.key === state.key ? !state.isOpen : true,isCustom: false,id: action.key,- headerText: action.headerText,- bodyText: null,+ headerText: action.headerText || '',+ bodyText: '',template: action.template,customProps: {}});diff --git a/web/js/modules/modal/reducers.test.js b/web/js/modules/modal/reducers.test.jsnew file mode 100644index 000000000..e62c75269--- /dev/null+++ b/web/js/modules/modal/reducers.test.js@@ -0,0 +1,112 @@+import { defaultRequestState } from '../core/reducers';+import * as constants from './constants';+import { modalAboutPage, modalReducer, modalState } from './reducers';+import { assign as lodashAssign } from 'lodash';+import util from '../../util/util';++describe('modalAboutPage request reducer', () => {+ test('should return the initial state', () => {+ expect(modalAboutPage([], {})).toEqual(defaultRequestState);+ });+ test('Should set isLoading to true on Request Start', () => {+ expect(+ modalAboutPage([], {+ type: constants.ABOUT_PAGE_REQUEST_START+ })+ ).toEqual({+ isLoading: true,+ error: null,+ response: null,+ type: null+ });+ });+ test('Should return response upon request success ', () => {+ expect(+ modalAboutPage([], {+ type: constants.ABOUT_PAGE_REQUEST_SUCCESS,+ response: constants.MOCK_RESPONSE_BODY+ })+ ).toEqual({+ isLoading: false,+ error: null,+ response: constants.MOCK_RESPONSE_BODY,+ type: null+ });+ });+});+describe('main modalReducer', () => {+ test('should return the initial state', () => {+ expect(modalReducer(undefined, {})).toEqual(modalState);+ });+ test(constants.TOGGLE + 'action type should toggle open value', () => {+ const modalToggledOpenObj = { isOpen: true };+ expect(+ modalReducer([], {+ type: constants.TOGGLE+ })+ ).toEqual(modalToggledOpenObj);+ });+ test(+ constants.OPEN_BASIC + 'action type should update various modal props',+ () => {+ const basicOpenModalObject = {+ isOpen: true,+ isCustom: false,+ id: util.encodeId('__BASIC_MODAL__' + constants.TEST_HEADER),+ headerText: constants.TEST_HEADER,+ bodyText: constants.TEST_BODY,+ customProps: {}+ };++ expect(+ modalReducer([], {+ type: constants.OPEN_BASIC,+ headerText: constants.TEST_HEADER,+ bodyText: constants.TEST_BODY,+ key: util.encodeId('__BASIC_MODAL__' + constants.TEST_HEADER)+ })+ ).toEqual(basicOpenModalObject);+ }+ );+ test(+ constants.OPEN_CUSTOM + ' action type should update various modal props',+ () => {+ const customProps = { id: 'tester' };+ const modalCustomObject = lodashAssign({}, modalState, {+ isOpen: true,+ isCustom: true,+ id: constants.TEST_KEY,+ customProps: customProps+ });+ expect(+ modalReducer(modalState, {+ type: constants.OPEN_CUSTOM,+ key: constants.TEST_KEY,+ customProps: customProps+ })+ ).toEqual(modalCustomObject);+ }+ );+ test(+ constants.RENDER_TEMPLATE ++ ' action type should update various modal props',+ () => {+ const template = '<html>';+ const modalCustomObject = lodashAssign({}, modalState, {+ id: util.encodeId(constants.TEST_KEY),+ template: template,+ headerText: constants.TEST_HEADER,+ isOpen: true,+ isCustom: false+ });+ expect(+ modalReducer(modalState, {+ type: constants.RENDER_TEMPLATE,+ key: util.encodeId(constants.TEST_KEY),+ headerText: constants.TEST_HEADER,+ template: template+ })+ ).toEqual(modalCustomObject);+ }+ );+});diff --git a/web/js/modules/notifications/actions.test.js b/web/js/modules/notifications/actions.test.jsindex 754d51e36..3a7b44dd8 100644--- a/web/js/modules/notifications/actions.test.js+++ b/web/js/modules/notifications/actions.test.js@@ -30,7 +30,7 @@ describe('Notification fetch action', () => {];const store = mockStore({ notifications: {} });return store- .dispatch(actions.requestNotifications(loc, 'json'))+ .dispatch(actions.requestNotifications(loc, 'application/json')).then(() => {expect(store.getActions()).toEqual(expectedActions);});@@ -49,7 +49,7 @@ describe('Notification fetch action', () => {];const store = mockStore({ shortLink: {} });return store- .dispatch(actions.requestNotifications(loc, 'json'))+ .dispatch(actions.requestNotifications(loc, 'application/json')).catch(() => {expect(store.getActions()).toEqual(expectedActions);});@@ -59,7 +59,7 @@ describe('Notification post-request actions', () => {test('setNotication action returns ' +constants.SET_NOTIFICATIONS +- 'action type with array',+ ' action type with array',() => {const expectedAction = {type: constants.SET_NOTIFICATIONS,