We've now got this import redirecting thing or file mocking ads it's referred to with just working.
Now we need to figure out how to write a fake implementation.
Let's take a look at a couple diagrams and help you understand what I really mean by that.
So to write out a fake implementation we really have to take a very deep look at our Nat's wrapper file
the one inside of our SRT directory and we need to understand what this class right here does.
We need to understand what parts of it are used by other files that we are trying to test and then we're
going to provide some fake implementation of just the parts that are expected to be implemented implemented
by those other files.
Again let me just show you a couple of diagrams so we have our new ticket root handler.
That is what we're currently trying to test the new ticket root handler is going to import the Nats
wrapper file.
Now when it imports the real Nats wrapper file it gets an object that looks like this an object that
has an underscore client property that is of type Stan or member Nats client and has a client property
that gives us a Stan or Nats client and a connect function as well.
How do I know that is the object.
Well we can just take a look at the real Nats wrapper file directly.
Here's the real Nats wrapper file inside of here we create a class as you will recall at the very bottom
we create an instance of that class and then we export it as a named export of Nats wrapper.
So what we eventually get inside of our new root handler is an instance of Nats wrapper and an instance
of rats Nats wrapper has underscored clients as the get client and the Connect function doing you now
need to now take a look at how the new ticket root handler actually uses the properties inside of this
object while the new ticket root handler does not care about the underscore client property as a matter
of fact that client property.
The underscore client property is marked as private.
We can not access that thing at all inside of our new ticket root handler but means we really don't
care about it at all.
The new ticket root handler also does not care about the Connect function.
The Connect function only gets in boats from the index not just file when our application is first starting
up when we are trying to actually run it for real.
Not in a test environment and so that leaves just the client property.
This is the only property that our new ticket root handler cares about and that means that this is the
only property that we need to define inside of our little fake implementation let's go into our fake
implementation file right now and we're going to just define this client property.
We won't worry about assigning it a value just yet it's going to find my underscore underscore mocks.
Here's my fake implementation file or the mock file inside this object I'm going to define that client
property so we're gonna come back and provide a value for it in just a moment but before we do we're
going to go through a very similar process where we figure out where this client property is actually
used and we're then going to figure out how to fake that things implementation so we know that this
client property is taken and provided to the ticket created publisher.
So we need to think about how the ticket created publisher actually uses that client.
So let's go and take a look at the ticket created publisher implementation.
We can find that inside of our events publishers ticket created publisher file but here's the publisher.
You'll recall that this thing extends our base publisher class and so the ticket create a publisher
doesn't really do anything with the client directly.
Instead if we want to figure out how that client gets used we need to go and take a little peek at our
base publisher class you'll recall that we implemented that inside of our common module.
Let's go back over to our common module very quickly and take a look at what that publisher really does
with the client.
All right here's my coming module inside there's the SRT directory.
Inside there is events and inside of that is base publisher T.S. but here's the base publisher based
publisher expects to be provided that client property and then internally the only thing it actually
does with it is call the clients publish function when it calls the publish function.
It's going to provide.
We are going to provide three arguments to it.
This subject some string of five data and a callback function then the expectation is that this callback
function is going to eventually be invoked by the client.
That is what the real client does.
The real client is going to eventually call the callback function to indicate that the publish is complete.
So with that in mind a couple more diagrams.
Sorry I should've showed you this one just a moment ago.
Purpose of this one was to just say we have our new ticket root handler it imports the Nats wrapper
and the only thing that it cares about inside there is the client and the client gets passed off to
the publisher.
We just discussed that it's now inside of our base publisher.
This thing takes the client it has a published function the base publisher has a published function
and the base publisher is going to try to invoke this client publish and it's going to call it with
some subject data and a callback.
And the expectation is that that callback is going to be invoked after the event is successfully published.
So for us to continue to implement this kind of fake or mock class we need to make sure that let's go
back to our mock file really quickly.
We may need to make sure that this client thing right here is an object that has a Polish function the
published function that needs to take in a subject that is a string data that is a string and a callback
function.
And as soon as we receive that callback function we should invoke it right away.
So we're gonna do that right now.
We're gonna find client right here.
This needs to be an object.
It needs to have a public function to define this as a ERO function like so and this public function
needs to take in a subject that is a string data that is a string and a callback function that is a
function
and then the expectation is that inside this Polish function we are going to call callback right away.
And that is it if we run this code right now our test for the new root handler should pass.
But before we do just want to make sure it's super crystal clear what is going on here because this
is some confusing stuff.
They're just one more time to run through the entire setup.
We have built out this mock file when our tests are executed in the test environment our new root handler.
Here's the new handler is going to import that fake or the mock Nats wrapper file out of that file.
It's going to look for a named export of Nat's wrapper.
So the new root handler is going to get this whole object right here then inside the new root handler.
We're going to take that Nats wrapper object.
We're gonna pull off the client property and we're going to provide it to the get created publisher.
So in other words the ticket created publisher is going to receive just this object right here.
That is the fake client.
We just put together so that little object just this object with the publish function is going to show
up inside of our base publisher as client the base publisher is gonna take that client assign it to
the best client property and then at some point time our handler is going to call this publish function
right here to try to publish some event internally.
This thing is going to create a new promise and return it.
And inside that promise it's going to access this client not publish this client not publish.
That is the basic function we just put together right here.
That is what is actually going to get executed
so it's gonna call this duck client publish provide a subject provide the data and this callback function
and then we want to make sure that the callback function gets executed right away.
So the overall promise gets result and that's why inside of our fake implementation right here we receive
the subject the data and the callback function and then inside all we did was all the callback.
As soon as we possibly could.
So let's save all this.
We're gonna go back over to our terminal an LLC that our new test out handler as all of its test passing
looks pretty great if we try to run all of our tests now.
So if I do a W to see the watch menu I can then press a on my keyboard to run all tests.
They'll do that and we are going to see still a lot of tests failing.
There we go.
So still a lot of tests failing.
The reason for that is that we now have you might recall inside of our new test file we have told the
new test file or just to try to block that file.
But we have other test files that do not have the same mock statement.
So we have to go round to our other test files and tell all of our other tests.
Hey if you ever tried to import the Nats wrapper file instead do a redirect.
Get this other fake file.
Let's take a pause right here and copy paste thing around in just a moment.