In this video we're going to take a look at how we can add in a test to make sure that the new root
handler in the update root handler are both actually publishing events that get started.
I'm gonna go back to my underscore underscore mocks Nats wrapper file.
Inside there we've got that fake implementation of the published function.
So if we really want to make sure that we are somehow publishing an event off to Nats we really are
going to want to make sure that this function right here truly gets executed.
If this thing gets called that means that we are 100 percent trying to actually publish an event rather
than providing this fake implementation this function.
We're going to instead provide a mock function a mock function is essentially a fake function but it
allows us to make tests around it or make expectations around it so we can expect that a mock function
it's executed or that it gets executed with some particular arguments.
So I'm going to delete our little fake implementation and I will replace that with just not a fan.
That is how we create a muck.
This is going to return a new function and assign it to the property of publish.
That is a function that can be called from anything inside of application.
And that function internally is going to keep track of whether or not it has been called what arguments
it's been provided.
And so on.
So we can make some expectations around it.
Now if we try to run our code like this right now none of our task or a lot of our tests are not going
to actually pass.
The reason for that is I remember eventually our publisher the base publisher is going to try to call
this function that's going to provide a callback function that callback back function must be invoked
for the publisher to think that everything went ok there's not quite enough to just provide this fake
function because even though it will be invoked it needs to take the callback function that it will
be provided and call it.
In other words we need to still do this right here.
So we need to still do this but we also want to have a mock function to to kind of do both them together
to have a mock function and have a custom implementation.
Here's what we're gonna write out we're gonna put in jest not f n and we'll chain on mock implementation
and then to this we're going to provide a function.
So this is the actual function that will be invoked when someone tries to run publish when someone tries
to run publish it's still going to keep track of the fact that this thing was invoked a number of times
it was invoked what arguments it was provided and that will allow us to run some tests and whatnot but
we will also be able to execute this function right here taken that callback and invoke it to make sure
that the base publisher is still happy.
Now we do still have to write the same kind of function right here so this thing is going to be invoked
by the based publisher which means it's going to receive a subject that is a string data that is a string
and a callback function as well.
That is a type function that doesn't return anything then inside the function body.
Once again all we're going to do is execute callback.
Now we see this right now.
All of our tests should still run the same thing and go back over my test suite.
Yeah looks like everything is still running as expected but now we can also start to write out some
assertions and make sure that that published function is actually being invoked.
Let me show you how back inside of my new test file I'm going to find roots underscore underscore test
new test.
Yes I'm going to go down to the very bottom the thing and I'm going to write out a new expectation or
new test.
I'll say it publishes an event
to mark that function as async and then inside of here I want to try to successfully create a new ticket.
And then after creating the ticket we're going to try to take a look at that fake function the one we
just put together the mock one we're going to make sure the thing that that thing was actually invoked
but to actually create a ticket I'm going to go up to the test that we run out just before I'm going
to copy where we created a title and then made a request to create a ticket with the title and a given
price and expected to get back responsive to a one I'm going to copy all that and paste it down inside
my test so then right after we successfully make this request right after that we should be able to
say that the publish function was called so to do so we're gonna go to the very top of the file and
we're going to import the fake Nats wrapper.
Now when I say fake we're actually going to import the real that's wrapper.
But once again just even inside of our test file is going to redirect that import for us and it's going
to instead give us the mark we're gonna try to get the real that's wrapper but it's going to give us
the mock one instead by going to import Nat's wrapper from up one directory up another Nats dash wrapper
then down inside the tests we just wrote right now let us do a console lug of Nat's wrapper and I want
you to see something really interesting.
Let's save this to flip back over now we're gonna see a lot of console logs right here but one of them
we should see with a really big console log.
And here it is right here though it's right underneath the pass statement for a new test.
Yes.
Here's the console log.
We just did.
You'll notice that this is the fake implementation we just put together.
This is the contents of the fake or the mock and that's wrapper file though it's some plain object that
has a client property that has an object that has a public function and the published thing is a mock
function.
That's what all this stuff is right here.
But these are a bunch of helpers that we're going to allow us to make sure that this thing was actually
called was invoked to some certain arguments and so on.
So now we can actually write out an expectation down here at the very bottom going to put down here
expect that's rapper not client not publish.
To have been called.
That's how we're going to test this thing and make sure that the publish function actually successfully
gets invoked right after creating a ticket before testing this out.
Our terminal.
One other very quick thing to go inside of our Nats wrapper file.
We created this mock implementation exactly one time the same mock function gets reused for every single
one of our tests.
Though usually between each of our tests we want to reset this mock function.
Remember the mock function internally is going to record how many times it gets called the different
arguments it is provided and so on.
We're going to eventually write out some different expectations about those number of calls the arguments
and so on.
We want to make sure that between every single test we reset that data that we're not somehow running
a test and polluting one test with data from another 10.
Long story short back inside of our test setup.
Yes file that's why we're doing all of our initial test config.
We're gonna find out before each statement and inside there we'll write out.
Just thought clear all box just have to do that one time and that's pretty much it.
All right.
Let's save this.
Go back over to our terminal and we're going to make sure that all of our tests are passing in specific
the new test suite.
Right there.
Yep looks like it's good.
That means we are successfully calling that Polish function well well before continuing on because that's
pretty much it.
I just want to put every similar test inside of our update file as well and make sure that the published
function gets called so I gonna go to my update test.
Yes file at the very top I will import Nats rapper from up one directory up another that's wrapper I'm
then going to go down to the bottom to file I'll add in it abolishes an event
then instead of here we want to try to successfully edit or update a ticket and then we will make sure
that the publish function got invoked so we can very easily come back up to the previous test.
We'll just take some code right here.
It looks like this one right here was around testing to make sure a ticket was actually updated.
I can take everything from cookie on down to right after we do the update with the put we've got cookie
one request second request I'm going to copy all that go down to the bottom to file a stat all in
and then right after we'll do and expect that's wrapper client.
Wish to have an alt right.
Let's say that run these tests as well and make sure that the update is passing.
And where is it.
Yep there it is right there.
Update is passing.
Let's say we are good to go.
Okay.
That's pretty much it well we've got a working test suite now.
We also have the ability to add in tests around publishing events which is just fantastic.
So let's take a pause right here in as usual.
Continue in just a minute.