No they were done with mocking and let's go ahead and close mail.
J.S. and account dot J S and we can collapse those directories as well just to free up a bit of space
in the sidebar and what we're going to do from here is right.
Just a few more tests related to the user.
Then in the next lesson we'll write some tests focused on our tasks.
Now there are a couple of new techniques we need to explore to add some new tools to our tool belt.
And one is figuring out how we can send a file using super test.
So what I'm gonna do is send an image off to my upload Avatar endpoint and I'll make sure that I do
get a buffer stored in the Avatar field in the database.
So to get that done let's go ahead and kick things off by creating the brand new test case I'll call
test right here providing a name.
Something like should upload Avatar image then I can set this test up as an async function down below.
Now one thing we're gonna need to do is get access to an image that we can upload in the test cases.
Now to do this what we're going to do is create a new directory in our tests folder.
This is a directory we'll be using right now as well as in the next lesson for something kind of different.
And this directory is gonna be called fixtures in the testing world a fixture or fixtures plural are
things that allow you to set up the environment your tests are going to run in.
So what we're going to do is put an image inside of this directory that our tests can use when they're
trying to upload an image to the server to test that endpoint.
Now if you remember from earlier in the class I did provide you with a bunch of sample images that we
had use to test things out using postmen.
We're just going to grab one of those and use it for this as well.
Now if you don't have those you can find those over at links dot Meade dot I O forward slash files.
This is exactly where we grabbed them from a few sections ago.
You can download the note of course images zip extract it and get those images.
Now I still have that on my machine.
So what I'm gonna do is add an image into the fixtures directory.
I'll crack open my finder on the Mac or the file explorer depending on your operating system.
I have that directory in my downloads folder and right here what I'm going to do is go ahead and grab
a profile pic dot J P G.
So that is sitting right here.
That is the robot picture I'm gonna use command see to copy that into the clipboard that you can always
right click if you do not know the keyboard shortcut for your operating system.
We want to copy that file and bring it over to our fixtures directory.
Now right here I have that on the desktop that is node course task manager that is tests followed by
fixtures right here.
This is where I want to put that file so I will paste it using either the keyboard shortcut or the right
click menu.
Now we can close down the founder or file explorer and inside of the fixtures folder we should have
an image that we can use when testing that upload route.
As long as you're seeing an image inside of here then you're ready to actually fill out this test case.
Now to get started it's going to look very similar to what we've done with our other test cases.
I'll be using a wait with Super Test so I'll call a request passing the app in and then we're going
to specify the TTP method.
Here we are trying to use post with the following You are L that is forward slash users forward slash
me forward slash avatar.
Now in this case we do want to be authenticated.
So we'll go ahead and set that up as well.
Just down below I'll use set to configure this and we're going to set up authorization using as always
an iOS 6 template string we have a bearer space then we use the E6 template string syntax to interpolate
the following value that is user 1 dot tokens we're gonna grab that first token object and grab the
token property off of it exactly like we had done up above for the other test cases now that we have
the token set what we want to do is also attach the avatar field remember that is the one we're looking
for to do that we use dot attach attaches provided by Super Test allowing us to attach files.
So right here what we do if we want to attach a file is provide two arguments both are strings.
The first is the form field we're trying to set now our server is configured to look for Avatar we also
configured this when sending the image via postman and in this case the second is the path to the file
and this starts from the root of our project.
So for us that would be tests forward slash fixtures forward slash profile hyphen pic dot J P G.
So that is going to correctly upload that file to the following endpoint.
And what are we going to do while I'm going to expect that I get a two hundred back meaning that everything
went well let's go ahead and start here first then we'll focus on adding a couple of other assertions
down below right here I'll go ahead and save the test to suite and in a moment we should see 14 passing
tests down below and that is exactly what we get now we could continue on to write other assertions
and for this particular test case we can figure out what makes sense now we can't compare the binary
data for this image directly with what's stored in the database because remember we do use that sharp
NPM module to manipulate the image.
Now if we wanted to we could load this file in using the F S module I could manipulate it using these
same techniques and then compare the binary data but that is definitely overkill what we're going to
do is just check that binary data was indeed safe.
That's a great place to start.
So right here we're going to grab the user const user equals a weight with user dot find by I.D. And
we're just looking for user 1 I.D. as we had done before now there's one important thing we need to
talk about when it comes to our assertions and to illustrate this let's use expect what I'm gonna do
is expect this empty object to be this empty object and it's going to fail let's go ahead and see it
fail first then we'll talk about why it's failing so down below I can see I expected this and I received
this.
Now this doesn't seem to make much sense because we have the exact same thing.
What's important to note is that to be uses these triple equality operator provided by JavaScript So
when you first learn about if statements in JavaScript you learn about the equality operator now you
know that one is indeed equal to 1.
So this is why if I use expect and to be with these numbers it would work but you also learn that objects
are not equal to other objects even if they have these same properties.
When we're checking objects for equality it's checking to see if it's the exact same object in memory
and in this case it is not.
I'm creating two distinct objects even though they have these same properties.
So if we want to compare objects using expect we can't use to be the good news though is that we can
just switch over to two equal to equal does not use the triple equality operator.
Instead it uses an algorithm which is going to look at the properties on the object and compare those
and in this case I would expect those two to be equal down below the test suite is rerunning and now
we're getting a passing test suite.
So once again the whole purpose of bringing this up is to just show you that there are times where to
be is not going to work as expected when working with objects to equal is what you want.
Now what we're going to do is check the data stored on user right here.
We're going to look at user dot Avatar.
That is where the binary data should be stored and we're going to check if it's equal to something.
Now we don't have that real something.
We just want to see if it's a buffer if it's binary data stored in a buffer and expect gives us a way
to get that done.
Right here we can pass in a two to equal expect dot any this takes in the constructor function for some
sort of type.
And it's going to check if what you're looking at is indeed of that type.
And right here we're going to check if it's a buffer.
Now we could use expect dot any with string to check if it's any string or with something like number
to check if it's any number.
In this case we want to go ahead and use capital B buffer.
So once again we're looking at that Avatar property and we're checking if it equals any buffer.
If it does great.
If it does not that's a problem because the image hasn't been uploaded correctly.
Down below.
All we're going to do is save the user test suite to see what happens.
The test suite is rerunning we should have those 14 passing tests and that is exactly what we end up
getting.
Now that we've messed around with a few other techniques allowing us to write test cases you're gonna
go ahead and write a couple on your own as the challenge for the video.
So down below.
Let's go ahead and talk about what I'd like you to do right here.
You'll be creating two test cases for testing update functionality.
So one create should update valid user fields to try to update something like the user's name.
So up above I believe before my test user I have the name Mike I'll change that to something else.
Then I'm going to actually find the user in the database and make sure the name has changed.
So this makes sure that things go well when you're authenticated and you're changing a valid field down
below create should not update invalid user fields.
So in this case I could try to update something like location which doesn't exist.
And I would expect the correct error status code and you could always refer to the javascript code we
wrote for that route if you forget what the status code was last up test your work with the new test
cases in place save the test suite and you should go from 14 passing tests to 16 and that'll be where
we stop for now.
Take some time to get this done when you're done come back and click play
how'd that go.
Let's get to it.
Just down below with our first one right here a new test with the name I should update right here.
Valid user fields and we're going to try to update the name.
Let's go ahead and try to spell things correctly.
There we go.
Now this will be an async function and we will set it up as such.
And the first thing we're gonna do is fire off this request.
So what I'll do is use a wait calling request once again a theme amongst our tests that test our API
will pass in the application and we'll set up our call.
Now right here we want to use a patch.
So that is dot patch exactly what we used in Express when setting the endpoint up and the actual path
is forward slash users forward slash and me to update your user profile.
Next up we want to make sure we're sending some data.
So let's go ahead and use send it to send our Jason request body and I will update the name.
I'll go from a mike to Jess or at least I'll try to.
Next up we'll go ahead and set up our expect call.
So don't expect expecting a two hundred right here.
Now with this in place.
Things should at least be passing so we can save the test suite though we're still going to go ahead
and check the data to see if that's been changed.
Down below I did forget to add the authentication token on so I can see the test is failing and that's
okay.
I'll just make sure to add it in right here.
Dot set.
It's okay if you don't type everything out and hit the home run right away.
Sometimes you have typos and you have to write a little code rerun it again to get things working.
That's perfectly normal.
So right here set authorization we're gonna go ahead and set that equal to what we've been using before
bearer with these space followed by that user token User 1 dot tokens grabbing the first item and then
grabbing its token prop..
So now I would expect things to be working if I go ahead and save that file down below.
This suite is rerunning and it looks like we're all set.
Now we can move on to the second part for this test case fetching that user and confirming the name
is changed so console user will use a with user dot defined by I.D. again grabbing that test user.
User 1 I.D. is the I.D. of the user we're looking for and down below.
I'm going to expect something about the user name field.
I'm going to expect it to be Jess.
Now in this case we are comparing something that would work with to be but if we wanted to we could
use to equal as well.
That would work just as well.
So typically I'll find myself using just two equal and not using to be at all.
Right here I'm going to provide what I expect which would be Jess.
Now we can save this test case and if that one is passing we can move on to the second test case for
the challenge down below it is.
So let's create that second test should not update invalid user fields.
So to get started let's go ahead and grab this entire test case copy it to the clipboard and paste it
down below.
Now we can just go ahead and edit this to fit our needs.
So for example I'm going to change the name right here should not update invalid user fields.
Then down below we'll make some changes to the request as well.
For example I don't want to send a name I want to send something else like location and I'll try to
set that equal to Philadelphia even though it's not a field that we can set.
And next up will change what we're expecting down below in terms of a status code right here we're going
to expect a four hundred which is what we would get back if we tried to update an invalid field.
Now down here there's no need to do anything like find the user and make sure that their name did not
change.
You could do that if you wanted to but that would definitely be overkill.
All right.
Right here the test is running.
We can see that everything is passing so far and now we have 16 passing tests.
So with this in place we've covered all of the basic techniques necessary for testing.
We know how to test our requests with or without authentication.
We know how to mock libraries and how to check the database to make sure everything's when as expected.
And with this in place what I'd like to do to wrap up this section is do something similar for our tasks.
We'll figure out how we can create some task data for our test user and then we'll write some tests
for a couple of those endpoints as well using what we've already learned.
So there's not a lot of new information to cover but we are going to get a bit more experience working
with testing and creating tests.
Then we'll be done with the section.
I'm excited to get to that.
So let's jump right in to the next one.