1 00:00:01,180 --> 00:00:06,450 Let's do a very quick test around our order cancel listener just make sure it's working as expected. 2 00:00:06,610 --> 00:00:07,900 We're going to go through this rather quickly. 3 00:00:07,930 --> 00:00:09,730 So let's get to it inside of our test directory. 4 00:00:09,730 --> 00:00:19,490 We'll make an order cancelled listener test to yes file at the very top we'll make a setup function 5 00:00:19,490 --> 00:00:19,910 once again 6 00:00:23,240 --> 00:00:25,300 then inside of here we're going to need to create our listener. 7 00:00:25,300 --> 00:00:26,170 Create a ticket. 8 00:00:26,170 --> 00:00:29,480 Create the event create a message return everything. 9 00:00:29,480 --> 00:00:31,760 So first off we will create the listener itself. 10 00:00:31,770 --> 00:00:40,260 I'll do a listener is a new order canceled listener and we're going to have to pass in a client when 11 00:00:40,260 --> 00:00:40,680 we do that. 12 00:00:40,680 --> 00:00:45,570 So we'll assume that we're going to import Nats wrapper that client let's add in an import for both 13 00:00:45,570 --> 00:00:51,990 those first I'll get Nats rapper from up 3 directories. 14 00:00:51,990 --> 00:01:00,690 That's rapper and my order answered listener from up one directory. 15 00:01:00,770 --> 00:01:03,190 We then need to create a ticket and save it. 16 00:01:03,230 --> 00:01:04,370 So I'll get my ticket model 17 00:01:08,820 --> 00:01:09,210 and go 18 00:01:12,390 --> 00:01:17,020 don't make a new ticket or not new ticket. 19 00:01:17,020 --> 00:01:18,150 Ticket Bill. 20 00:01:18,200 --> 00:01:20,280 There we go. 21 00:01:20,390 --> 00:01:23,940 Throw in a fake title rice. 22 00:01:24,170 --> 00:01:25,070 What else do we need here. 23 00:01:25,070 --> 00:01:30,120 We need a user I.D. and we also need an order I.D. as well. 24 00:01:30,140 --> 00:01:34,610 So the order I.D. We are going to have to also include inside the order a canceled event that eventually 25 00:01:34,610 --> 00:01:36,440 flows into the listener itself. 26 00:01:36,440 --> 00:01:41,440 So we're probably going to want to generate a realistic looking I.D. and assign it to a separate variables 27 00:01:41,450 --> 00:01:43,520 we can make use of it later on. 28 00:01:43,520 --> 00:01:44,890 So for that let's import mongers. 29 00:01:44,900 --> 00:01:45,380 Once again 30 00:01:48,530 --> 00:02:02,770 and then make an order i.e. variable which will be Mongoose type stock object i.e. to X string. 31 00:02:02,790 --> 00:02:06,380 Now if we tried to add in the order I.D. like so you'll notice we get an error. 32 00:02:06,540 --> 00:02:10,740 The reason for that is we had assumed that whenever we first create a ticket or whenever we first build 33 00:02:10,740 --> 00:02:14,600 one in this case we would not yet have an order I.D. to assign to it. 34 00:02:14,610 --> 00:02:19,590 So the interface so we had assigned to the build function specifically says not going to have an order 35 00:02:19,590 --> 00:02:21,420 I.D. property. 36 00:02:21,440 --> 00:02:25,160 Now we could certainly change that and we could say that the order I.D. property is optional when we 37 00:02:25,160 --> 00:02:26,300 are first building a ticket. 38 00:02:26,390 --> 00:02:30,610 But I would be kind of changing our code just to make our tests a little bit easier. 39 00:02:30,620 --> 00:02:36,260 I really do not expect that in reality in our actual application code we're ever going to create a ticket 40 00:02:36,500 --> 00:02:39,080 and simultaneously assign it an order I.D.. 41 00:02:39,320 --> 00:02:41,380 So I'm not going to go change the interface. 42 00:02:41,420 --> 00:02:43,710 Instead we'll use a very very simple workaround. 43 00:02:43,790 --> 00:02:49,040 Right after we build the ticket we'll just set the order I.D. property they'll do a ticket set of order 44 00:02:49,040 --> 00:02:49,820 I.D.. 45 00:02:49,820 --> 00:02:56,720 Very simple workaround and definitely better in long term after that we'll save the ticket and there 46 00:02:56,720 --> 00:02:57,110 we go. 47 00:02:59,000 --> 00:03:04,340 Next up we're going to build our data events for this Let's import the definition or the interface of 48 00:03:04,340 --> 00:03:10,300 our event from our common module so at the top we'll get our order canceled event 49 00:03:13,570 --> 00:03:14,740 from our common module 50 00:03:18,340 --> 00:03:27,030 so we'll build up a data object of type order canceled events at data and then as usual we'll be told 51 00:03:27,030 --> 00:03:28,720 how to actually implement this thing. 52 00:03:28,740 --> 00:03:31,790 So we need an I.D. a version of the order itself. 53 00:03:31,980 --> 00:03:37,850 And then the tickets I.D. our I.D. is the order I.D. reversion doesn't really matter here. 54 00:03:37,870 --> 00:03:41,880 We'll just throw in a zero for the ticket itself. 55 00:03:41,880 --> 00:03:49,260 It's gonna be this tickets I.D. they will put in a ticket with an idea of ticket I.D. 56 00:03:52,870 --> 00:03:53,280 after that. 57 00:03:53,280 --> 00:03:54,550 Build up our message object 58 00:04:00,560 --> 00:04:03,120 and this will be of AK. 59 00:04:03,380 --> 00:04:07,470 Just not at Ben let's import that message type at the very top 60 00:04:15,230 --> 00:04:16,810 and I think that is it for our setup. 61 00:04:16,880 --> 00:04:17,970 Very last thing have to do. 62 00:04:17,990 --> 00:04:23,120 Well of course we need that little us ignore thing just to tell typescript. 63 00:04:23,160 --> 00:04:23,820 Don't sweat it. 64 00:04:23,820 --> 00:04:27,570 And then finally let's return our message data. 65 00:04:27,650 --> 00:04:30,870 Tickets order idea because we'll probably need to reference that. 66 00:04:30,870 --> 00:04:39,390 Who knows maybe at some point in time and the listener itself we can actually write out our test rather 67 00:04:39,390 --> 00:04:44,600 than writing out to tests and trying to make sure that say we update the tickets and save it. 68 00:04:44,610 --> 00:04:49,800 And then a separate one for making sure Ach it's called and maybe even a separate one for the publisher. 69 00:04:49,800 --> 00:04:55,050 Let's just condense this all down to one test just to save ourselves a little bit of time here so I 70 00:04:55,060 --> 00:05:05,480 can say it updates the tickets publishes an event and acts the message if you want to absolutely feel 71 00:05:05,480 --> 00:05:06,360 free. 72 00:05:06,410 --> 00:05:08,710 I would actually even recommend you do this right. 73 00:05:08,810 --> 00:05:10,480 Split this into three separate tests. 74 00:05:10,490 --> 00:05:13,220 I'm just gonna put it all into one just to save ourselves a little bit of time. 75 00:05:13,220 --> 00:05:17,390 For the purposes of this video it's one side of fear we'll call our setup function. 76 00:05:17,450 --> 00:05:19,400 Let's pull out the message data ticket. 77 00:05:19,400 --> 00:05:26,960 Order I.D. and a listener from a weight setup then as usual we'll call the on message function on a 78 00:05:26,960 --> 00:05:27,350 listener. 79 00:05:27,370 --> 00:05:37,110 So a wait listener on message send data and the message. 80 00:05:37,320 --> 00:05:38,910 Now we'll take a look into our database. 81 00:05:38,910 --> 00:05:41,010 We're going to take a look at the tickets collection. 82 00:05:41,010 --> 00:05:45,900 We're going to find the tickets right here or the same idea and we're going to make sure that it's order 83 00:05:45,900 --> 00:05:53,760 I.D. property was unset it should be equal to undefined so define a updated ticket that will be a way 84 00:05:53,790 --> 00:05:56,600 ticket find by Idi. 85 00:05:56,760 --> 00:06:00,650 We want to find the tickets I.D. And we're going to expect that things. 86 00:06:00,660 --> 00:06:00,950 Order. 87 00:06:00,960 --> 00:06:01,170 I.D.. 88 00:06:01,170 --> 00:06:03,010 Property to be undefined. 89 00:06:03,090 --> 00:06:12,730 Expect updated ticket that order I.D. to or not to be defined. 90 00:06:12,730 --> 00:06:13,070 There we go. 91 00:06:14,550 --> 00:06:19,430 And we gonna throw that exclamation in as usual it's Alex. 92 00:06:19,430 --> 00:06:19,840 Good. 93 00:06:19,850 --> 00:06:22,100 Let's also make sure that we call the act function. 94 00:06:22,100 --> 00:06:31,240 So do expect message dot EC Do you have been called and then finally we'll also make sure that the published 95 00:06:31,240 --> 00:06:36,080 function on our Nats wrapper client was invoked as well. 96 00:06:38,230 --> 00:06:47,010 I'll do it and expect Nats wrapper client publish to have been dealt. 97 00:06:47,080 --> 00:06:51,400 You'll notice here unlike the last test I'm not taking a look at that mock functions arguments. 98 00:06:51,400 --> 00:06:53,060 You can certainly do so if you wanted to. 99 00:06:53,080 --> 00:06:57,300 I would once again also really recommend it but in this case I'm just gonna make sure that we do publish 100 00:06:57,300 --> 00:06:58,090 and events. 101 00:06:58,140 --> 00:07:03,070 Not really checking what event writing like that but at least we know a event is being published so 102 00:07:03,130 --> 00:07:05,000 probably good enough right now. 103 00:07:05,220 --> 00:07:05,940 I'm gonna say this. 104 00:07:06,250 --> 00:07:10,630 Let's take a look at our tests and just see how we're doing. 105 00:07:10,640 --> 00:07:15,680 Looks like it's all working as expected as usual just a very quick comment out make sure that thing 106 00:07:15,680 --> 00:07:18,410 fails is always worth the time to do that. 107 00:07:18,410 --> 00:07:23,700 Yup definitely fails and so I'd say we're good to go Yes that is it for the order cancelled. 108 00:07:23,720 --> 00:07:26,700 Listener pause right here as usual. 109 00:07:26,700 --> 00:07:27,810 Move on in just a moment.