Instructor: We've now got specs for our reducer
in place and we're able to see our spec fail
at the command line as well, very plainly.
Yep, everything's failing.
Very good.
That means that we're ready to go do some implementation
inside of our reducer.
Now this is gonna be a pretty basic reducer
so hopefully again, you know, this is intended to
be a little bit more of an advanced course,
so hopefully the code that we're gonna write in
here isn't gonna be too much of a surprise.
So we'll start off with doing a switch
over the actions type,
and if the case is
save comment, which we still need to import
so let's do that before we forget it.
So we'll import, save, comment, from.
We'll go up into actions and types.
So if this is the case,
we want to return all of our existing comments
which will be dot, dot, dot, state
and then we'll also add on top,
Action dot payload, the new comment.
If you're not familiar
with this syntax right here, the dot, dot, dot
it's no problem, it's completely
100% equivalent to writing something
like state dot
and cat action dot payload.
So it takes the existing array
and it concats on another array
containing action dot payload.
So if you're not familiar with the dot, dot, dot
here, the spread operator, no problem.
You can just go with the concat option instead.
Okay, so let's go ahead and save this and see how we do.
All saved.
And we get cannot read property type of undefined.
Okay, so that's kind of interesting.
Let's go back over to our comments test file.
So this is a great time or a great exercise
where with our tests, when we start implementing code
previous tests that we had passing start to fail.
So let's look at the failed spec one more time,
specifically says comments reducer handles action
with unknown type.
So if you recall, that's this spec right here
and it was passing, everything was going fine,
but now all of a sudden it started failing.
So let's think a little bit about why it's failing.
When we call a comment reducer,
we're not passing in any arguments whatsoever.
And so when the reducer is ran, state gets defaulted
to empty array, and action is undefined, then immediately
inside the switch we say find the property type
of undefined.
And so that's where our error is coming from.
And so this is something that can be a little
bit frustrating, having specs that pass
and then you write some code
and all of a sudden the previous specs fail.
But again, that's kind of the purpose of specs.
We wanna know when we make changes
to our code that result in it
maybe breaking in some case that we might not expect.
So to solve this, I'm gonna pass in explicitly undefined
and an empty object.
And so now, when that switch looks for action dot type
it will read off the type property on this empty object
which is gonna be undefined, but it'll still run.
And so I save my specs rerun, and everything goes green.
Fantastic.
So there's one last thing I think that we need to do.
We need to wire up our reducer to our
combined reducer's call.
So let's take care of that in the next section
and do a bigger view of the entire app.
I'll see you there.