Let's write out some tests around this listener we just made.
So inside of my events directory I'll find the listener's folder.
I'll make a new directory inside there.
Of underscore underscored test underscore underscore then inside that folder I'll make a new file of
order created listener test.
Yes and you can see the entire file name right there at the top of my screen so inside this test file.
We're gonna go through with just about the identical series of steps to how we tested our other two
listeners back inside the order service.
We're gonna once again read a setup function inside there we're going to create an instance of our listener.
We'll do a fake data object a fake message object and then return all that stuff.
In this case we are also operating on presumably some existing ticket so we might also need to create
a ticket and save it to our collection inside the setup function as well let's first define the setup
function and it will add in imports along the way it's inside of you.
The first thing to do is create an instance of the listener.
That means we need to import the listener at the top along with the Nats wrapper class with the Nats
wrapper Singleton.
At the very top let's get our order read listener from up one directory order creating listener.
I will also get my Nats wrapper from up 3 directories.
That's where in the last video I said three directories Nats wrapper I'll then make my listener as a
new order created listener and pass in Nats wrapper dot client after that we're going to create and
save a ticket.
So this is going to be the ticket that we are trying to reserve in order to create and save a ticket.
We need the ticket model once again at the top we will import tickets from up 3 directories models ticket
will then make it ticket using ticket up built whenever we create a ticket.
We have to provide a title a price and a user I.D. as usual I'll just throw in a title of concert price
of a 9 and a user I.D. of whatever it does really matter because again we're not going to really use
this user I.D. so we could randomly generate one using the Mongoose library or that object I.D. constructor
or alternatively we could just throw in some arbitrary value will then save this within a weight ticket
dot save.
After that we need to create the fake data event or the fake data object.
So this is going to be a data object that's going to satisfy the data property of our order created
interface or the order created event interface to help us put this data object together.
We're going to import that interface at the top of the file once again from our common module at the
very top.
We will get our order created events from our communal module and then we can use that thing to help
us build out this fake data object.
So say data is going to be up type order created event at data again since this is going to be a really
big object a lot of different properties here we can use typescript to just help us along the way and
remember what properties this thing requires so in total we've got well everything you see listed here
just to help myself out I'm going to copy as much as I can out of this little note right here.
And then paste it in I am going to have to change a lot of stuff inside of here.
I'm really just copying it so I can easily reference all these different properties we have to define.
And then finally on the ticket as well I missed one line in there.
In addition to the ticket idea we also have to reflect the ticket price
these are all the properties we have to define.
So now going to go through replace all the types and the semicolons with an actual value in a comma.
So first off for I.D. right here we can once again either generate a random idea using Mongoose or put
in a fake I.D..
In this case let's actually generate a I.D. with Mongoose because this I.D. actually does have a pretty
good amount of meaning to us.
It would be nice to use some realistic looking data though to generate a fake I.D. at the top.
Let's import Mongoose from Mongoose and then to generate the I.D. Mongoose types object i.e. to hex
string.
Next up our version.
So the thing presumably it was just created the order I mean.
So we should probably say that this starts off with a version number of 0.
Next up our status.
Whenever an order first gets created it's going to have an order status equal to I believe was created.
I want to say that was our order status.
Either way remember we defined all of our possible order statuses inside of an enum inside of our common
module.
Let's import that you know at the very top.
And we can use that to assign actual value to this thing from our common module.
I'll get my order status NUM
and then for status right here we'll replace that with order status dots.
And it was created.
That was the name of it then our user I.D. again doesn't really matter.
But in a fake string here expires that this is our timestamp of when the thing expires.
Not using the timestamp whatsoever here but in a fake value and now on to something actually matters
the ticket itself.
So the idea that ticket that we're trying to reserve is gonna be the ticket or the idea the ticket that
we just built and saved on the step before.
So for the idea of this ticket we will say ticket dot I.D. and then its price will be equal to the things
price as well though ticket price.
All right.
There's our fake data object.
Next up our fake message objects.
We've gone through this several times so we can put it together rather easily.
We're going to say that we're gonna have a message object that has a type message it's going to have
an act function which is actually gonna be a just mock function now we've got an error here because
we have not imported the message type from the note that streaming library.
Let's do that at the top right away.
Note that streaming once you've got that import typescript will say fantastic.
This thing is defined but you did not define message object correctly.
So again we'll say touch script just don't sweat it.
We're kind of cheating here.
All right.
That is it for the setup.
So now we're going to take all the stuff we just created and return it so we can actually use all the
stuff inside of our different tests.
So we're going to return in total listener ticket data and message the return listener ticket data message
and that's it.
Now we can start to write out our test but first this is a long video.
So let's take a pause right here and continue in just a moment.