1 00:00:00,900 --> 00:00:02,880 Instructor: We've now got specs for our reducer 2 00:00:02,880 --> 00:00:06,870 in place and we're able to see our spec fail 3 00:00:06,870 --> 00:00:09,390 at the command line as well, very plainly. 4 00:00:09,390 --> 00:00:10,530 Yep, everything's failing. 5 00:00:10,530 --> 00:00:11,760 Very good. 6 00:00:11,760 --> 00:00:14,340 That means that we're ready to go do some implementation 7 00:00:14,340 --> 00:00:15,633 inside of our reducer. 8 00:00:16,560 --> 00:00:18,870 Now this is gonna be a pretty basic reducer 9 00:00:18,870 --> 00:00:21,060 so hopefully again, you know, this is intended to 10 00:00:21,060 --> 00:00:22,620 be a little bit more of an advanced course, 11 00:00:22,620 --> 00:00:24,810 so hopefully the code that we're gonna write in 12 00:00:24,810 --> 00:00:27,630 here isn't gonna be too much of a surprise. 13 00:00:27,630 --> 00:00:29,970 So we'll start off with doing a switch 14 00:00:29,970 --> 00:00:31,653 over the actions type, 15 00:00:32,820 --> 00:00:35,920 and if the case is 16 00:00:38,610 --> 00:00:42,030 save comment, which we still need to import 17 00:00:42,030 --> 00:00:44,070 so let's do that before we forget it. 18 00:00:44,070 --> 00:00:48,273 So we'll import, save, comment, from. 19 00:00:49,350 --> 00:00:52,983 We'll go up into actions and types. 20 00:00:54,300 --> 00:00:55,800 So if this is the case, 21 00:00:55,800 --> 00:01:00,090 we want to return all of our existing comments 22 00:01:00,090 --> 00:01:02,940 which will be dot, dot, dot, state 23 00:01:02,940 --> 00:01:05,160 and then we'll also add on top, 24 00:01:05,160 --> 00:01:07,830 Action dot payload, the new comment. 25 00:01:07,830 --> 00:01:08,670 If you're not familiar 26 00:01:08,670 --> 00:01:11,310 with this syntax right here, the dot, dot, dot 27 00:01:11,310 --> 00:01:13,440 it's no problem, it's completely 28 00:01:13,440 --> 00:01:15,780 100% equivalent to writing something 29 00:01:15,780 --> 00:01:17,940 like state dot 30 00:01:17,940 --> 00:01:21,303 and cat action dot payload. 31 00:01:23,190 --> 00:01:25,530 So it takes the existing array 32 00:01:25,530 --> 00:01:29,370 and it concats on another array 33 00:01:29,370 --> 00:01:31,380 containing action dot payload. 34 00:01:31,380 --> 00:01:33,240 So if you're not familiar with the dot, dot, dot 35 00:01:33,240 --> 00:01:35,280 here, the spread operator, no problem. 36 00:01:35,280 --> 00:01:37,773 You can just go with the concat option instead. 37 00:01:39,300 --> 00:01:42,540 Okay, so let's go ahead and save this and see how we do. 38 00:01:42,540 --> 00:01:44,220 All saved. 39 00:01:44,220 --> 00:01:47,640 And we get cannot read property type of undefined. 40 00:01:47,640 --> 00:01:49,260 Okay, so that's kind of interesting. 41 00:01:49,260 --> 00:01:51,633 Let's go back over to our comments test file. 42 00:01:52,620 --> 00:01:55,500 So this is a great time or a great exercise 43 00:01:55,500 --> 00:01:58,980 where with our tests, when we start implementing code 44 00:01:58,980 --> 00:02:02,190 previous tests that we had passing start to fail. 45 00:02:02,190 --> 00:02:04,980 So let's look at the failed spec one more time, 46 00:02:04,980 --> 00:02:08,070 specifically says comments reducer handles action 47 00:02:08,070 --> 00:02:09,930 with unknown type. 48 00:02:09,930 --> 00:02:12,870 So if you recall, that's this spec right here 49 00:02:12,870 --> 00:02:15,690 and it was passing, everything was going fine, 50 00:02:15,690 --> 00:02:17,880 but now all of a sudden it started failing. 51 00:02:17,880 --> 00:02:20,430 So let's think a little bit about why it's failing. 52 00:02:21,810 --> 00:02:23,280 When we call a comment reducer, 53 00:02:23,280 --> 00:02:26,460 we're not passing in any arguments whatsoever. 54 00:02:26,460 --> 00:02:30,030 And so when the reducer is ran, state gets defaulted 55 00:02:30,030 --> 00:02:34,650 to empty array, and action is undefined, then immediately 56 00:02:34,650 --> 00:02:38,400 inside the switch we say find the property type 57 00:02:38,400 --> 00:02:39,540 of undefined. 58 00:02:39,540 --> 00:02:41,610 And so that's where our error is coming from. 59 00:02:41,610 --> 00:02:43,350 And so this is something that can be a little 60 00:02:43,350 --> 00:02:45,480 bit frustrating, having specs that pass 61 00:02:45,480 --> 00:02:46,560 and then you write some code 62 00:02:46,560 --> 00:02:48,780 and all of a sudden the previous specs fail. 63 00:02:48,780 --> 00:02:51,150 But again, that's kind of the purpose of specs. 64 00:02:51,150 --> 00:02:52,620 We wanna know when we make changes 65 00:02:52,620 --> 00:02:54,360 to our code that result in it 66 00:02:54,360 --> 00:02:57,510 maybe breaking in some case that we might not expect. 67 00:02:57,510 --> 00:03:02,040 So to solve this, I'm gonna pass in explicitly undefined 68 00:03:02,040 --> 00:03:03,930 and an empty object. 69 00:03:03,930 --> 00:03:08,310 And so now, when that switch looks for action dot type 70 00:03:08,310 --> 00:03:11,400 it will read off the type property on this empty object 71 00:03:11,400 --> 00:03:14,493 which is gonna be undefined, but it'll still run. 72 00:03:15,540 --> 00:03:20,520 And so I save my specs rerun, and everything goes green. 73 00:03:20,520 --> 00:03:21,483 Fantastic. 74 00:03:22,620 --> 00:03:25,795 So there's one last thing I think that we need to do. 75 00:03:25,795 --> 00:03:27,270 We need to wire up our reducer to our 76 00:03:27,270 --> 00:03:29,520 combined reducer's call. 77 00:03:29,520 --> 00:03:32,070 So let's take care of that in the next section 78 00:03:32,070 --> 00:03:34,440 and do a bigger view of the entire app. 79 00:03:34,440 --> 00:03:35,390 I'll see you there.