Add tests to notifications module 07/68007/1
authorbogumil_zebek <bogumil.zebek@nokia.com>
Thu, 20 Sep 2018 07:23:34 +0000 (09:23 +0200)
committerbogumil_zebek <bogumil.zebek@nokia.com>
Thu, 20 Sep 2018 07:24:46 +0000 (09:24 +0200)
Issue-ID: AAI-1640
Change-Id: I69bee609539e3977996f9eb2bfc821432028464b
Signed-off-by: bogumil_zebek <bogumil.zebek@nokia.com>
src/generic-components/notifications/NotificationReducer.js
test/generic-components/notifications/NotificationReducer.test.js [new file with mode: 0644]

index e20a8fb..1f5122c 100644 (file)
 import NotificationConstants from './NotificationConstants.js';
 
 export default (state = null, action) => {
-  switch (action.type) {
-    case NotificationConstants.NOTIFY_INFO:
-      return {
-        type: 'default',
-        title: action.data.title,
-        msg: action.data.msg,
-        timeout: action.data.timeout
-      };
-    
-    case NotificationConstants.NOTIFY_ERROR:
-      return {
-        type: 'error',
-        title: action.data.title,
-        msg: action.data.msg,
-        timeout: action.data.timeout
-      };
-    
-    case NotificationConstants.NOTIFY_WARNING:
-      return {
-        type: 'warning',
-        title: action.data.title,
-        msg: action.data.msg,
-        timeout: action.data.timeout
-      };
-    
-    case NotificationConstants.NOTIFY_SUCCESS:
-      return {
-        type: 'success',
+    switch (action.type) {
+        case NotificationConstants.NOTIFY_INFO:
+            return createStatus('default', action);
+        case NotificationConstants.NOTIFY_ERROR:
+            return createStatus('error', action);
+        case NotificationConstants.NOTIFY_WARNING:
+            return createStatus('warning', action);
+        case NotificationConstants.NOTIFY_SUCCESS:
+            return createStatus('success', action);
+        case NotificationConstants.NOTIFY_CLOSE:
+            return null;
+        default:
+            return state;
+    }
+
+};
+
+function createStatus(type, action) {
+    return {
+        type: type,
         title: action.data.title,
         msg: action.data.msg,
         timeout: action.data.timeout
-      };
-    case NotificationConstants.NOTIFY_CLOSE:
-      return null;
-    
-    default:
-      return state;
-  }
-  
-};
+    };
+}
diff --git a/test/generic-components/notifications/NotificationReducer.test.js b/test/generic-components/notifications/NotificationReducer.test.js
new file mode 100644 (file)
index 0000000..40d6b05
--- /dev/null
@@ -0,0 +1,197 @@
+import NotificationReducer from 'generic-components/notifications/NotificationReducer';
+import NotificationConstants from "generic-components/notifications/NotificationConstants";
+
+
+describe('NotificationReducer', () => {
+    const defaultState = {
+        type: 'default',
+        title: 'some default title',
+        msg: 'some default message',
+        timeout: 1
+    };
+
+    it('Should return default state when action type is not supported', () => {
+        // given
+        const unsupportedAction = {
+            type: undefined
+        };
+
+        // when
+        const actualState = NotificationReducer(defaultState, unsupportedAction);
+
+        // then
+        expect(actualState).toEqual(defaultState);
+    });
+
+    it('Should return state with type default when action type is info', () => {
+        // given
+        const expectedState = {
+            type: 'default',
+            title: 'some title',
+            msg: 'some message',
+            timeout: 5
+        };
+
+        const infoAction =  {
+            type: NotificationConstants.NOTIFY_INFO,
+            data: {
+                title: "some title",
+                msg: "some message",
+                timeout: 5
+            }
+        };
+
+        // when
+        const actualState = NotificationReducer(defaultState, infoAction);
+
+        // then
+        expect(actualState).toEqual(expectedState);
+    });
+
+
+    it('Should return status with type success when action type is success', () => {
+        // given
+        const expectedState = {
+            type: 'success',
+            title: 'some title',
+            msg: 'some message',
+            timeout: 2
+        };
+
+        const infoAction =  {
+            type: NotificationConstants.NOTIFY_SUCCESS,
+            data: {
+                title: "some title",
+                msg: "some message",
+                timeout: 2
+            }
+        };
+
+        // when
+        const actualState = NotificationReducer(defaultState, infoAction);
+
+        // then
+        expect(actualState).toEqual(expectedState);
+    });
+
+    it('Should return status with type success when action type is success', () => {
+        // given
+        const expectedState = {
+            type: 'success',
+            title: 'some title',
+            msg: 'some message',
+            timeout: 2
+        };
+
+        const infoAction =  {
+            type: NotificationConstants.NOTIFY_SUCCESS,
+            data: {
+                title: "some title",
+                msg: "some message",
+                timeout: 2
+            }
+        };
+
+        // when
+        const actualState = NotificationReducer(defaultState, infoAction);
+
+        // then
+        expect(actualState).toEqual(expectedState);
+    });
+
+    it('Should return status with type error when action type is error', () => {
+        // given
+        const expectedState = {
+            type: 'error',
+            title: 'some title',
+            msg: 'some message',
+            timeout: 2
+        };
+
+        const infoAction =  {
+            type: NotificationConstants.NOTIFY_ERROR,
+            data: {
+                title: "some title",
+                msg: "some message",
+                timeout: 2
+            }
+        };
+
+        // when
+        const actualState = NotificationReducer(defaultState, infoAction);
+
+        // then
+        expect(actualState).toEqual(expectedState);
+    });
+
+    it('Should return status with type error when action type is error', () => {
+        // given
+        const expectedState = {
+            type: 'error',
+            title: 'some title',
+            msg: 'some message',
+            timeout: 2
+        };
+
+        const infoAction =  {
+            type: NotificationConstants.NOTIFY_ERROR,
+            data: {
+                title: "some title",
+                msg: "some message",
+                timeout: 2
+            }
+        };
+
+        // when
+        const actualState = NotificationReducer(defaultState, infoAction);
+
+        // then
+        expect(actualState).toEqual(expectedState);
+    });
+
+    it('Should return status with type warning when action type is warning', () => {
+        // given
+        const expectedState = {
+            type: 'warning',
+            title: 'some title',
+            msg: 'some message',
+            timeout: 2
+        };
+
+        const infoAction =  {
+            type: NotificationConstants.NOTIFY_WARNING,
+            data: {
+                title: "some title",
+                msg: "some message",
+                timeout: 2
+            }
+        };
+
+        // when
+        const actualState = NotificationReducer(defaultState, infoAction);
+
+        // then
+        expect(actualState).toEqual(expectedState);
+    });
+
+    it('Should return null when action type is close', () => {
+        // given
+        const expectedState = null;
+
+        const infoAction =  {
+            type: NotificationConstants.NOTIFY_CLOSE,
+            data: {
+                title: "some title",
+                msg: "some message",
+                timeout: 2
+            }
+        };
+
+        // when
+        const actualState = NotificationReducer(defaultState, infoAction);
+
+        // then
+        expect(actualState).toEqual(expectedState);
+    });
+
+});