1 00:00:01,800 --> 00:00:04,230 Let's write out some tests around this listener we just made. 2 00:00:05,130 --> 00:00:07,950 So inside of my events directory I'll find the listener's folder. 3 00:00:07,950 --> 00:00:09,560 I'll make a new directory inside there. 4 00:00:09,810 --> 00:00:15,550 Of underscore underscored test underscore underscore then inside that folder I'll make a new file of 5 00:00:15,580 --> 00:00:18,930 order created listener test. 6 00:00:19,130 --> 00:00:26,020 Yes and you can see the entire file name right there at the top of my screen so inside this test file. 7 00:00:26,050 --> 00:00:30,160 We're gonna go through with just about the identical series of steps to how we tested our other two 8 00:00:30,160 --> 00:00:32,370 listeners back inside the order service. 9 00:00:32,380 --> 00:00:36,880 We're gonna once again read a setup function inside there we're going to create an instance of our listener. 10 00:00:37,000 --> 00:00:42,290 We'll do a fake data object a fake message object and then return all that stuff. 11 00:00:42,340 --> 00:00:47,350 In this case we are also operating on presumably some existing ticket so we might also need to create 12 00:00:47,380 --> 00:00:53,230 a ticket and save it to our collection inside the setup function as well let's first define the setup 13 00:00:53,230 --> 00:00:59,360 function and it will add in imports along the way it's inside of you. 14 00:00:59,360 --> 00:01:04,100 The first thing to do is create an instance of the listener. 15 00:01:04,180 --> 00:01:08,380 That means we need to import the listener at the top along with the Nats wrapper class with the Nats 16 00:01:08,380 --> 00:01:09,900 wrapper Singleton. 17 00:01:10,000 --> 00:01:17,200 At the very top let's get our order read listener from up one directory order creating listener. 18 00:01:17,260 --> 00:01:21,750 I will also get my Nats wrapper from up 3 directories. 19 00:01:21,760 --> 00:01:30,860 That's where in the last video I said three directories Nats wrapper I'll then make my listener as a 20 00:01:30,860 --> 00:01:41,660 new order created listener and pass in Nats wrapper dot client after that we're going to create and 21 00:01:41,660 --> 00:01:43,550 save a ticket. 22 00:01:43,580 --> 00:01:48,680 So this is going to be the ticket that we are trying to reserve in order to create and save a ticket. 23 00:01:48,680 --> 00:01:57,860 We need the ticket model once again at the top we will import tickets from up 3 directories models ticket 24 00:01:59,830 --> 00:02:04,900 will then make it ticket using ticket up built whenever we create a ticket. 25 00:02:04,900 --> 00:02:12,870 We have to provide a title a price and a user I.D. as usual I'll just throw in a title of concert price 26 00:02:12,870 --> 00:02:19,630 of a 9 and a user I.D. of whatever it does really matter because again we're not going to really use 27 00:02:19,630 --> 00:02:25,030 this user I.D. so we could randomly generate one using the Mongoose library or that object I.D. constructor 28 00:02:25,300 --> 00:02:31,130 or alternatively we could just throw in some arbitrary value will then save this within a weight ticket 29 00:02:31,130 --> 00:02:34,090 dot save. 30 00:02:34,150 --> 00:02:39,720 After that we need to create the fake data event or the fake data object. 31 00:02:39,820 --> 00:02:44,980 So this is going to be a data object that's going to satisfy the data property of our order created 32 00:02:45,040 --> 00:02:50,750 interface or the order created event interface to help us put this data object together. 33 00:02:50,770 --> 00:02:56,600 We're going to import that interface at the top of the file once again from our common module at the 34 00:02:56,600 --> 00:02:58,000 very top. 35 00:02:58,060 --> 00:03:07,150 We will get our order created events from our communal module and then we can use that thing to help 36 00:03:07,150 --> 00:03:09,320 us build out this fake data object. 37 00:03:09,350 --> 00:03:18,000 So say data is going to be up type order created event at data again since this is going to be a really 38 00:03:18,000 --> 00:03:22,200 big object a lot of different properties here we can use typescript to just help us along the way and 39 00:03:22,200 --> 00:03:28,220 remember what properties this thing requires so in total we've got well everything you see listed here 40 00:03:28,790 --> 00:03:34,270 just to help myself out I'm going to copy as much as I can out of this little note right here. 41 00:03:37,260 --> 00:03:40,880 And then paste it in I am going to have to change a lot of stuff inside of here. 42 00:03:40,890 --> 00:03:45,220 I'm really just copying it so I can easily reference all these different properties we have to define. 43 00:03:45,260 --> 00:03:47,820 And then finally on the ticket as well I missed one line in there. 44 00:03:47,940 --> 00:03:51,120 In addition to the ticket idea we also have to reflect the ticket price 45 00:03:53,800 --> 00:03:55,380 these are all the properties we have to define. 46 00:03:55,380 --> 00:04:01,910 So now going to go through replace all the types and the semicolons with an actual value in a comma. 47 00:04:01,920 --> 00:04:08,310 So first off for I.D. right here we can once again either generate a random idea using Mongoose or put 48 00:04:08,310 --> 00:04:09,300 in a fake I.D.. 49 00:04:09,390 --> 00:04:13,950 In this case let's actually generate a I.D. with Mongoose because this I.D. actually does have a pretty 50 00:04:13,950 --> 00:04:15,360 good amount of meaning to us. 51 00:04:15,450 --> 00:04:20,040 It would be nice to use some realistic looking data though to generate a fake I.D. at the top. 52 00:04:20,070 --> 00:04:32,480 Let's import Mongoose from Mongoose and then to generate the I.D. Mongoose types object i.e. to hex 53 00:04:32,570 --> 00:04:35,750 string. 54 00:04:35,870 --> 00:04:37,060 Next up our version. 55 00:04:37,070 --> 00:04:40,910 So the thing presumably it was just created the order I mean. 56 00:04:40,910 --> 00:04:45,550 So we should probably say that this starts off with a version number of 0. 57 00:04:45,580 --> 00:04:46,750 Next up our status. 58 00:04:46,750 --> 00:04:52,330 Whenever an order first gets created it's going to have an order status equal to I believe was created. 59 00:04:52,330 --> 00:04:54,370 I want to say that was our order status. 60 00:04:54,370 --> 00:04:59,170 Either way remember we defined all of our possible order statuses inside of an enum inside of our common 61 00:04:59,170 --> 00:04:59,620 module. 62 00:05:00,100 --> 00:05:01,860 Let's import that you know at the very top. 63 00:05:01,960 --> 00:05:06,320 And we can use that to assign actual value to this thing from our common module. 64 00:05:06,320 --> 00:05:08,540 I'll get my order status NUM 65 00:05:12,360 --> 00:05:16,250 and then for status right here we'll replace that with order status dots. 66 00:05:16,360 --> 00:05:17,650 And it was created. 67 00:05:17,650 --> 00:05:22,510 That was the name of it then our user I.D. again doesn't really matter. 68 00:05:22,580 --> 00:05:27,230 But in a fake string here expires that this is our timestamp of when the thing expires. 69 00:05:27,230 --> 00:05:33,400 Not using the timestamp whatsoever here but in a fake value and now on to something actually matters 70 00:05:33,820 --> 00:05:34,890 the ticket itself. 71 00:05:35,310 --> 00:05:39,730 So the idea that ticket that we're trying to reserve is gonna be the ticket or the idea the ticket that 72 00:05:39,730 --> 00:05:41,800 we just built and saved on the step before. 73 00:05:42,610 --> 00:05:47,980 So for the idea of this ticket we will say ticket dot I.D. and then its price will be equal to the things 74 00:05:47,980 --> 00:05:53,140 price as well though ticket price. 75 00:05:53,160 --> 00:05:53,740 All right. 76 00:05:53,880 --> 00:05:56,040 There's our fake data object. 77 00:05:56,140 --> 00:05:59,020 Next up our fake message objects. 78 00:05:59,020 --> 00:06:02,510 We've gone through this several times so we can put it together rather easily. 79 00:06:02,560 --> 00:06:07,310 We're going to say that we're gonna have a message object that has a type message it's going to have 80 00:06:07,310 --> 00:06:12,310 an act function which is actually gonna be a just mock function now we've got an error here because 81 00:06:12,310 --> 00:06:15,160 we have not imported the message type from the note that streaming library. 82 00:06:15,190 --> 00:06:19,520 Let's do that at the top right away. 83 00:06:19,670 --> 00:06:25,600 Note that streaming once you've got that import typescript will say fantastic. 84 00:06:25,600 --> 00:06:29,090 This thing is defined but you did not define message object correctly. 85 00:06:29,110 --> 00:06:31,700 So again we'll say touch script just don't sweat it. 86 00:06:31,720 --> 00:06:34,870 We're kind of cheating here. 87 00:06:34,930 --> 00:06:35,120 All right. 88 00:06:35,120 --> 00:06:36,050 That is it for the setup. 89 00:06:36,050 --> 00:06:39,810 So now we're going to take all the stuff we just created and return it so we can actually use all the 90 00:06:39,810 --> 00:06:41,660 stuff inside of our different tests. 91 00:06:41,660 --> 00:06:50,930 So we're going to return in total listener ticket data and message the return listener ticket data message 92 00:06:51,260 --> 00:06:54,370 and that's it. 93 00:06:54,420 --> 00:06:57,450 Now we can start to write out our test but first this is a long video. 94 00:06:57,450 --> 00:06:59,900 So let's take a pause right here and continue in just a moment.