From: bogumil_zebek Date: Mon, 11 Feb 2019 12:04:55 +0000 (+0100) Subject: Add tests to Inventory module X-Git-Tag: 1.4.0~6^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fsparky-fe.git;a=commitdiff_plain;h=eec9820acfb269178e8f8c8bdc2ecfb2d13ad5f4 Add tests to Inventory module Change-Id: I9a6b1749f80f8d4a4e28291e7f028bd7f864c86a Issue-ID: AAI-2143 Signed-off-by: Zebek Bogumil --- diff --git a/src/app/inventory/InventoryActions.js b/src/app/inventory/InventoryActions.js index 66e9339..7454c08 100644 --- a/src/app/inventory/InventoryActions.js +++ b/src/app/inventory/InventoryActions.js @@ -111,8 +111,7 @@ function getCountByDateQueryURL() { } export function onLoadTotalCountByDate() { - return function (dispatch) { - fetch(getCountByDateQueryURL(), { + return dispatch => fetch(getCountByDateQueryURL(), { credentials: SAME_ORIGIN, method: GET, headers: POST_HEADER @@ -124,12 +123,11 @@ export function onLoadTotalCountByDate() { ).catch( () => dispatch(getFailedCountByDateQueryEvent()) ); - }; + } export function onCountByTypeLoad() { - return function (dispatch) { - fetch(getCountByTypeQueryURL(), { + return dispatch => fetch(getCountByTypeQueryURL(), { credentials: SAME_ORIGIN, method: GET, headers: POST_HEADER @@ -141,12 +139,11 @@ export function onCountByTypeLoad() { ).catch( () => dispatch(getFailedCountByTypeQueryEvent()) ); - }; + } export function onTopographicMapMounted(requestObject) { - return function (dispatch) { - fetch(getDynamicTopographicQueryURL(requestObject.entityType), { + return dispatch => fetch(getDynamicTopographicQueryURL(requestObject.entityType), { credentials: SAME_ORIGIN, method: GET, headers: POST_HEADER @@ -160,6 +157,5 @@ export function onTopographicMapMounted(requestObject) { dispatch(getFailedTopographicVisualizationQueryEvent()); } ); - }; } diff --git a/src/app/inventory/InventoryReducer.js b/src/app/inventory/InventoryReducer.js index c42fc97..6ae3a5f 100644 --- a/src/app/inventory/InventoryReducer.js +++ b/src/app/inventory/InventoryReducer.js @@ -28,21 +28,18 @@ export default(state = {}, action) => { ...state, mapPlotPoints: action.data.plotPoints }; - break; case InventoryActionTypes.COUNT_BY_ENTITY_SUCCESS: return { ...state, countByType: action.data.countByType }; - break; case InventoryActionTypes.COUNT_BY_DATE_SUCCESS: return { ...state, countByDate: action.data.countByDate }; - break; case InventoryActionTypes.TOPOGRAPHIC_QUERY_FAILED: case InventoryActionTypes.COUNT_BY_ENTITY_FAILED: @@ -52,7 +49,6 @@ export default(state = {}, action) => { feedbackMsgSeverity: action.data.severity, feedbackMsgText: action.data.message }; - break; default: break; diff --git a/test/app/inventory/Inventory.test.js b/test/app/inventory/Inventory.test.js new file mode 100644 index 0000000..45b3450 --- /dev/null +++ b/test/app/inventory/Inventory.test.js @@ -0,0 +1,30 @@ +import React from 'react'; +import thunk from 'redux-thunk'; + +import configureMockStore from 'redux-mock-store'; +import expect from 'expect'; + +import Inventory from 'app/inventory/Inventory'; +import {shallow} from 'enzyme'; + + +const mockStore = configureMockStore([thunk]); +const store = mockStore({inventoryReducer: {}}); + +describe('Inventory component', () => { + + fetch = require('jest-fetch-mock'); + + it('should be rendered', () => { + + // when + let wrapper = shallow( + + ); + + // then + let actual = wrapper.render().text(); + expect(actual).toInclude('Active Inventory'); + }); + +}); diff --git a/test/app/inventory/InventoryActions.test.js b/test/app/inventory/InventoryActions.test.js new file mode 100644 index 0000000..9efe54d --- /dev/null +++ b/test/app/inventory/InventoryActions.test.js @@ -0,0 +1,219 @@ +import configureMockStore from 'redux-mock-store'; +import thunk from 'redux-thunk'; +import fetchMock from 'fetch-mock'; +import expect from 'expect'; + +import {onLoadTotalCountByDate, onCountByTypeLoad, onTopographicMapMounted} from 'app/inventory/InventoryActions'; +import {InventoryActionTypes} from 'app/inventory/InventoryConstants'; + + +const middlewares = [thunk]; +const mockStore = configureMockStore(middlewares); + + +function mockRequestToEntityCountHistoryEndpoint(postfix, response) { + fetchMock.getOnce( + `http://localhost:/rest/visualization${postfix}`, + response + ); +} + +describe('InventoryActions', () => { + + afterEach(() => { + fetchMock.restore() + }); + + describe('verify onLoadTotalCountByDate', () => { + it('creates COUNT_BY_DATE_SUCCESS response when there is no error', async () => { + + // given + mockRequestToEntityCountHistoryEndpoint("/entityCountHistory?type=graph",{ + status: 200, + body: { + "result": { + "keyA": "valueA", + "keyB": "valueB" + } + } + }); + + const expectedActions = [ + { + type: InventoryActionTypes.COUNT_BY_DATE_SUCCESS, + data: { + countByDate: { + "keyA": "valueA", + "keyB": "valueB" + } + } + }]; + + const store = mockStore(); + + // when + await store.dispatch(onLoadTotalCountByDate()); + + // then + expect(store.getActions()).toEqual(expectedActions); + }); + + + it('creates COUNT_BY_DATE_FAILED response when there is a problem with remote service', async () => { + + // given + mockRequestToEntityCountHistoryEndpoint("/entityCountHistory?type=graph",{ + status: 500 + }); + + const expectedActions = [ + { + type: InventoryActionTypes.COUNT_BY_DATE_FAILED, + data: { + message: 'Error fetching data from server', + severity: 'danger' + } + }]; + + const store = mockStore(); + + // when + await store.dispatch(onLoadTotalCountByDate()); + + // then + expect(store.getActions()).toEqual(expectedActions); + }); + }); + + + + describe('verify onCountByTypeLoad', () => { + it('creates COUNT_BY_ENTITY_SUCCESS response when there is no error', async () => { + + // given + mockRequestToEntityCountHistoryEndpoint("/entityCountHistory?type=table",{ + status: 200, + body: { + "result": { + "keyA": "valueA", + "keyB": "valueB" + } + } + }); + + const expectedActions = [ + { + type: InventoryActionTypes.COUNT_BY_ENTITY_SUCCESS, + data: { + countByType: { + "keyA": "valueA", + "keyB": "valueB" + } + } + }]; + + const store = mockStore(); + + // when + await store.dispatch(onCountByTypeLoad()); + + // then + expect(store.getActions()).toEqual(expectedActions); + }); + + + it('creates COUNT_BY_ENTITY_FAILED response when there is a problem with remote service', async () => { + + // given + mockRequestToEntityCountHistoryEndpoint("/entityCountHistory?type=table",{ + status: 500 + }); + + const expectedActions = [ + { + type: InventoryActionTypes.COUNT_BY_ENTITY_FAILED, + data: { + message: 'Error fetching data from server', + severity: 'danger' + } + }]; + + const store = mockStore(); + + // when + await store.dispatch(onCountByTypeLoad()); + + // then + expect(store.getActions()).toEqual(expectedActions); + }); + }); + + + + describe('verify onTopographicMapMounted', () => { + it('creates TOPOGRAPHIC_QUERY_SUCCESS response when there is no error', async () => { + + // given + const requestObject = { + entityType: "entityType" + }; + mockRequestToEntityCountHistoryEndpoint("/geovisualization/?entity=entityType",{ + status: 200, + body: { + "plotPoints": { + "keyA": "valueA", + "keyB": "valueB" + } + } + }); + + const expectedActions = [ + { + type: InventoryActionTypes.TOPOGRAPHIC_QUERY_SUCCESS, + data: { + plotPoints: { + "keyA": "valueA", + "keyB": "valueB" + } + } + }]; + + const store = mockStore(); + + // when + await store.dispatch(onTopographicMapMounted(requestObject)); + + // then + expect(store.getActions()).toEqual(expectedActions); + }); + + + it('creates TOPOGRAPHIC_QUERY_FAILED response when there is a problem with remote service', async () => { + + // given + const requestObject = { + entityType: "entityType" + }; + mockRequestToEntityCountHistoryEndpoint("/geovisualization/?entity=entityType",{ + status: 500 + }); + + const expectedActions = [ + { + type: InventoryActionTypes.TOPOGRAPHIC_QUERY_FAILED, + data: { + message: 'Error fetching data from server', + severity: 'danger' + } + }]; + + const store = mockStore(); + + // when + await store.dispatch(onTopographicMapMounted(requestObject)); + + // then + expect(store.getActions()).toEqual(expectedActions); + }); + }); +}); diff --git a/test/app/inventory/InventoryReducer.test.js b/test/app/inventory/InventoryReducer.test.js new file mode 100644 index 0000000..49f8e58 --- /dev/null +++ b/test/app/inventory/InventoryReducer.test.js @@ -0,0 +1,186 @@ +import fetchMock from 'fetch-mock'; +import expect from 'expect'; +import {InventoryActionTypes} from "app/inventory/InventoryConstants"; +import reducer from 'app/inventory/InventoryReducer'; + + + +function verifyStateProducedByReducer(action, currentState, expectedState) { + // when + const actual = reducer(currentState, action); + + // then + expect(actual).toEqual(expectedState); +} + +describe('InventoryReducer', () => { + + afterEach(() => { + fetchMock.restore() + }); + + it('verify store state after TOPOGRAPHIC_QUERY_SUCCESS action', async () => { + + // given + const action = { + type: InventoryActionTypes.TOPOGRAPHIC_QUERY_SUCCESS, + data: { + "plotPoints": { + "keyA": "valueA", + "keyB": "valueB" + } + } + }; + + const expectedState = { + state: "dummy", + mapPlotPoints: { + "keyA": "valueA", + "keyB": "valueB" + } + }; + + + const currentState = {state: "dummy"}; + + verifyStateProducedByReducer(action, currentState, expectedState); + + }); + + it('verify store state after COUNT_BY_ENTITY_SUCCESS action', async () => { + + // given + const action = { + type: InventoryActionTypes.COUNT_BY_ENTITY_SUCCESS, + data: { + "countByType": { + "keyA": "valueA", + "keyB": "valueB" + } + } + }; + + const expectedState = { + state: "dummy", + countByType: { + "keyA": "valueA", + "keyB": "valueB" + } + }; + + const currentState = {state: "dummy"}; + + verifyStateProducedByReducer(action, currentState, expectedState); + }); + + + it('verify store state after COUNT_BY_DATE_SUCCESS action', async () => { + + // given + const action = { + type: InventoryActionTypes.COUNT_BY_DATE_SUCCESS, + data: { + "countByDate": { + "keyA": "valueA", + "keyB": "valueB" + } + } + }; + + const expectedState = { + state: "dummy", + countByDate: { + "keyA": "valueA", + "keyB": "valueB" + } + }; + + const currentState = {state: "dummy"}; + + verifyStateProducedByReducer(action, currentState, expectedState); + }); + + it('verify store state after TOPOGRAPHIC_QUERY_FAILED action', async () => { + + // given + const action = { + type: InventoryActionTypes.TOPOGRAPHIC_QUERY_FAILED, + data: { + severity: "ERROR", + message: "Some error occurred" + } + }; + + const expectedState = { + state: "dummy", + feedbackMsgSeverity: "ERROR", + feedbackMsgText: "Some error occurred" + + }; + + const currentState = {state: "dummy"}; + + verifyStateProducedByReducer(action, currentState, expectedState); + }); + + it('verify store state after COUNT_BY_ENTITY_FAILED action', async () => { + + // given + const action = { + type: InventoryActionTypes.COUNT_BY_ENTITY_FAILED, + data: { + severity: "ERROR", + message: "Some error occurred" + } + }; + + const expectedState = { + state: "dummy", + feedbackMsgSeverity: "ERROR", + feedbackMsgText: "Some error occurred" + + }; + + const currentState = {state: "dummy"}; + + verifyStateProducedByReducer(action, currentState, expectedState); + }); + + it('verify store state after COUNT_BY_DATE_FAILED action', async () => { + + // given + const action = { + type: InventoryActionTypes.COUNT_BY_DATE_FAILED, + data: { + severity: "ERROR", + message: "Some error occurred" + } + }; + + const expectedState = { + state: "dummy", + feedbackMsgSeverity: "ERROR", + feedbackMsgText: "Some error occurred" + + }; + + const currentState = {state: "dummy"}; + + verifyStateProducedByReducer(action, currentState, expectedState); + }); + + it('verify store state after unknown action', async () => { + + // given + const action = { + type: 'unknown', + }; + + const expectedState = {state: "dummy"}; + + const currentState = {state: "dummy"}; + + verifyStateProducedByReducer(action, currentState, expectedState); + }); + +});