Let's do a very quick test around our order cancel listener just make sure it's working as expected.
We're going to go through this rather quickly.
So let's get to it inside of our test directory.
We'll make an order cancelled listener test to yes file at the very top we'll make a setup function
once again
then inside of here we're going to need to create our listener.
Create a ticket.
Create the event create a message return everything.
So first off we will create the listener itself.
I'll do a listener is a new order canceled listener and we're going to have to pass in a client when
we do that.
So we'll assume that we're going to import Nats wrapper that client let's add in an import for both
those first I'll get Nats rapper from up 3 directories.
That's rapper and my order answered listener from up one directory.
We then need to create a ticket and save it.
So I'll get my ticket model
and go
don't make a new ticket or not new ticket.
Ticket Bill.
There we go.
Throw in a fake title rice.
What else do we need here.
We need a user I.D. and we also need an order I.D. as well.
So the order I.D. We are going to have to also include inside the order a canceled event that eventually
flows into the listener itself.
So we're probably going to want to generate a realistic looking I.D. and assign it to a separate variables
we can make use of it later on.
So for that let's import mongers.
Once again
and then make an order i.e. variable which will be Mongoose type stock object i.e. to X string.
Now if we tried to add in the order I.D. like so you'll notice we get an error.
The reason for that is we had assumed that whenever we first create a ticket or whenever we first build
one in this case we would not yet have an order I.D. to assign to it.
So the interface so we had assigned to the build function specifically says not going to have an order
I.D. property.
Now we could certainly change that and we could say that the order I.D. property is optional when we
are first building a ticket.
But I would be kind of changing our code just to make our tests a little bit easier.
I really do not expect that in reality in our actual application code we're ever going to create a ticket
and simultaneously assign it an order I.D..
So I'm not going to go change the interface.
Instead we'll use a very very simple workaround.
Right after we build the ticket we'll just set the order I.D. property they'll do a ticket set of order
I.D..
Very simple workaround and definitely better in long term after that we'll save the ticket and there
we go.
Next up we're going to build our data events for this Let's import the definition or the interface of
our event from our common module so at the top we'll get our order canceled event
from our common module
so we'll build up a data object of type order canceled events at data and then as usual we'll be told
how to actually implement this thing.
So we need an I.D. a version of the order itself.
And then the tickets I.D. our I.D. is the order I.D. reversion doesn't really matter here.
We'll just throw in a zero for the ticket itself.
It's gonna be this tickets I.D. they will put in a ticket with an idea of ticket I.D.
after that.
Build up our message object
and this will be of AK.
Just not at Ben let's import that message type at the very top
and I think that is it for our setup.
Very last thing have to do.
Well of course we need that little us ignore thing just to tell typescript.
Don't sweat it.
And then finally let's return our message data.
Tickets order idea because we'll probably need to reference that.
Who knows maybe at some point in time and the listener itself we can actually write out our test rather
than writing out to tests and trying to make sure that say we update the tickets and save it.
And then a separate one for making sure Ach it's called and maybe even a separate one for the publisher.
Let's just condense this all down to one test just to save ourselves a little bit of time here so I
can say it updates the tickets publishes an event and acts the message if you want to absolutely feel
free.
I would actually even recommend you do this right.
Split this into three separate tests.
I'm just gonna put it all into one just to save ourselves a little bit of time.
For the purposes of this video it's one side of fear we'll call our setup function.
Let's pull out the message data ticket.
Order I.D. and a listener from a weight setup then as usual we'll call the on message function on a
listener.
So a wait listener on message send data and the message.
Now we'll take a look into our database.
We're going to take a look at the tickets collection.
We're going to find the tickets right here or the same idea and we're going to make sure that it's order
I.D. property was unset it should be equal to undefined so define a updated ticket that will be a way
ticket find by Idi.
We want to find the tickets I.D. And we're going to expect that things.
Order.
I.D..
Property to be undefined.
Expect updated ticket that order I.D. to or not to be defined.
There we go.
And we gonna throw that exclamation in as usual it's Alex.
Good.
Let's also make sure that we call the act function.
So do expect message dot EC Do you have been called and then finally we'll also make sure that the published
function on our Nats wrapper client was invoked as well.
I'll do it and expect Nats wrapper client publish to have been dealt.
You'll notice here unlike the last test I'm not taking a look at that mock functions arguments.
You can certainly do so if you wanted to.
I would once again also really recommend it but in this case I'm just gonna make sure that we do publish
and events.
Not really checking what event writing like that but at least we know a event is being published so
probably good enough right now.
I'm gonna say this.
Let's take a look at our tests and just see how we're doing.
Looks like it's all working as expected as usual just a very quick comment out make sure that thing
fails is always worth the time to do that.
Yup definitely fails and so I'd say we're good to go Yes that is it for the order cancelled.
Listener pause right here as usual.
Move on in just a moment.