At the end of last video I mentioned going back and fixing a couple other tests in sidebar order service
rather than going off topic.
Let's just stick to what we were doing before.
Let's write out some tests are done.
Our ticket updated listener will write out some tests run that and then we'll get back to fixing some
tests inside of our routes directory specifically.
Those are the ones essentially that are failing right now.
All right.
So we're gonna write some tests out around the ticket update the listener.
I've got it open right here.
The tests we're gonna write out around this listener are gonna be very similar in nature to the ones
we already put together for to get created.
We will create an instance of this listener.
We will create a fake data object and a fake message will then call on message with those fake arguments
and the will of write out some assertions to make sure that this thing finds a ticket inside the ticket
collection makes a change to it and saves that update back to the tickets collection.
We'll also probably write out a separate test once again to make sure that this thing eventually calls
the act.
Function so let's get to it inside of our listeners test directory.
I'm gonna make a new file of ticket.
Updated listener test.
Yes I will once again create a setup function at the very top.
I'm gonna put some comments in the side of this thing just to guide myself.
I know we've gone through one set up function already this one's gonna be almost identical but there
will be one or two very small changes so I would like to just throw in a couple of very quick comments
so inside of setup we're gonna first create a listener this time around.
We're going to create and save a ticket into our tickets collection.
This is gonna be the ticket that we are trying to update will create and save a ticket.
Will then create a fake data object that describes them update that we're going to make to that ticket
we just saved we'll create a fake message object and then return all of this stuff.
Then what do we really want to test here.
Well let's write out to kind of stubs for tests.
We'll say it finds updates and saves a ticket.
So that's gonna be really our success case right there.
And we'll also throw in another test to year very similar to the last one as well something that's just
says it X the message
that we could certainly write out a couple of comments inside about these but I bet you can imagine
what's going to happen.
We're gonna call the setup function.
Get all this stuff kind of set up for us then call on message and then write out an expectation or to
vary so much that we just did a moment ago inside a ticket created All right.
So let's get to it.
First thing we have to do right here create a listener to create the listener.
We have to import our ticket.
Updated listener.
So the actual class.
We also need to import the Nats wrapper because remember to create an instance of a listener.
We have to provide a Nats client so at the very top I will add in an import for tickets.
Updated listener from up directory.
Ticket update listener
I will also get my Nats rapper
from 3 directories up Nats rapper then our listener will be a new ticket updated listener Nats rapper
dot client
after that we'll go ahead and create and save our ticket to do so we need access to our ticket model
I will make sure that I import my ticket model from up 3 directories as well models ticket when we build
a ticket we have to provide a I.D. for this ticket because remember the idea here is that we're sharing
the same I.D. for all these different tickets across our different services in order to provide some
I.D. or an existing idea we have to generate an idea on the fly and to generate an I.D. We're going
to use Mongoose member Mongoose as a reference to that object I.D. constructor that we can use to make
a new I.D. though I will also import mangoes from Mongoose so we can then create our ticket that would
be a ticket.
Build and then if we forget what property is we have to add as usual just mouse over.
We've got to provide that I.D..
That's what we just imported mongers.
We're going to generate that idea on the fly a title enterprise so our ideas will be mongers that types
object ideas that to hex string
then a title I'll use concert and a price of 20 that's going to create the ticket in memory it creates
just the document.
Now we have to actually take this ticket and save it to our tickets collection.
So I will do it in a wait ticket that save like some.
Next up our fake data object.
So this is going to be the thing that describes some update to our ticket if we need a little bit of
help on assembling this event or this data object.
We can always import the event object or the event type from our common module.
Let's do that at the very top.
I'm going to import the tickets updated event from my common module
will then use that to create our fake data objects so say data is of type ticket updated event I got
ticket updated.
Right.
Yes very good.
And specifically we want to create an object that mirrors the data property on that thing so that we
can mouse over data and that will give us some help on what we need to do here.
So we need the I.D. version titled price user I.D. Well the idea right here is going to be the same
I.D. of the as the ticket that we have created just a moment ago right here.
The variety that will be ticket dot I.D..
Next up a version to the version in this case it needs to be whatever this thing's version is plus one.
So put in a version of the ticket's version plus one.
Next up a title price and user I.D..
So these will be the things we actually change in some way.
So we probably want to make sure that we provide either a different title maybe a different price user
I.D. as usual.
We're not really making use of it inside this order service.
We could throw in some kind of a matching user I.D. It doesn't really matter but we should make sure
that we provide some different title in some different price because that's what we're going to eventually
write an assertion around.
We're gonna make sure that when we process this data object right here we end up with whatever this
thing's title and price are.
So I'll give this a title of new concerts.
So notice how it's different than the original title.
Not very different but different enough and a price of about nine 9 9.
I'll then throw in the user I.D. of whatever.
Again we're not really using that.
So not big difference right.
So I do not have an error around the data object anymore which means that typescript is happy with my
definition of that data object finally our fake message object.
So again we will annotate this as a message.
The only function we care about on this thing is the act function.
We're going to make it a just mock function.
Script is very unhappy with our definition of message right here.
We have not actually imported that let's import that the very top really quick
message from node and that's streaming.
That's better.
And now if we take a look.
Touch it's going to be really unhappy with our definition because we are not defining all these other
properties.
But again we just don't care about them or the purposes of this test.
So we can throw in a t yes ignore and say typescript just don't sweat it.
We know what we're doing here and finally we're going to return all the stuff that we just created.
It's all return inside an object the message the data object the ticket that we created and saved and
the listener as well.
The message data ticket listener Yeah that looks good let's take a quick pause right here and we will
implement both our tests in just a moment.