import { ADD_ENTITY } from '../constants/ActionTypes';

function cmsAppState(state = [], action) {
  switch (action.type) {
    case ADD_ENTITY:
      return [
        ...state, {
          id: action.id,
          text: action.text,
          published: action.published
        }
      ];
    case 'TOGGLE_ENTITY':
      return state.map(entity => {
        if (entity.id !== action.id) {
          return entity;
        }
        return {
          ...entity,
          published: !entity.published
        };
      });
    default:
      return state;
  }
}
export default cmsAppState;

import expect from 'expect';
import deepFreeze from 'deep-freeze';

const testAddEntity = () => {
  const stateBefore = [];
  const action = {
    type: ADD_ENTITY,
    id: 0,
    text: 'Cms String',
    published: false
  };
  const stateAfter = [
    {
      id: 0,
      text: 'Cms String',
      published: false
    }
  ];

  deepFreeze(stateBefore);
  deepFreeze(action);

  expect(
    cmsAppState(stateBefore, action)
  ).toEqual(stateAfter);
};

const testToggleEntity = () => {
  const stateBefore = [
    {
      id: 0,
      text: 'Some entity',
      published: false
    },
    {
      id: 1,
      text: 'Another entity',
      published: false
    }
  ];
  const action = {
    type: 'TOGGLE_ENTITY',
    id: 1
  };
  const stateAfter = [
    {
      id: 0,
      text: 'Some entity',
      published: false
    },
    {
      id: 1,
      text: 'Another entity',
      published: true
    }
  ];

  deepFreeze(stateBefore);
  deepFreeze(action);

  expect(
    cmsAppState(stateBefore, action)
  ).toEqual(stateAfter);
};

testAddEntity();
testToggleEntity();
console.log('Passed!');

@@ -62,30 +62,20 @@
 ion 
-cmsAppState
+entity
 (state
- = %5B%5D
 , ac
@@ -145,32 +145,10 @@
 urn 
-%5B%0A        ...state, %7B%0A  
+%7B%0A
     
@@ -166,18 +166,16 @@
 ion.id,%0A
-  
         
@@ -193,34 +193,32 @@
 n.text,%0A        
-  
 published: actio
@@ -216,123 +216,64 @@
 ed: 
-action.published%0A        %7D%0A      %5D;%0A    case 'TOGGLE_ENTITY':%0A      return state.map(entity =%3E %7B%0A        if (entity
+false%0A      %7D;%0A    case 'TOGGLE_ENTITY':%0A      if (state
 .id 
@@ -301,31 +301,28 @@
     
-  
 return 
-entity
+state
 ;%0A      
   %7D%0A
@@ -317,22 +317,18 @@
 ;%0A      
-  %7D%0A  
+%7D%0A
       re
@@ -346,23 +346,18 @@
     
-  
 ...
-entity,%0A  
+state,%0A
     
@@ -372,22 +372,21 @@
 ished: !
-entity
+state
 .publish
@@ -398,20 +398,246 @@
     
-  %7D;%0A      %7D
+%7D;%0A  %7D%0A%7D%0A%0Afunction cmsAppState(state = %5B%5D, action) %7B%0A  switch (action.type) %7B%0A    case ADD_ENTITY:%0A      return %5B%0A        ...state, entity(undefined, action)%0A      %5D;%0A    case 'TOGGLE_ENTITY':%0A      return state.map(e =%3E entity(e, action)
 );%0A 

Add helper reducer to switch on an entity