1 00:00:01,670 --> 00:00:05,210 As I mentioned at the end the last video we're going to write out a very quick test around the order 2 00:00:05,210 --> 00:00:06,290 created listener. 3 00:00:06,290 --> 00:00:11,330 Again I am sure at this point that you might be a bit tired of testing but it still is just such a valuable 4 00:00:11,330 --> 00:00:12,070 thing to have. 5 00:00:12,140 --> 00:00:15,160 We can make sure this code works without having to actually manually test it. 6 00:00:15,230 --> 00:00:17,210 So if you are tired of testing don't sweat it. 7 00:00:17,210 --> 00:00:20,460 We're not going to really go over any new techniques or anything like that inside this video. 8 00:00:20,540 --> 00:00:22,060 So you can skip over this one if you want to. 9 00:00:22,070 --> 00:00:27,240 Otherwise stick around and we'll put the test together for this very very quickly so to get started 10 00:00:27,270 --> 00:00:32,970 inside of listeners will make a new folder of underscore underscore test underscore underscore and then 11 00:00:32,970 --> 00:00:38,100 inside that I'll make a new file of order created listener test dot t yes 12 00:00:42,750 --> 00:00:43,490 then inside of here. 13 00:00:43,500 --> 00:00:47,830 As usual let's build our setup function. 14 00:00:47,850 --> 00:00:50,030 The first thing I do is well build a listener. 15 00:00:50,070 --> 00:00:55,620 For that we're going to need the Nats streaming server client from our Nats wrapper file so we know 16 00:00:55,620 --> 00:00:58,040 those are at least two imports we can do right away. 17 00:00:58,230 --> 00:01:01,710 So I will get Nats rapper from up 3 directories 18 00:01:04,920 --> 00:01:07,530 and I will get my order created. 19 00:01:07,530 --> 00:01:11,460 Listener from up one directory order create the listener 20 00:01:14,280 --> 00:01:19,310 will then make my order it created listener and feed in the Nats wrappers. 21 00:01:19,320 --> 00:01:29,070 Client after that we're going to create a order from scratch or I should say the data order event that 22 00:01:29,070 --> 00:01:32,820 we need to get our type for the order created event from our coming module. 23 00:01:32,970 --> 00:01:42,420 So at the very top we'll get our order created event from our common module so say that we're going 24 00:01:42,420 --> 00:01:43,080 to have some data. 25 00:01:43,080 --> 00:01:49,960 This will be of type order created event at data and then inside of here we definitely need to list 26 00:01:49,960 --> 00:01:54,620 out a realistic ideas property for the order itself while the other properties we could probably make 27 00:01:54,630 --> 00:02:00,370 up but at least the idea has to be realistic looking and as usual for that we will directly import Mongoose 28 00:02:01,600 --> 00:02:03,140 from mongoose. 29 00:02:03,200 --> 00:02:09,230 And the idea the order will be Mongoose types object idea that 2 hex string 30 00:02:12,520 --> 00:02:12,790 okay. 31 00:02:12,830 --> 00:02:13,880 So what else do we need here. 32 00:02:13,880 --> 00:02:20,870 Well version status user I.D. expires at the open in a version of 0 expires at. 33 00:02:20,880 --> 00:02:24,920 We're not releasing that so we get put in just whatever string a user I.D.. 34 00:02:24,930 --> 00:02:29,100 We're also not releasing that sold through in whatever string. 35 00:02:29,240 --> 00:02:30,210 What else do we need here. 36 00:02:30,210 --> 00:02:37,330 A status and then ticket stuff so for these status we do need the order status you know from our common 37 00:02:37,330 --> 00:02:43,020 module so we will add in an import for order status and we can list out a realistic looking status right 38 00:02:43,020 --> 00:02:43,620 here. 39 00:02:44,570 --> 00:02:49,660 Of read it and then for a ticket listing we'll have an I.D. again. 40 00:02:49,770 --> 00:02:57,480 Doesn't matter what the ideas in these have price as well which I will simply make 10 OK so there's 41 00:02:57,490 --> 00:03:00,330 our data object as usual. 42 00:03:00,340 --> 00:03:05,350 Next we'll make is our message object that is going to have the marked ACH function. 43 00:03:05,350 --> 00:03:10,510 So at the very top I will as usual import the message type from node that streaming 44 00:03:14,690 --> 00:03:20,230 will then tell typescript to ignore what we're about to type right here because no we are kind of cheating. 45 00:03:20,230 --> 00:03:21,760 Well really cheating. 46 00:03:21,760 --> 00:03:24,940 We'll say this thing has a ACH function. 47 00:03:25,150 --> 00:03:29,650 They'll be just append. 48 00:03:29,790 --> 00:03:31,050 I think that is it for a setup. 49 00:03:31,080 --> 00:03:33,300 So now going to return all this stuff. 50 00:03:33,340 --> 00:03:40,390 So at the very bottom a setup I will return listener data and message and we could start to write out 51 00:03:40,400 --> 00:03:45,700 actual tests we'll say it replicates the order info 52 00:03:48,570 --> 00:03:56,340 and we'll also make another test something like it acts the message. 53 00:03:56,510 --> 00:04:02,690 In both cases we're going to call the setup function and get back our listener data and message from 54 00:04:02,750 --> 00:04:12,650 a weight setup and then do an awaits listener dot on message and pass in the data and the message. 55 00:04:12,990 --> 00:04:15,680 And these first two lines right here we need inside both tests. 56 00:04:15,690 --> 00:04:18,820 So I'm going to copy paste those two lines down into the second test as well. 57 00:04:20,300 --> 00:04:20,510 OK. 58 00:04:20,530 --> 00:04:25,830 So we can actually do an expectation well in the case the first one we can try to find in order instead 59 00:04:25,830 --> 00:04:29,880 of our orders collection and just make sure that it has maybe the correct price or something like that 60 00:04:30,400 --> 00:04:31,530 so we can do that really quickly. 61 00:04:31,530 --> 00:04:40,050 We could say order will be a wait order find by I.T. we're gonna try to find an order that was saved 62 00:04:40,080 --> 00:04:45,510 with data I.D. We're making use of the order model right here so let's make sure we add an import for 63 00:04:45,510 --> 00:04:53,380 that at the top to file so at the very top I will import order from up 3 directories models order 64 00:04:58,490 --> 00:05:03,080 once we fetch the order we can make sure that it just has the correct price something very simple and 65 00:05:03,080 --> 00:05:04,030 straightforward. 66 00:05:04,150 --> 00:05:12,740 So say order exclamation don't forget that price match should equal data dot ticket price and then back 67 00:05:12,740 --> 00:05:23,770 in the message we can do a message dot EQ or we expect message dot ACH to have been built there we go 68 00:05:25,320 --> 00:05:25,590 OK. 69 00:05:25,620 --> 00:05:27,330 So that should be it for our two tests. 70 00:05:27,330 --> 00:05:32,740 Let's say this file will then go back over to our terminal and run them very quickly I'll change into 71 00:05:32,740 --> 00:05:36,670 my immense directory and do an NPM run test 72 00:05:42,590 --> 00:05:44,590 and looks good as usual. 73 00:05:44,630 --> 00:05:47,910 Let's comment out the two on message lines. 74 00:05:48,180 --> 00:05:53,570 So 1 and to save it look back over make sure it fails. 75 00:05:53,570 --> 00:05:55,060 Yep two failures. 76 00:05:55,130 --> 00:05:56,300 So I think we are looking good 77 00:05:59,170 --> 00:05:59,410 all right. 78 00:06:00,010 --> 00:06:04,180 Well like I said a test that looks very much all the ones we put together but at least now we are pretty 79 00:06:04,180 --> 00:06:06,820 sure that this thing works as expected. 80 00:06:06,880 --> 00:06:07,980 So one more pause. 81 00:06:08,020 --> 00:06:11,650 Come back the next video we're gonna start to work on another listener for order canceled.