
上QQ阅读APP看书,第一时间看更新
Testing out our reducer
Now that we have written our reducer function, we can test it out by passing data directly to it. First, we will define the initial state, an empty array of posts:
const initialState = []
Now, we will need to define the action. To do this, we use the previously defined createPost action creator:
const action = createPost('dan', 'New post')
Finally, we execute the reducer and log the result:
const newState = postsReducer(initialState, action)
console.log(newState)
The preceding code should have the following output:
[
{
"user": "dan",
"text": "New post"
}
]
This means that we successfully inserted a new post into the application state.