14

How React Testing Library Can Improve Your Mental Health [Part 2]

 3 years ago
source link: https://hackernoon.com/how-react-testing-library-can-improve-your-mental-health-part-2-071b31hk
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

@macMac Wasilewski

During the day a developer. A husband and dad at night.

You can find the first part here: Why testing?

0 reactions

Let’s continue writing our Redux tests. The file we are testing is:

0 reactions
const initialState = {
   todos: [
       { id: 1, text: 'Do boring stuff', completed: false, color: 'purple' },
       { id: 2, text: 'Write tests!', completed: false, color: 'blue' }
   ],
   filters: {
       status: 'All',
       colors: []
   }
}
function nextTodoId(todos) {
const maxId = todos.reduce((maxId, todo) => Math.max(todo.id, maxId), -1)
return maxId + 1
}
export default function appReducer(state = initialState, action) {
switch (action.type) {
case 'todos/todoAdded':
           {
return {
                   ...state,
                   todos: [
                       ...state.todos,
                       {
                           id: nextTodoId(state.todos),
                           text: action.payload,
                           completed: false
                       }
                   ]
               }
           }
case 'todos/todoToggled':
           {
return {
                   ...state,
                   todos: state.todos.map(todo => {
if (todo.id !== action.payload) {
return todo
                       }
return {
                           ...todo,
                           completed: !todo.completed
                       }
                   })
               }
           }
case 'filters/statusFilterChanged':
           {
return {
// Copy the whole state
                   ...state,
// Overwrite the filters value
                   filters: {
// copy the other filter fields
                       ...state.filters,
// And replace the status field with the new value
                       status: action.payload
                   }
               }
           }
default:
return state
   }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK