1 00:00:01,150 --> 00:00:04,780 In this video we're going to take a look at how we can add in a test to make sure that the new root 2 00:00:04,780 --> 00:00:09,900 handler in the update root handler are both actually publishing events that get started. 3 00:00:09,910 --> 00:00:13,630 I'm gonna go back to my underscore underscore mocks Nats wrapper file. 4 00:00:14,050 --> 00:00:17,760 Inside there we've got that fake implementation of the published function. 5 00:00:17,940 --> 00:00:23,590 So if we really want to make sure that we are somehow publishing an event off to Nats we really are 6 00:00:23,590 --> 00:00:27,760 going to want to make sure that this function right here truly gets executed. 7 00:00:27,760 --> 00:00:34,120 If this thing gets called that means that we are 100 percent trying to actually publish an event rather 8 00:00:34,120 --> 00:00:36,730 than providing this fake implementation this function. 9 00:00:36,850 --> 00:00:43,030 We're going to instead provide a mock function a mock function is essentially a fake function but it 10 00:00:43,030 --> 00:00:48,580 allows us to make tests around it or make expectations around it so we can expect that a mock function 11 00:00:48,790 --> 00:00:52,110 it's executed or that it gets executed with some particular arguments. 12 00:00:53,100 --> 00:00:59,240 So I'm going to delete our little fake implementation and I will replace that with just not a fan. 13 00:00:59,570 --> 00:01:01,380 That is how we create a muck. 14 00:01:01,490 --> 00:01:06,970 This is going to return a new function and assign it to the property of publish. 15 00:01:07,090 --> 00:01:10,720 That is a function that can be called from anything inside of application. 16 00:01:10,720 --> 00:01:15,340 And that function internally is going to keep track of whether or not it has been called what arguments 17 00:01:15,340 --> 00:01:16,080 it's been provided. 18 00:01:16,090 --> 00:01:16,720 And so on. 19 00:01:16,720 --> 00:01:19,350 So we can make some expectations around it. 20 00:01:19,610 --> 00:01:24,310 Now if we try to run our code like this right now none of our task or a lot of our tests are not going 21 00:01:24,310 --> 00:01:25,690 to actually pass. 22 00:01:25,750 --> 00:01:30,940 The reason for that is I remember eventually our publisher the base publisher is going to try to call 23 00:01:30,940 --> 00:01:37,240 this function that's going to provide a callback function that callback back function must be invoked 24 00:01:37,410 --> 00:01:42,520 for the publisher to think that everything went ok there's not quite enough to just provide this fake 25 00:01:42,520 --> 00:01:46,840 function because even though it will be invoked it needs to take the callback function that it will 26 00:01:46,840 --> 00:01:48,790 be provided and call it. 27 00:01:48,790 --> 00:01:52,090 In other words we need to still do this right here. 28 00:01:52,260 --> 00:01:58,630 So we need to still do this but we also want to have a mock function to to kind of do both them together 29 00:01:58,660 --> 00:02:02,170 to have a mock function and have a custom implementation. 30 00:02:02,200 --> 00:02:08,650 Here's what we're gonna write out we're gonna put in jest not f n and we'll chain on mock implementation 31 00:02:10,630 --> 00:02:13,710 and then to this we're going to provide a function. 32 00:02:13,830 --> 00:02:19,180 So this is the actual function that will be invoked when someone tries to run publish when someone tries 33 00:02:19,180 --> 00:02:24,010 to run publish it's still going to keep track of the fact that this thing was invoked a number of times 34 00:02:24,010 --> 00:02:28,660 it was invoked what arguments it was provided and that will allow us to run some tests and whatnot but 35 00:02:28,660 --> 00:02:34,690 we will also be able to execute this function right here taken that callback and invoke it to make sure 36 00:02:34,690 --> 00:02:37,960 that the base publisher is still happy. 37 00:02:37,980 --> 00:02:42,000 Now we do still have to write the same kind of function right here so this thing is going to be invoked 38 00:02:42,000 --> 00:02:48,960 by the based publisher which means it's going to receive a subject that is a string data that is a string 39 00:02:49,530 --> 00:02:51,590 and a callback function as well. 40 00:02:51,840 --> 00:02:58,030 That is a type function that doesn't return anything then inside the function body. 41 00:02:58,070 --> 00:03:05,090 Once again all we're going to do is execute callback. 42 00:03:05,160 --> 00:03:06,540 Now we see this right now. 43 00:03:07,760 --> 00:03:11,700 All of our tests should still run the same thing and go back over my test suite. 44 00:03:11,730 --> 00:03:16,730 Yeah looks like everything is still running as expected but now we can also start to write out some 45 00:03:16,760 --> 00:03:21,690 assertions and make sure that that published function is actually being invoked. 46 00:03:21,700 --> 00:03:28,950 Let me show you how back inside of my new test file I'm going to find roots underscore underscore test 47 00:03:29,250 --> 00:03:29,760 new test. 48 00:03:29,760 --> 00:03:36,640 Yes I'm going to go down to the very bottom the thing and I'm going to write out a new expectation or 49 00:03:36,650 --> 00:03:37,160 new test. 50 00:03:37,160 --> 00:03:41,000 I'll say it publishes an event 51 00:03:44,230 --> 00:03:50,360 to mark that function as async and then inside of here I want to try to successfully create a new ticket. 52 00:03:50,360 --> 00:03:55,130 And then after creating the ticket we're going to try to take a look at that fake function the one we 53 00:03:55,130 --> 00:03:59,960 just put together the mock one we're going to make sure the thing that that thing was actually invoked 54 00:04:00,400 --> 00:04:05,960 but to actually create a ticket I'm going to go up to the test that we run out just before I'm going 55 00:04:05,970 --> 00:04:11,030 to copy where we created a title and then made a request to create a ticket with the title and a given 56 00:04:11,030 --> 00:04:16,340 price and expected to get back responsive to a one I'm going to copy all that and paste it down inside 57 00:04:16,340 --> 00:04:23,460 my test so then right after we successfully make this request right after that we should be able to 58 00:04:23,460 --> 00:04:30,420 say that the publish function was called so to do so we're gonna go to the very top of the file and 59 00:04:30,420 --> 00:04:33,930 we're going to import the fake Nats wrapper. 60 00:04:33,930 --> 00:04:37,770 Now when I say fake we're actually going to import the real that's wrapper. 61 00:04:37,830 --> 00:04:43,380 But once again just even inside of our test file is going to redirect that import for us and it's going 62 00:04:43,380 --> 00:04:47,340 to instead give us the mark we're gonna try to get the real that's wrapper but it's going to give us 63 00:04:47,430 --> 00:04:58,770 the mock one instead by going to import Nat's wrapper from up one directory up another Nats dash wrapper 64 00:05:02,460 --> 00:05:07,890 then down inside the tests we just wrote right now let us do a console lug of Nat's wrapper and I want 65 00:05:07,890 --> 00:05:10,320 you to see something really interesting. 66 00:05:10,410 --> 00:05:16,210 Let's save this to flip back over now we're gonna see a lot of console logs right here but one of them 67 00:05:16,210 --> 00:05:19,440 we should see with a really big console log. 68 00:05:19,540 --> 00:05:23,650 And here it is right here though it's right underneath the pass statement for a new test. 69 00:05:23,650 --> 00:05:24,280 Yes. 70 00:05:24,280 --> 00:05:25,050 Here's the console log. 71 00:05:25,060 --> 00:05:26,020 We just did. 72 00:05:26,230 --> 00:05:29,910 You'll notice that this is the fake implementation we just put together. 73 00:05:29,950 --> 00:05:35,530 This is the contents of the fake or the mock and that's wrapper file though it's some plain object that 74 00:05:35,530 --> 00:05:41,800 has a client property that has an object that has a public function and the published thing is a mock 75 00:05:41,800 --> 00:05:42,790 function. 76 00:05:42,790 --> 00:05:44,700 That's what all this stuff is right here. 77 00:05:44,840 --> 00:05:49,090 But these are a bunch of helpers that we're going to allow us to make sure that this thing was actually 78 00:05:49,090 --> 00:05:53,800 called was invoked to some certain arguments and so on. 79 00:05:53,840 --> 00:05:58,800 So now we can actually write out an expectation down here at the very bottom going to put down here 80 00:05:59,610 --> 00:06:06,210 expect that's rapper not client not publish. 81 00:06:06,330 --> 00:06:08,800 To have been called. 82 00:06:08,880 --> 00:06:14,570 That's how we're going to test this thing and make sure that the publish function actually successfully 83 00:06:14,570 --> 00:06:18,050 gets invoked right after creating a ticket before testing this out. 84 00:06:18,060 --> 00:06:18,590 Our terminal. 85 00:06:18,590 --> 00:06:22,910 One other very quick thing to go inside of our Nats wrapper file. 86 00:06:22,940 --> 00:06:28,160 We created this mock implementation exactly one time the same mock function gets reused for every single 87 00:06:28,160 --> 00:06:29,280 one of our tests. 88 00:06:29,420 --> 00:06:34,160 Though usually between each of our tests we want to reset this mock function. 89 00:06:34,190 --> 00:06:38,120 Remember the mock function internally is going to record how many times it gets called the different 90 00:06:38,120 --> 00:06:40,250 arguments it is provided and so on. 91 00:06:40,350 --> 00:06:44,390 We're going to eventually write out some different expectations about those number of calls the arguments 92 00:06:44,390 --> 00:06:45,260 and so on. 93 00:06:45,260 --> 00:06:50,360 We want to make sure that between every single test we reset that data that we're not somehow running 94 00:06:50,360 --> 00:06:54,160 a test and polluting one test with data from another 10. 95 00:06:54,320 --> 00:06:56,870 Long story short back inside of our test setup. 96 00:06:56,910 --> 00:07:01,060 Yes file that's why we're doing all of our initial test config. 97 00:07:01,120 --> 00:07:04,690 We're gonna find out before each statement and inside there we'll write out. 98 00:07:04,690 --> 00:07:09,940 Just thought clear all box just have to do that one time and that's pretty much it. 99 00:07:11,190 --> 00:07:11,530 All right. 100 00:07:11,570 --> 00:07:12,620 Let's save this. 101 00:07:12,860 --> 00:07:17,900 Go back over to our terminal and we're going to make sure that all of our tests are passing in specific 102 00:07:18,200 --> 00:07:20,090 the new test suite. 103 00:07:20,090 --> 00:07:20,550 Right there. 104 00:07:20,580 --> 00:07:22,050 Yep looks like it's good. 105 00:07:22,070 --> 00:07:28,260 That means we are successfully calling that Polish function well well before continuing on because that's 106 00:07:28,260 --> 00:07:29,390 pretty much it. 107 00:07:29,400 --> 00:07:34,650 I just want to put every similar test inside of our update file as well and make sure that the published 108 00:07:34,650 --> 00:07:37,830 function gets called so I gonna go to my update test. 109 00:07:37,830 --> 00:07:51,610 Yes file at the very top I will import Nats rapper from up one directory up another that's wrapper I'm 110 00:07:51,620 --> 00:07:58,490 then going to go down to the bottom to file I'll add in it abolishes an event 111 00:08:01,470 --> 00:08:06,990 then instead of here we want to try to successfully edit or update a ticket and then we will make sure 112 00:08:06,990 --> 00:08:13,390 that the publish function got invoked so we can very easily come back up to the previous test. 113 00:08:13,500 --> 00:08:15,250 We'll just take some code right here. 114 00:08:15,360 --> 00:08:19,810 It looks like this one right here was around testing to make sure a ticket was actually updated. 115 00:08:19,820 --> 00:08:27,390 I can take everything from cookie on down to right after we do the update with the put we've got cookie 116 00:08:28,300 --> 00:08:35,020 one request second request I'm going to copy all that go down to the bottom to file a stat all in 117 00:08:38,990 --> 00:08:44,710 and then right after we'll do and expect that's wrapper client. 118 00:08:44,820 --> 00:08:50,590 Wish to have an alt right. 119 00:08:50,610 --> 00:08:56,190 Let's say that run these tests as well and make sure that the update is passing. 120 00:08:56,410 --> 00:08:57,160 And where is it. 121 00:08:57,190 --> 00:08:58,330 Yep there it is right there. 122 00:08:58,330 --> 00:08:59,250 Update is passing. 123 00:08:59,260 --> 00:09:01,120 Let's say we are good to go. 124 00:09:01,120 --> 00:09:01,380 Okay. 125 00:09:01,390 --> 00:09:04,730 That's pretty much it well we've got a working test suite now. 126 00:09:04,730 --> 00:09:09,290 We also have the ability to add in tests around publishing events which is just fantastic. 127 00:09:09,290 --> 00:09:11,510 So let's take a pause right here in as usual. 128 00:09:11,510 --> 00:09:12,490 Continue in just a minute.