Let's put a test together around our order cancel listener s I mentioned this is an entirely optional
video so if you don't want to do any testing if you're just tired of it inside this course no problem.
Just pause right here and move on to the next video.
Otherwise stick around and let's get to it.
Let's get started inside of our test directory.
We'll make a new file of order canceled listener test not t s
and then inside of here.
Once again we'll make a setup function it'll be async at the top.
We'll immediately add in a bunch of different imports and then write out the entire setup function so
as usual.
We definitely need our order answer listener from up one directory order and a listener.
We definitely need our Nats rapper from up 3 directories we get that Nats rapper let's great the listener
right away so make a listener as New Order canceled listener and past in my Nats rapper
after that we're going to create a new order and save it to our database.
This is going to be the order that we're going to eventually try to cancel.
So at the top once again I will import my order model from up 3 directories
then create an order with order dot build
and we do have to provide a different a few different properties for the thing.
So we do have to give it a realistic looking I.D. a version probably hardcoded at zero and then a user
I.D. at the price and the status over the I.D. and the status.
We're going to need the Mongoose library to generate the I.D. and we'll need our order status in from
our com module.
Let's add those imports at the very top
and our order status income from the company module.
So now instead of order duck billed we'll pass it an I.D. coming from Mongoose stock type start object
I.D. to hex string.
I'll give it a status of order status dot created a price.
How about 10 user I.D. of whatever and what other property am I missing here.
Let's see version of course we'll give a version of 0
then immediately after creating the order let's save it.
So we'll do an order not save and we'll make sure to await that
case.
That is our initial setups.
Now we can create a data object that's going to try to mark this order as being canceled so for that
we're definitely going to need our order canceled event interface from the common module let's import
that as well.
So order canceled event
and then after we create our order we'll make our data object.
It will be of type order canceled event at data
so we need to make sure that this thing has the same ideas as the order we had just created.
Well given an idea coming from order dot I.D. and there's a couple of other properties we have to define
as well we need to make sure this thing also has a version a ticket and inside there should be some
kind of I.D..
That's the idea that ticket doesn't only matter because we're not using it here.
Well that really matters is the version number in the version should definitely be the previous version
plus one we'll get a version add 1
then we'll give it a ticket with an I.D..
And again we can give it any I.D. we want because we're not really using that I.D..
And then finally our message object to the message object as usual we will import the message type from
node that string at the top.
I'm going to tell typescript to ignore this next line and we'll make our message
so it'll have the ACT function.
It's going to just mark.
Same exact thing we've done it many times for now that we've got everything put together as usual time
to return it.
So the return listener data message in the order as well
said we can write out the actual test and we'll say it updates the status of the order and I'll make
a second test just to say again X the message
will D structure off our listener data message in order from calling a weight setup and then await the
listeners on message function to which we'll pass in our data and message and then as usual we really
want those two lines inside of both our tests.
So I will copy paste them down to 4 update the status of the order.
Well I bet you can guess what we're gonna do there.
We're going to refresh the order out of the database and make sure that its status was updated you to
canceled.
That's pretty much it.
Give an updated order and that will come from a wait order find by I.D. And we want to look up the order
I.D. So the existing order one is the same idea right there the order dot I.D. we can expect the updated
order exclamation not status to equal the order status enum canceled property
date and then our second test right here.
I bet you can guess what we're gonna do here.
Yeah.
Just a simple expect message dot EQ to have been called and that's it let's flip back over to a terminal
and make sure these two tests are passing.
So back over at my terminal Yep looks like everything's good to go.
However as usual we'll do a quick comment out of the on message call on both and it just makes sure
we now have two test failures get two failures.
That means that our tests are working as expected.
So I will uncommon but those await listener on message calls and we are also at this test again.
I know I said it so many times really boring test looks identical to everything else.
But again we now know that this thing works as expected.
Does pause right here and move on to our next topic in just a moment.