As I mentioned at the end the last video we're going to write out a very quick test around the order
created listener.
Again I am sure at this point that you might be a bit tired of testing but it still is just such a valuable
thing to have.
We can make sure this code works without having to actually manually test it.
So if you are tired of testing don't sweat it.
We're not going to really go over any new techniques or anything like that inside this video.
So you can skip over this one if you want to.
Otherwise stick around and we'll put the test together for this very very quickly so to get started
inside of listeners will make a new folder of underscore underscore test underscore underscore and then
inside that I'll make a new file of order created listener test dot t yes
then inside of here.
As usual let's build our setup function.
The first thing I do is well build a listener.
For that we're going to need the Nats streaming server client from our Nats wrapper file so we know
those are at least two imports we can do right away.
So I will get Nats rapper from up 3 directories
and I will get my order created.
Listener from up one directory order create the listener
will then make my order it created listener and feed in the Nats wrappers.
Client after that we're going to create a order from scratch or I should say the data order event that
we need to get our type for the order created event from our coming module.
So at the very top we'll get our order created event from our common module so say that we're going
to have some data.
This will be of type order created event at data and then inside of here we definitely need to list
out a realistic ideas property for the order itself while the other properties we could probably make
up but at least the idea has to be realistic looking and as usual for that we will directly import Mongoose
from mongoose.
And the idea the order will be Mongoose types object idea that 2 hex string
okay.
So what else do we need here.
Well version status user I.D. expires at the open in a version of 0 expires at.
We're not releasing that so we get put in just whatever string a user I.D..
We're also not releasing that sold through in whatever string.
What else do we need here.
A status and then ticket stuff so for these status we do need the order status you know from our common
module so we will add in an import for order status and we can list out a realistic looking status right
here.
Of read it and then for a ticket listing we'll have an I.D. again.
Doesn't matter what the ideas in these have price as well which I will simply make 10 OK so there's
our data object as usual.
Next we'll make is our message object that is going to have the marked ACH function.
So at the very top I will as usual import the message type from node that streaming
will then tell typescript to ignore what we're about to type right here because no we are kind of cheating.
Well really cheating.
We'll say this thing has a ACH function.
They'll be just append.
I think that is it for a setup.
So now going to return all this stuff.
So at the very bottom a setup I will return listener data and message and we could start to write out
actual tests we'll say it replicates the order info
and we'll also make another test something like it acts the message.
In both cases we're going to call the setup function and get back our listener data and message from
a weight setup and then do an awaits listener dot on message and pass in the data and the message.
And these first two lines right here we need inside both tests.
So I'm going to copy paste those two lines down into the second test as well.
OK.
So we can actually do an expectation well in the case the first one we can try to find in order instead
of our orders collection and just make sure that it has maybe the correct price or something like that
so we can do that really quickly.
We could say order will be a wait order find by I.T. we're gonna try to find an order that was saved
with data I.D. We're making use of the order model right here so let's make sure we add an import for
that at the top to file so at the very top I will import order from up 3 directories models order
once we fetch the order we can make sure that it just has the correct price something very simple and
straightforward.
So say order exclamation don't forget that price match should equal data dot ticket price and then back
in the message we can do a message dot EQ or we expect message dot ACH to have been built there we go
OK.
So that should be it for our two tests.
Let's say this file will then go back over to our terminal and run them very quickly I'll change into
my immense directory and do an NPM run test
and looks good as usual.
Let's comment out the two on message lines.
So 1 and to save it look back over make sure it fails.
Yep two failures.
So I think we are looking good
all right.
Well like I said a test that looks very much all the ones we put together but at least now we are pretty
sure that this thing works as expected.
So one more pause.
Come back the next video we're gonna start to work on another listener for order canceled.