Let's get started with our test implementation.
So step one put together these setup function.
We're gonna have to create an instance of the listener at the very top.
So let's import our ticket created listener from of one directory to create a listener then right after
that comment we'll say listener is a new ticket created listener.
Whenever we create an instance of a listener we are required to pass in a Nats client.
Now you might recall a little bit ago we had set up the MOX folder inside the SRT directory so we've
got a mock around Nats wrapper right here so this right here is our mock.
It is only used inside the test environment and this is essentially our fake client.
So we're going to provide this to our instance of ticket created listener going to add in another import
statement up here for NATS rapper from up to directories or maybe three directories.
Yep three directories and then we'll pass and that's wrapper dot client.
Looks good.
Next up we're going to create a fake data event or really data object.
So this needs to be something that satisfies this data argument.
That's the first argument to be on message function inside of a ticket created listener.
So in other words the fake data object needs to satisfy the interface ticket created event or more specifically
the data piece of that interface so we can either look up that interface definition by doing a command
click or there's a little trick we could use to help ourselves out.
Back inside my test file at the very top.
I'm going to import that ticket created event run my company module and then we're we're going to create
the fake data event.
I'll say cons data and then I'm going to annotate this variable right away as tickets redid event specifically
the data property if I then set that equal to an empty object I can then hover over data and touch script
is gonna give us some hints on how we could satisfy that interface.
So we need to specifically provide an idea as a string version as a number title price user I.D. and
so on so we'll fill in some of those properties right here.
We're going to make up some properties on the fly more or less.
First I'll give it a version of zero.
I'll give it an idea and the idea we provide here does have to be a real I.D. is we're going to eventually
take this idea and use it to save a new ticket to our local database.
So it has to be a legitimate Mongo DV I.D. That means you're going to have to generate an I.D..
So at the very top I am going to import Mongoose from Mongoose and I'll generate the I.D. by putting
in new Mongoose types dot object I.D. to X string like so
then our title put in concert or price.
Sure 10 and then a user I.D. and we're not really using the user I.D. anywhere.
Nonetheless let's provide a really user I.D. as well.
So a real Mongo I.D..
So I'm going to copy that object I.D. and throw it down assign it to user I.D. now will notice that
touch script is happy.
We have created a valid looking data object.
Next up we need to create a fake message object to create this fake message object.
We're going to use a similar trick where we're going to create a variable called message and we'll annotate
its type and then typescript will jump in and help us create the proper looking objects.
So I'll say const MSE is UB type message and I'm going to import the message type at the top of the
file as well from the no net streaming library.
At the very top message from node that's streaming
once again typescript is going to give us an error right here and tell us that there's a bunch of different
properties we need to define.
Now in the context of our listener or specifically the on message function we do not care about the
vast majority of these different functions.
And I don't really want out to provide a fake implementation for each of them just to get typescript
to be happy with the code we're writing here.
So we're going to just do a little cheat once again we're going to use that to yes ignore helper or
the little comment to tell typescript Hey don't sweat it just pretend that this thing is a message even
if we do not implement the message correctly.
Now that error goes away and if I hover over message typescript says okay I give up your right whatever
it's a message in total there's really only one function on this message object we care about and if
we go back over to the listener we can see that it is the act function.
That's the only thing we really care about existing on the message at all so we're going to implement
a fake ACH function.
We're gonna make it a just mock.
So remember a mock function is essentially a function that's going to keep track of how many times it
gets called and what arguments it has provided.
And it's really useful for whenever you are testing something just to make sure that it function actually
gets invoked inside the message object.
We will assign back as just dot F in now as far as typescript is concerned.
This is a real message object.
It has the one function that we really care about and when we pass this into our on message function
inside of our listener whenever we call act we're going to actually invoke that mock function.
And since it is a mock function it's going to be really easy for us to test it and make sure that it
got called at least once OK.
So that's pretty much it for our setup function.
Now last thing we're going to do is take the listener take data and take message and we will return
all them at the bottom.
So inside of an object I'm going to return listener data and NSG
so we can move down to our actual tests.
We will first call the set function at the very top and get the listener that we have already created
data and MSE and that will all come from a weight setup.
Now again we are not actually doing anything async in nature inside of setup but we will eventually
not actually inside this test file but in another one we will say I'm going to throw on this the async
keyword right away even though again not really anything async inside of here.
No big deal case.
Now we can just follow these comments very easily.
All we have to do is call the on message function on the listener.
And when we do so we're going to pass Senate data and message will then write out an assertion to make
sure that a ticket was actually created so I will do and a wait listener on message and I'll pass in
data and message.
Like so now in theory if we go back to our listener we can really imagine what's gonna go on behind
the scenes this on message function is going to take the idea off that data object the title in the
price.
Use that all to build up a new ticket save it to the database and then act the message.
So if you want to now write out an assertion we should be able to write out a query that's going to
find some ticket inside the database with the idea that we had provided and then we could simply write
out some assertion and say hey let's make sure this ticket actually exists back inside of our test file
in order to run a query against the ticket collection.
We need to import our model file.
So at the very top we will import ticket from up three directories models ticket
and then at the bottom of our test we'll say ticket is a weight ticket fined by I.T. we're gonna try
to find a ticket with the same I.D. that we had randomly generated and stuck into that data object right
there.
So put in data.
And then finally we can expect ticket to be defined maybe I will also expect the tickets titled to equal
the data objects title.
Just make sure this thing actually uses the appropriate properties and let's make sure the price gets
carried over as well data dot price as usual typescript is not sure if we actually found a record here.
As usual exclamation exclamation.
If that fails right there.
That's fantastic.
We kind of want that to fail.
We want to see something say hey ticket isn't defined it's actually equal to no so let's say this and
try running these tests back in my terminal I got to change into the orders directory not just you know
we do actually have some other broken tests inside the orders project right now.
Turns out that a while ago we had added in something to allow us to pass in an I.D. and we never actually
updated any of our tests to accommodate for that.
So we're probably gonna end up seeing some kind of error here.
Totally fine but nonetheless let's just run our tests and at least make sure that the tests or the listener
are passing even if some other ones are failing.
All right.
So let's see what happens here.
Looks like we definitely have some failing.
We will fix those very shortly.
But most importantly it looks like the listener test is working as expected.
Just to make sure I'll come back over and I'll try replacing about the data title here with some gibberish
just to make sure that this test will fail and if I rerun that yep looks like that one will definitely
fail.
Well it's Alex Good.
So that's our first working test.
Let's take a quick pause right here we'll come back the next video.
We're going to fix up this act test down here or put it implementation together and then we will also
take care of some of these failing tests as well.