Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

create notification unit tests #1576

Benjamin King572ee17e5b4e
diff --git a/web/js/modules/core/reducers.js b/web/js/modules/core/reducers.js
index c2169ce6a..aabe113c8 100644
--- a/web/js/modules/core/reducers.js
+++ b/web/js/modules/core/reducers.js
@@ -1,18 +1,14 @@
import { assign as lodashAssign } from 'lodash';
+export const defaultRequestState = {
+ isLoading: false,
+ error: null,
+ response: null,
+ type: null
+};
export function requestResponse(props = {}) {
- return lodashAssign(
- {},
- {
- isLoading: false,
- error: null,
- response: null,
- type: null
- },
- props
- );
+ return lodashAssign({}, defaultRequestState, props);
}
-
export function requestReducer(actionName, state, action, callback) {
const START = `${actionName}_START`;
const SUCCESS = `${actionName}_SUCCESS`;
@@ -20,7 +16,6 @@ export function requestReducer(actionName, state, action, callback) {
switch (action.type) {
case START:
return requestResponse({
- response: state.response,
isLoading: true
});
case SUCCESS:
@@ -34,6 +29,6 @@ export function requestReducer(actionName, state, action, callback) {
isLoading: false
});
default:
- return state;
+ return requestResponse({});
}
}
diff --git a/web/js/modules/link/actions.js b/web/js/modules/link/actions.js
index e0883065b..3d38b9ab0 100644
--- a/web/js/modules/link/actions.js
+++ b/web/js/modules/link/actions.js
@@ -1,18 +1,13 @@
-import { TOGGLE_URL, UPDATE_PERMALINK, REQUEST_SHORT_LINK } from './constants';
+import { UPDATE_PERMALINK, REQUEST_SHORT_LINK } from './constants';
import { requestAction } from '../core/actions';
-export function toggleUrl() {
- return {
- type: TOGGLE_URL
- };
-}
export function updatePermalink(queryString) {
return {
type: UPDATE_PERMALINK,
queryString: queryString
};
}
-export function requestShortLink(location, type, signal, debug) {
+export function requestShortLink(location, type, signal) {
return dispatch => {
return requestAction(dispatch, REQUEST_SHORT_LINK, location, type, signal);
};
diff --git a/web/js/modules/notifications/actions.test.js b/web/js/modules/notifications/actions.test.js
index cfe931874..5fa87a6c0 100644
--- a/web/js/modules/notifications/actions.test.js
+++ b/web/js/modules/notifications/actions.test.js
@@ -2,38 +2,15 @@ import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import fetchMock from 'fetch-mock';
import * as actions from './actions';
-import * as types from './constants';
+import * as constants from './constants';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
-const RESPONSE_BODY = [
- {
- id: 537,
- notification_type: 'outage',
- message: 'This is a test Outage',
- updated_at: '2018-05-20T16:26:43.013-04:00',
- created_at: '2018-05-20T16:23:37.049-04:00',
- starttime: null,
- endtime: null,
- applications: ['Worldview (OPS)'],
- domains: ['https://worldview.earthdata.nasa.gov'],
- dismissible: true,
- path: ''
- }
-];
-const MOCK_RESPONSE = {
- success: true,
- notifications: RESPONSE_BODY
-};
-
describe('Notification fetch action', () => {
- afterEach(() => {
- fetchMock.restore();
- });
- it('creates REQUEST_APP_NOTIFICATIONS_SUCCESS when fetching notifications is complete', () => {
- const loc = 'mock/notify-outage.json';
+ test('triggers start and success action types', () => {
+ const loc = 'mock/';
fetchMock.getOnce(loc, {
- body: MOCK_RESPONSE,
+ body: constants.MOCK_RESPONSE,
headers: {
'content-type': 'application/json',
'Access-Control-Allow-Origin': '*',
@@ -41,10 +18,10 @@ describe('Notification fetch action', () => {
}
});
const expectedActions = [
- { type: types.REQUEST_NOTIFICATIONS_START },
+ { type: constants.REQUEST_NOTIFICATIONS_START },
{
- type: types.REQUEST_NOTIFICATIONS_SUCCESS,
- response: JSON.stringify(MOCK_RESPONSE)
+ type: constants.REQUEST_NOTIFICATIONS_SUCCESS,
+ response: JSON.stringify(constants.MOCK_RESPONSE)
}
];
const store = mockStore({ notifications: {} });
@@ -55,3 +32,30 @@ describe('Notification fetch action', () => {
});
});
});
+describe('Notification post-request actions', () => {
+ test(
+ 'setNotication action returns ' +
+ constants.SET_NOTIFICATIONS +
+ 'action type with array',
+ () => {
+ const expectedAction = {
+ type: constants.SET_NOTIFICATIONS,
+ array: ['alerts', 'outages', 'messages']
+ };
+ expect(
+ actions.setNotifications(['alerts', 'outages', 'messages'])
+ ).toEqual(expectedAction);
+ }
+ );
+ test(
+ 'notificationsSeen action returns ' +
+ constants.NOTIFICATIONS_SEEN +
+ ' action type',
+ () => {
+ const expectedAction = {
+ type: constants.NOTIFICATIONS_SEEN
+ };
+ expect(actions.notificationsSeen()).toEqual(expectedAction);
+ }
+ );
+});
diff --git a/web/js/modules/notifications/constants.js b/web/js/modules/notifications/constants.js
index 83d2875d5..5e4d51ba9 100644
--- a/web/js/modules/notifications/constants.js
+++ b/web/js/modules/notifications/constants.js
@@ -9,3 +9,41 @@ export const SET_NOTIFICATIONS = 'SET_NOTIFICATION_RESPONSE';
export const NOTIFICATIONS_SEEN = 'NOTIFICATIONS_HAVE_BEEN_SEEN';
export const STATUS_REQUEST_URL =
'https://status.earthdata.nasa.gov/api/v1/notifications?domain=https%3A%2F%2Fworldview.earthdata.nasa.gov';
+export const MOCK_RESPONSE_BODY = [
+ {
+ id: 537,
+ notification_type: 'outage',
+ message: 'This is a test Outage',
+ updated_at: '2018-05-20T16:26:43.013-04:00',
+ created_at: '2018-05-20T16:23:37.049-04:00',
+ starttime: null,
+ endtime: null,
+ applications: ['Worldview (OPS)'],
+ domains: ['https://worldview.earthdata.nasa.gov'],
+ dismissible: true,
+ path: ''
+ }
+];
+export const MOCK_RESPONSE = {
+ success: true,
+ notifications: MOCK_RESPONSE_BODY
+};
+export const MOCK_SORTED_NOTIFICATIONS = {
+ messages: [],
+ outages: [
+ {
+ id: 537,
+ notification_type: 'outage',
+ message: 'This is a test Outage',
+ updated_at: '2018-05-20T16:26:43.013-04:00',
+ created_at: '2018-05-20T16:23:37.049-04:00',
+ starttime: null,
+ endtime: null,
+ applications: ['Worldview (OPS)'],
+ domains: ['https://worldview.earthdata.nasa.gov'],
+ dismissible: true,
+ path: ''
+ }
+ ],
+ alerts: []
+};
diff --git a/web/js/modules/notifications/reducers.js b/web/js/modules/notifications/reducers.js
index 728cba30b..07ebad236 100644
--- a/web/js/modules/notifications/reducers.js
+++ b/web/js/modules/notifications/reducers.js
@@ -7,7 +7,7 @@ import {
} from './constants';
import { assign } from 'lodash';
-const notificationState = {
+export const notificationReducerState = {
number: null,
type: '',
isActive: false,
@@ -18,7 +18,7 @@ export function notificationsRequest(state = {}, action) {
return requestReducer(REQUEST_NOTIFICATIONS, state, action);
}
-export function notificationsReducer(state = notificationState, action) {
+export function notificationsReducer(state = notificationReducerState, action) {
switch (action.type) {
case SET_NOTIFICATIONS:
if (action.array.length > 0) {
@@ -33,7 +33,7 @@ export function notificationsReducer(state = notificationState, action) {
case NOTIFICATIONS_SEEN:
return assign({}, state, {
number: null,
- type: null,
+ type: '',
isActive: true
});
default:
diff --git a/web/js/modules/notifications/reducers.test.js b/web/js/modules/notifications/reducers.test.js
new file mode 100644
index 000000000..4c54d609d
--- /dev/null
+++ b/web/js/modules/notifications/reducers.test.js
@@ -0,0 +1,78 @@
+import {
+ notificationsRequest,
+ notificationsReducer,
+ notificationReducerState
+} from './reducers';
+import * as constants from './constants';
+import { defaultRequestState } from '../core/reducers';
+
+describe('notificationsRequest reducer', () => {
+ test('should return the initial state', () => {
+ expect(notificationsRequest([], {})).toEqual(defaultRequestState);
+ });
+ test('Should set isLoading to true on Request Start', () => {
+ expect(
+ notificationsRequest([], {
+ type: constants.REQUEST_NOTIFICATIONS_START
+ })
+ ).toEqual({
+ isLoading: true,
+ error: null,
+ response: null,
+ type: null
+ });
+ });
+ test('Should return response upon request success ', () => {
+ expect(
+ notificationsRequest([], {
+ type: constants.REQUEST_NOTIFICATIONS_SUCCESS,
+ response: constants.MOCK_RESPONSE_BODY
+ })
+ ).toEqual({
+ isLoading: false,
+ error: null,
+ response: constants.MOCK_RESPONSE_BODY,
+ type: null
+ });
+ });
+});
+describe('notificationsReducer', () => {
+ test('should return the initial state', () => {
+ expect(notificationsReducer(undefined, {})).toEqual(
+ notificationReducerState
+ );
+ });
+ test(
+ constants.SET_NOTIFICATIONS +
+ 'action type should return object containing sorted mock object',
+ () => {
+ expect(
+ notificationsReducer([], {
+ type: constants.SET_NOTIFICATIONS,
+ array: constants.MOCK_RESPONSE_BODY
+ })
+ ).toEqual({
+ number: 1,
+ type: 'outage',
+ isActive: true,
+ object: constants.MOCK_SORTED_NOTIFICATIONS
+ });
+ }
+ );
+ test(
+ constants.SET_NOTIFICATIONS +
+ 'action type should return object containing sorted mock object',
+ () => {
+ expect(
+ notificationsReducer(notificationReducerState, {
+ type: constants.NOTIFICATIONS_SEEN
+ })
+ ).toEqual({
+ number: null,
+ type: '',
+ isActive: true,
+ object: {}
+ });
+ }
+ );
+});