In this section, we, well I guess more appropriately you, are going to be writing two test cases that verify patch works as expected. We're going to take one Todo that's not completed and make it complete, and we'll take a second one that is complete and make it incomplete.
Now, in order to do that, we are going to need to tweak the seed data we have in our server.test file. The seed data right in server.test file is two Todo items; neither of them have the completed property specified, which means it's going to default to false. For the second item, we're going to go ahead and set it. We're going to set completed: true and we're also going to set completedAt equal to whatever value we want. You can choose any number at all. I'm going to go ahead and use 333:
const todos = [{
_id: new ObjectID(),
text: 'First test todo'
},{
_id: new ObjectID(),
text: 'Second test todo',
completed: true,
completedAt: 333
}];
Now we have two Todos that are going to let us toggle both ways. To get started down below I will help you create the describe an It block so we're on the same page, but you will be responsible for filling out the actual test cases. This section is basically going to be a challenge because we've done a lot of this stuff before. First up, the describe block. We're going to describe this group of tests; we'll use the method followed by the URL to do just that, then we can go ahead and add our function, and then we can define our two test cases:
describe('PATCH /todos/:id', () => {
});
The first test is going to take our first Todo and set its text equal to something else, and we'll change completed from false to true, it('should update the todo'). We can provide our function with the done argument, and I'll leave some comments inside in just a moment to give you an idea as to how I'd like you to accomplish this. The second test is going to be for toggling that second Todo where the completed value is already equal to true, and then it('should clear completedAt when todo is not completed'). This test case is going to make sure that when we go ahead and remove the completed status, setting it equal to false, completedAt gets cleared. Now, for the first test case what you're going to do is grab the ID of the first item, grab id of first item, then you're going to make our patch request; you're going to provide the proper URL with the ID inside of it, and you're going to use send to send some data along as the request body. For this one I want you to update text, set it equal to whatever you like, and you're going to set completed equal to true. Now, once you send that off you'll be ready to make your assertions and you're going to make one assertion using the basic system, assert that you get a 200 status code back, and you're going to make one custom assertion. The custom assertion is going to verify that the response body has a text property equal to the text you sent in, text is changed. You're going to verify that completed is true, and you're also going to verify that completedAt is a number, and you can use the .toBeA method available inside of expect to get that done. Now, for the second test we're going to do something similar but we're just going to go in the other direction; we're going to grab id of second todo item, you're going to update the text to something different, and you're going to set completed to false. Then you can make your assertions. Once again, we will be expecting 200 for this one and we will be expecting that the response body now represents those changes, that the text is changed to whatever you happen to pick. I also want you to check that completed is now false and to check that completedAt is null, and you can use the .toNotExist method available on expect to make that assertion. This is what you need to do to complete the test suite. Once you're done I want you to run npm test and make sure both test cases pass.