1 00:00:00,930 --> 00:00:03,090 Let's get started with our test implementation. 2 00:00:03,190 --> 00:00:06,030 So step one put together these setup function. 3 00:00:06,120 --> 00:00:08,620 We're gonna have to create an instance of the listener at the very top. 4 00:00:08,670 --> 00:00:18,120 So let's import our ticket created listener from of one directory to create a listener then right after 5 00:00:18,120 --> 00:00:24,490 that comment we'll say listener is a new ticket created listener. 6 00:00:24,490 --> 00:00:28,640 Whenever we create an instance of a listener we are required to pass in a Nats client. 7 00:00:28,660 --> 00:00:34,330 Now you might recall a little bit ago we had set up the MOX folder inside the SRT directory so we've 8 00:00:34,330 --> 00:00:38,950 got a mock around Nats wrapper right here so this right here is our mock. 9 00:00:38,950 --> 00:00:43,360 It is only used inside the test environment and this is essentially our fake client. 10 00:00:43,360 --> 00:00:50,170 So we're going to provide this to our instance of ticket created listener going to add in another import 11 00:00:50,170 --> 00:00:56,350 statement up here for NATS rapper from up to directories or maybe three directories. 12 00:00:56,410 --> 00:01:03,310 Yep three directories and then we'll pass and that's wrapper dot client. 13 00:01:03,400 --> 00:01:04,430 Looks good. 14 00:01:04,430 --> 00:01:09,060 Next up we're going to create a fake data event or really data object. 15 00:01:09,230 --> 00:01:12,330 So this needs to be something that satisfies this data argument. 16 00:01:12,380 --> 00:01:16,400 That's the first argument to be on message function inside of a ticket created listener. 17 00:01:16,460 --> 00:01:23,090 So in other words the fake data object needs to satisfy the interface ticket created event or more specifically 18 00:01:23,120 --> 00:01:29,330 the data piece of that interface so we can either look up that interface definition by doing a command 19 00:01:29,360 --> 00:01:33,550 click or there's a little trick we could use to help ourselves out. 20 00:01:33,680 --> 00:01:35,650 Back inside my test file at the very top. 21 00:01:35,690 --> 00:01:44,250 I'm going to import that ticket created event run my company module and then we're we're going to create 22 00:01:44,250 --> 00:01:45,320 the fake data event. 23 00:01:45,330 --> 00:01:52,680 I'll say cons data and then I'm going to annotate this variable right away as tickets redid event specifically 24 00:01:52,680 --> 00:01:59,010 the data property if I then set that equal to an empty object I can then hover over data and touch script 25 00:01:59,010 --> 00:02:01,980 is gonna give us some hints on how we could satisfy that interface. 26 00:02:02,550 --> 00:02:08,250 So we need to specifically provide an idea as a string version as a number title price user I.D. and 27 00:02:08,250 --> 00:02:13,410 so on so we'll fill in some of those properties right here. 28 00:02:13,420 --> 00:02:17,210 We're going to make up some properties on the fly more or less. 29 00:02:17,290 --> 00:02:19,420 First I'll give it a version of zero. 30 00:02:19,630 --> 00:02:25,030 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 31 00:02:25,060 --> 00:02:29,080 take this idea and use it to save a new ticket to our local database. 32 00:02:29,080 --> 00:02:34,760 So it has to be a legitimate Mongo DV I.D. That means you're going to have to generate an I.D.. 33 00:02:34,840 --> 00:02:41,580 So at the very top I am going to import Mongoose from Mongoose and I'll generate the I.D. by putting 34 00:02:41,580 --> 00:02:49,200 in new Mongoose types dot object I.D. to X string like so 35 00:02:52,820 --> 00:02:56,540 then our title put in concert or price. 36 00:02:56,540 --> 00:03:01,270 Sure 10 and then a user I.D. and we're not really using the user I.D. anywhere. 37 00:03:01,490 --> 00:03:04,420 Nonetheless let's provide a really user I.D. as well. 38 00:03:04,430 --> 00:03:05,800 So a real Mongo I.D.. 39 00:03:06,190 --> 00:03:13,620 So I'm going to copy that object I.D. and throw it down assign it to user I.D. now will notice that 40 00:03:13,800 --> 00:03:14,720 touch script is happy. 41 00:03:14,730 --> 00:03:17,910 We have created a valid looking data object. 42 00:03:19,020 --> 00:03:23,220 Next up we need to create a fake message object to create this fake message object. 43 00:03:23,220 --> 00:03:27,060 We're going to use a similar trick where we're going to create a variable called message and we'll annotate 44 00:03:27,060 --> 00:03:32,120 its type and then typescript will jump in and help us create the proper looking objects. 45 00:03:32,160 --> 00:03:38,990 So I'll say const MSE is UB type message and I'm going to import the message type at the top of the 46 00:03:38,990 --> 00:03:42,080 file as well from the no net streaming library. 47 00:03:42,360 --> 00:03:47,650 At the very top message from node that's streaming 48 00:03:50,740 --> 00:03:54,550 once again typescript is going to give us an error right here and tell us that there's a bunch of different 49 00:03:54,550 --> 00:03:56,470 properties we need to define. 50 00:03:56,470 --> 00:04:01,780 Now in the context of our listener or specifically the on message function we do not care about the 51 00:04:01,780 --> 00:04:04,480 vast majority of these different functions. 52 00:04:04,660 --> 00:04:09,010 And I don't really want out to provide a fake implementation for each of them just to get typescript 53 00:04:09,010 --> 00:04:11,740 to be happy with the code we're writing here. 54 00:04:11,740 --> 00:04:19,440 So we're going to just do a little cheat once again we're going to use that to yes ignore helper or 55 00:04:19,440 --> 00:04:24,240 the little comment to tell typescript Hey don't sweat it just pretend that this thing is a message even 56 00:04:24,240 --> 00:04:26,690 if we do not implement the message correctly. 57 00:04:26,790 --> 00:04:32,010 Now that error goes away and if I hover over message typescript says okay I give up your right whatever 58 00:04:32,010 --> 00:04:37,550 it's a message in total there's really only one function on this message object we care about and if 59 00:04:37,550 --> 00:04:40,790 we go back over to the listener we can see that it is the act function. 60 00:04:40,790 --> 00:04:46,360 That's the only thing we really care about existing on the message at all so we're going to implement 61 00:04:46,390 --> 00:04:48,070 a fake ACH function. 62 00:04:48,130 --> 00:04:49,800 We're gonna make it a just mock. 63 00:04:49,900 --> 00:04:54,040 So remember a mock function is essentially a function that's going to keep track of how many times it 64 00:04:54,040 --> 00:04:56,480 gets called and what arguments it has provided. 65 00:04:56,560 --> 00:05:00,070 And it's really useful for whenever you are testing something just to make sure that it function actually 66 00:05:00,070 --> 00:05:03,190 gets invoked inside the message object. 67 00:05:03,190 --> 00:05:10,390 We will assign back as just dot F in now as far as typescript is concerned. 68 00:05:10,390 --> 00:05:12,150 This is a real message object. 69 00:05:12,160 --> 00:05:17,110 It has the one function that we really care about and when we pass this into our on message function 70 00:05:17,140 --> 00:05:22,620 inside of our listener whenever we call act we're going to actually invoke that mock function. 71 00:05:22,620 --> 00:05:26,110 And since it is a mock function it's going to be really easy for us to test it and make sure that it 72 00:05:26,110 --> 00:05:29,200 got called at least once OK. 73 00:05:29,200 --> 00:05:31,120 So that's pretty much it for our setup function. 74 00:05:31,120 --> 00:05:37,800 Now last thing we're going to do is take the listener take data and take message and we will return 75 00:05:37,830 --> 00:05:39,380 all them at the bottom. 76 00:05:39,480 --> 00:05:45,360 So inside of an object I'm going to return listener data and NSG 77 00:05:48,610 --> 00:05:50,610 so we can move down to our actual tests. 78 00:05:50,740 --> 00:05:57,010 We will first call the set function at the very top and get the listener that we have already created 79 00:05:57,640 --> 00:06:02,000 data and MSE and that will all come from a weight setup. 80 00:06:02,020 --> 00:06:06,820 Now again we are not actually doing anything async in nature inside of setup but we will eventually 81 00:06:07,300 --> 00:06:11,500 not actually inside this test file but in another one we will say I'm going to throw on this the async 82 00:06:11,510 --> 00:06:14,710 keyword right away even though again not really anything async inside of here. 83 00:06:14,710 --> 00:06:17,480 No big deal case. 84 00:06:17,490 --> 00:06:19,400 Now we can just follow these comments very easily. 85 00:06:19,400 --> 00:06:22,220 All we have to do is call the on message function on the listener. 86 00:06:22,220 --> 00:06:26,690 And when we do so we're going to pass Senate data and message will then write out an assertion to make 87 00:06:26,690 --> 00:06:33,830 sure that a ticket was actually created so I will do and a wait listener on message and I'll pass in 88 00:06:33,890 --> 00:06:34,910 data and message. 89 00:06:34,910 --> 00:06:40,930 Like so now in theory if we go back to our listener we can really imagine what's gonna go on behind 90 00:06:40,930 --> 00:06:46,870 the scenes this on message function is going to take the idea off that data object the title in the 91 00:06:46,870 --> 00:06:47,610 price. 92 00:06:47,620 --> 00:06:52,350 Use that all to build up a new ticket save it to the database and then act the message. 93 00:06:52,390 --> 00:06:57,220 So if you want to now write out an assertion we should be able to write out a query that's going to 94 00:06:57,220 --> 00:07:02,740 find some ticket inside the database with the idea that we had provided and then we could simply write 95 00:07:02,740 --> 00:07:08,420 out some assertion and say hey let's make sure this ticket actually exists back inside of our test file 96 00:07:08,750 --> 00:07:11,300 in order to run a query against the ticket collection. 97 00:07:11,300 --> 00:07:13,240 We need to import our model file. 98 00:07:13,430 --> 00:07:21,170 So at the very top we will import ticket from up three directories models ticket 99 00:07:24,600 --> 00:07:33,020 and then at the bottom of our test we'll say ticket is a weight ticket fined by I.T. we're gonna try 100 00:07:33,020 --> 00:07:37,730 to find a ticket with the same I.D. that we had randomly generated and stuck into that data object right 101 00:07:37,730 --> 00:07:38,490 there. 102 00:07:38,510 --> 00:07:40,020 So put in data. 103 00:07:40,910 --> 00:07:51,010 And then finally we can expect ticket to be defined maybe I will also expect the tickets titled to equal 104 00:07:51,310 --> 00:07:52,840 the data objects title. 105 00:07:52,990 --> 00:07:57,970 Just make sure this thing actually uses the appropriate properties and let's make sure the price gets 106 00:07:57,970 --> 00:08:06,030 carried over as well data dot price as usual typescript is not sure if we actually found a record here. 107 00:08:06,400 --> 00:08:08,720 As usual exclamation exclamation. 108 00:08:08,770 --> 00:08:10,180 If that fails right there. 109 00:08:10,180 --> 00:08:10,930 That's fantastic. 110 00:08:10,930 --> 00:08:11,920 We kind of want that to fail. 111 00:08:11,920 --> 00:08:17,670 We want to see something say hey ticket isn't defined it's actually equal to no so let's say this and 112 00:08:17,670 --> 00:08:23,100 try running these tests back in my terminal I got to change into the orders directory not just you know 113 00:08:23,130 --> 00:08:26,890 we do actually have some other broken tests inside the orders project right now. 114 00:08:27,000 --> 00:08:32,310 Turns out that a while ago we had added in something to allow us to pass in an I.D. and we never actually 115 00:08:32,310 --> 00:08:34,860 updated any of our tests to accommodate for that. 116 00:08:34,860 --> 00:08:37,320 So we're probably gonna end up seeing some kind of error here. 117 00:08:37,320 --> 00:08:43,200 Totally fine but nonetheless let's just run our tests and at least make sure that the tests or the listener 118 00:08:43,200 --> 00:08:47,020 are passing even if some other ones are failing. 119 00:08:47,020 --> 00:08:47,410 All right. 120 00:08:47,440 --> 00:08:50,510 So let's see what happens here. 121 00:08:50,580 --> 00:08:51,920 Looks like we definitely have some failing. 122 00:08:51,930 --> 00:08:53,390 We will fix those very shortly. 123 00:08:53,490 --> 00:08:56,880 But most importantly it looks like the listener test is working as expected. 124 00:08:58,000 --> 00:09:04,490 Just to make sure I'll come back over and I'll try replacing about the data title here with some gibberish 125 00:09:04,820 --> 00:09:11,680 just to make sure that this test will fail and if I rerun that yep looks like that one will definitely 126 00:09:11,680 --> 00:09:12,870 fail. 127 00:09:12,890 --> 00:09:13,770 Well it's Alex Good. 128 00:09:13,790 --> 00:09:15,400 So that's our first working test. 129 00:09:15,400 --> 00:09:17,480 Let's take a quick pause right here we'll come back the next video. 130 00:09:17,560 --> 00:09:22,330 We're going to fix up this act test down here or put it implementation together and then we will also 131 00:09:22,330 --> 00:09:24,280 take care of some of these failing tests as well.