Set up function complete let's write out our tests so our tests are going to be pretty straightforward
in nature.
We really just want to make sure that we can call on message.
We're going to make sure that we update the order status and save it and we'll also make sure that we
emit some new events of order canceled publisher and at some point time we should probably also make
sure we call the act function back inside the TEST file.
I'm gonna write out three separate test this time.
We previously just combined three tests together into one and I'd said you probably shouldn't do this.
I really dislike in the course doing one thing and saying Oh yeah.
Don't do this so we're just gonna write this out correctly.
We're going to make three separate tests going to first say it updates the order status to canceled.
That's the first thing we want.
We want to take care of.
Well then make sure that we emit a an order canceled event and then naturally we should ask a message.
All right.
Each of these.
We've pretty much done before.
So should be pretty straightforward in nature.
In all three of them we definitely want to pull the listener order ticket data and message from a weight
setup.
And we're definitely going to want to in all three of them awaits listener.
On message and pass in data and message because they all need this.
Let's just do a copy paste right away.
I'm going to take those two lines and paste them in the other two tests and then we can go through and
fill out the expectations for each.
So on this first one I bet you could guess we're gonna do here.
We're going to reach into to our order collection we're gonna find the order that has been saved hopefully
updated we're gonna fetch it out of that collection and then just make sure that these stats property
was updated to canceled pretty much at so we'll do a an updated order will be and a wait order find
by order I.D. number.
We do have the order right there but in theory that is an out of date version of the order so then we'll
do it in expectation.
I'll make sure that the updated order status property equals order status dot cancelled as usual.
Let's say that exclamation on there we've been over that so many times now.
Is that should be it for a test number one we'll run all these and make sure they all fail at the end.
Let's just make them or put together some implementation for all three to get started.
Next up is order cancelled.
So we need to somehow take a look at our Nats wrapper mock right here.
We've been over this once before as well.
Remember the Nats wrapper it is a mock function or specifically the public function inside there.
So we're going to get the Nats wrapper dot client dot publish and we're gonna take a look at that mock
and just make sure that it was called and if we want to we can also make sure there was called with
a very specific set of arguments to back down at the order canceled events.
We're gonna throw in a Nats wrapper client not publish and then remember this Polish function is a just
Mark.
So to help typescript understand that we can then put down as just dots mark it's an typescript is gonna
understand that this thing is supposed to be a mock function and it's going to give us access to all
the appropriate properties on there without complaining to us a whole bunch.
Going gonna wrap that with parentheses and then remember if you want to get access to all the different
times its function has been invoked.
We can chain on a dot calls or see me marked calls.
That's better.
And that's gonna be an array of all the different times all the different arguments that this thing
has been provided over time so publish should have only been invoked one single time.
So take a look at the first time it was called and then maybe we want to just make sure that this event
that we just published.
How about let's say that we make sure it has the correct order I.D. inside of it back inside of our
listener.
Here's what we are publishing.
So we might just want to make sure that we are emitting the event with the correct order i.e. so that
we can get access to the second argument inside there.
Because remember the first argument is the subject or the channel name that we are publishing this event
to this is gonna give us back the entire data object as Jason.
So we have to turn it into an actual object and we can pull the I.D. property out of it.
So do a Jason pass wrap that entire thing with a set of parentheses and I'll sign this to you about
event data because that line is getting really long
then let's do an expectation here and we'll say that we expect event data dot I.T. to equal order i.e.
we can also just very simply maybe on the line right above all that stuff.
We could add in a very simple expectation just to make sure that the MOC itself is published function
actually was invoked though kind of two stage expectation is really nice in that regard because you
can first make sure that the thing called and then maybe even if it was called it might have been given
the incorrect arguments.
And so the two stage kind of setup or two expectations helps you debug stuff in the future.
So right above that event data stuff I'll put in a Nats wrapper client publish Do you have come on Ben
felt
now if you do not really remember this part right here from the last time we did it you could take that
value right there and just do a quick console log of it just to get a quick reminder of.
OK.
Yes.
Calls is the array of all the different times is my function was invoked on the last one.
Let's take this really quickly so we definitely done this one many times.
We're gonna take a look at our message and make sure that the ACH function wasn't booked.
I'll do it and expect message not EQ to have been called and that is it.
We are all done.
Let's save this we could definitely clean up some of the destruction arguments in here if we want to
by the way didn't really need ticket here I guess.
Didn't take it up here Charlie up to you at any rate let's make sure we just save the file and we'll
go run our tests the back of my terminal will change in two orders do an MP run test and for the first
go round we're gonna pray really just hope I suppose that all of our tests pass as we're looking for
yep looks good now that everything is passing as usual.
Still want to make sure that we can get our test to fail because they might be passing through some
fault on our side for all three tests we just wrote out I going to comment out the on message call so
there's three comments I'll then save this we'll now flip back over and just make sure that we've got
exactly three failures
Yep three failed.
Very good.
All right I'll undo those changes though they're now all on commented.
Say this again and we are all set.
All right.
That's one more step down.
We're getting closer and closer to finishing up this application.
We still have a pretty good amount to do.
So let's take a pause right here and move forward in just a moment.