1 00:00:00,860 --> 00:00:05,570 At the end of last video I mentioned going back and fixing a couple other tests in sidebar order service 2 00:00:05,930 --> 00:00:07,360 rather than going off topic. 3 00:00:07,370 --> 00:00:09,370 Let's just stick to what we were doing before. 4 00:00:09,380 --> 00:00:10,710 Let's write out some tests are done. 5 00:00:10,730 --> 00:00:15,680 Our ticket updated listener will write out some tests run that and then we'll get back to fixing some 6 00:00:15,680 --> 00:00:18,320 tests inside of our routes directory specifically. 7 00:00:18,320 --> 00:00:21,540 Those are the ones essentially that are failing right now. 8 00:00:21,600 --> 00:00:21,850 All right. 9 00:00:21,870 --> 00:00:25,190 So we're gonna write some tests out around the ticket update the listener. 10 00:00:25,350 --> 00:00:27,090 I've got it open right here. 11 00:00:27,090 --> 00:00:30,690 The tests we're gonna write out around this listener are gonna be very similar in nature to the ones 12 00:00:30,690 --> 00:00:32,820 we already put together for to get created. 13 00:00:32,820 --> 00:00:34,710 We will create an instance of this listener. 14 00:00:34,710 --> 00:00:40,230 We will create a fake data object and a fake message will then call on message with those fake arguments 15 00:00:40,740 --> 00:00:44,760 and the will of write out some assertions to make sure that this thing finds a ticket inside the ticket 16 00:00:44,760 --> 00:00:50,080 collection makes a change to it and saves that update back to the tickets collection. 17 00:00:50,170 --> 00:00:54,060 We'll also probably write out a separate test once again to make sure that this thing eventually calls 18 00:00:54,060 --> 00:00:54,570 the act. 19 00:00:54,570 --> 00:00:59,890 Function so let's get to it inside of our listeners test directory. 20 00:00:59,910 --> 00:01:02,090 I'm gonna make a new file of ticket. 21 00:01:02,100 --> 00:01:04,880 Updated listener test. 22 00:01:04,930 --> 00:01:12,710 Yes I will once again create a setup function at the very top. 23 00:01:12,870 --> 00:01:15,740 I'm gonna put some comments in the side of this thing just to guide myself. 24 00:01:15,750 --> 00:01:19,470 I know we've gone through one set up function already this one's gonna be almost identical but there 25 00:01:19,470 --> 00:01:23,760 will be one or two very small changes so I would like to just throw in a couple of very quick comments 26 00:01:24,250 --> 00:01:29,360 so inside of setup we're gonna first create a listener this time around. 27 00:01:29,390 --> 00:01:33,110 We're going to create and save a ticket into our tickets collection. 28 00:01:33,110 --> 00:01:37,740 This is gonna be the ticket that we are trying to update will create and save a ticket. 29 00:01:38,610 --> 00:01:43,600 Will then create a fake data object that describes them update that we're going to make to that ticket 30 00:01:43,600 --> 00:01:50,430 we just saved we'll create a fake message object and then return all of this stuff. 31 00:01:52,680 --> 00:01:54,420 Then what do we really want to test here. 32 00:01:54,420 --> 00:01:57,430 Well let's write out to kind of stubs for tests. 33 00:01:57,440 --> 00:02:04,290 We'll say it finds updates and saves a ticket. 34 00:02:04,320 --> 00:02:06,920 So that's gonna be really our success case right there. 35 00:02:09,360 --> 00:02:13,930 And we'll also throw in another test to year very similar to the last one as well something that's just 36 00:02:13,930 --> 00:02:17,490 says it X the message 37 00:02:20,850 --> 00:02:24,310 that we could certainly write out a couple of comments inside about these but I bet you can imagine 38 00:02:24,370 --> 00:02:25,340 what's going to happen. 39 00:02:25,390 --> 00:02:26,640 We're gonna call the setup function. 40 00:02:26,680 --> 00:02:31,540 Get all this stuff kind of set up for us then call on message and then write out an expectation or to 41 00:02:31,760 --> 00:02:35,970 vary so much that we just did a moment ago inside a ticket created All right. 42 00:02:35,980 --> 00:02:37,040 So let's get to it. 43 00:02:37,270 --> 00:02:40,260 First thing we have to do right here create a listener to create the listener. 44 00:02:40,270 --> 00:02:42,920 We have to import our ticket. 45 00:02:43,090 --> 00:02:44,050 Updated listener. 46 00:02:44,050 --> 00:02:45,180 So the actual class. 47 00:02:45,250 --> 00:02:49,070 We also need to import the Nats wrapper because remember to create an instance of a listener. 48 00:02:49,120 --> 00:02:56,050 We have to provide a Nats client so at the very top I will add in an import for tickets. 49 00:02:56,240 --> 00:02:59,920 Updated listener from up directory. 50 00:02:59,920 --> 00:03:00,820 Ticket update listener 51 00:03:04,410 --> 00:03:06,310 I will also get my Nats rapper 52 00:03:10,130 --> 00:03:21,320 from 3 directories up Nats rapper then our listener will be a new ticket updated listener Nats rapper 53 00:03:21,470 --> 00:03:22,250 dot client 54 00:03:25,930 --> 00:03:30,160 after that we'll go ahead and create and save our ticket to do so we need access to our ticket model 55 00:03:30,800 --> 00:03:40,810 I will make sure that I import my ticket model from up 3 directories as well models ticket when we build 56 00:03:40,810 --> 00:03:45,280 a ticket we have to provide a I.D. for this ticket because remember the idea here is that we're sharing 57 00:03:45,280 --> 00:03:50,740 the same I.D. for all these different tickets across our different services in order to provide some 58 00:03:50,800 --> 00:03:55,720 I.D. or an existing idea we have to generate an idea on the fly and to generate an I.D. We're going 59 00:03:55,720 --> 00:04:00,640 to use Mongoose member Mongoose as a reference to that object I.D. constructor that we can use to make 60 00:04:00,640 --> 00:04:10,290 a new I.D. though I will also import mangoes from Mongoose so we can then create our ticket that would 61 00:04:10,300 --> 00:04:11,270 be a ticket. 62 00:04:11,290 --> 00:04:17,770 Build and then if we forget what property is we have to add as usual just mouse over. 63 00:04:17,850 --> 00:04:18,990 We've got to provide that I.D.. 64 00:04:19,020 --> 00:04:20,460 That's what we just imported mongers. 65 00:04:20,460 --> 00:04:27,570 We're going to generate that idea on the fly a title enterprise so our ideas will be mongers that types 66 00:04:28,080 --> 00:04:32,130 object ideas that to hex string 67 00:04:35,120 --> 00:04:45,870 then a title I'll use concert and a price of 20 that's going to create the ticket in memory it creates 68 00:04:45,880 --> 00:04:46,910 just the document. 69 00:04:46,930 --> 00:04:50,310 Now we have to actually take this ticket and save it to our tickets collection. 70 00:04:50,340 --> 00:04:57,670 So I will do it in a wait ticket that save like some. 71 00:04:57,700 --> 00:04:59,520 Next up our fake data object. 72 00:04:59,530 --> 00:05:04,810 So this is going to be the thing that describes some update to our ticket if we need a little bit of 73 00:05:04,810 --> 00:05:07,710 help on assembling this event or this data object. 74 00:05:07,720 --> 00:05:12,820 We can always import the event object or the event type from our common module. 75 00:05:12,820 --> 00:05:14,930 Let's do that at the very top. 76 00:05:14,980 --> 00:05:23,090 I'm going to import the tickets updated event from my common module 77 00:05:27,230 --> 00:05:35,760 will then use that to create our fake data objects so say data is of type ticket updated event I got 78 00:05:35,760 --> 00:05:36,450 ticket updated. 79 00:05:36,480 --> 00:05:36,750 Right. 80 00:05:36,750 --> 00:05:38,210 Yes very good. 81 00:05:38,220 --> 00:05:45,480 And specifically we want to create an object that mirrors the data property on that thing so that we 82 00:05:45,480 --> 00:05:48,540 can mouse over data and that will give us some help on what we need to do here. 83 00:05:48,630 --> 00:05:54,170 So we need the I.D. version titled price user I.D. Well the idea right here is going to be the same 84 00:05:54,200 --> 00:05:58,940 I.D. of the as the ticket that we have created just a moment ago right here. 85 00:05:58,940 --> 00:06:04,650 The variety that will be ticket dot I.D.. 86 00:06:04,850 --> 00:06:12,440 Next up a version to the version in this case it needs to be whatever this thing's version is plus one. 87 00:06:12,500 --> 00:06:20,150 So put in a version of the ticket's version plus one. 88 00:06:20,170 --> 00:06:22,430 Next up a title price and user I.D.. 89 00:06:22,540 --> 00:06:24,700 So these will be the things we actually change in some way. 90 00:06:24,700 --> 00:06:29,350 So we probably want to make sure that we provide either a different title maybe a different price user 91 00:06:29,350 --> 00:06:30,250 I.D. as usual. 92 00:06:30,250 --> 00:06:33,220 We're not really making use of it inside this order service. 93 00:06:33,250 --> 00:06:37,000 We could throw in some kind of a matching user I.D. It doesn't really matter but we should make sure 94 00:06:37,000 --> 00:06:40,810 that we provide some different title in some different price because that's what we're going to eventually 95 00:06:40,810 --> 00:06:42,010 write an assertion around. 96 00:06:42,100 --> 00:06:46,720 We're gonna make sure that when we process this data object right here we end up with whatever this 97 00:06:46,720 --> 00:06:48,410 thing's title and price are. 98 00:06:48,460 --> 00:06:51,070 So I'll give this a title of new concerts. 99 00:06:51,250 --> 00:06:53,560 So notice how it's different than the original title. 100 00:06:53,650 --> 00:06:58,370 Not very different but different enough and a price of about nine 9 9. 101 00:06:58,630 --> 00:07:01,150 I'll then throw in the user I.D. of whatever. 102 00:07:01,150 --> 00:07:02,440 Again we're not really using that. 103 00:07:02,440 --> 00:07:06,150 So not big difference right. 104 00:07:06,190 --> 00:07:10,240 So I do not have an error around the data object anymore which means that typescript is happy with my 105 00:07:10,240 --> 00:07:16,060 definition of that data object finally our fake message object. 106 00:07:16,060 --> 00:07:18,620 So again we will annotate this as a message. 107 00:07:18,670 --> 00:07:21,460 The only function we care about on this thing is the act function. 108 00:07:21,550 --> 00:07:24,190 We're going to make it a just mock function. 109 00:07:24,310 --> 00:07:28,080 Script is very unhappy with our definition of message right here. 110 00:07:28,090 --> 00:07:31,510 We have not actually imported that let's import that the very top really quick 111 00:07:36,110 --> 00:07:39,370 message from node and that's streaming. 112 00:07:39,770 --> 00:07:40,760 That's better. 113 00:07:40,760 --> 00:07:41,810 And now if we take a look. 114 00:07:41,810 --> 00:07:45,750 Touch it's going to be really unhappy with our definition because we are not defining all these other 115 00:07:45,770 --> 00:07:46,220 properties. 116 00:07:46,220 --> 00:07:50,420 But again we just don't care about them or the purposes of this test. 117 00:07:50,630 --> 00:07:55,790 So we can throw in a t yes ignore and say typescript just don't sweat it. 118 00:07:55,920 --> 00:08:00,570 We know what we're doing here and finally we're going to return all the stuff that we just created. 119 00:08:00,970 --> 00:08:07,920 It's all return inside an object the message the data object the ticket that we created and saved and 120 00:08:07,920 --> 00:08:09,810 the listener as well. 121 00:08:09,950 --> 00:08:18,460 The message data ticket listener Yeah that looks good let's take a quick pause right here and we will 122 00:08:18,460 --> 00:08:20,290 implement both our tests in just a moment.