1 00:00:01,380 --> 00:00:02,090 All right my friends. 2 00:00:02,100 --> 00:00:03,310 I think we now have a plan. 3 00:00:03,330 --> 00:00:08,070 So when we create the order inside of our Test File we're going to randomly generate a amount or the 4 00:00:08,100 --> 00:00:09,510 price for the order. 5 00:00:09,510 --> 00:00:14,820 Then when that goes off to the stripe API it should have some amount but that very specific amount. 6 00:00:14,820 --> 00:00:19,230 Well then later on INSIDE THE TEST file it request a list of the 10 most recent charges we should be 7 00:00:19,230 --> 00:00:24,630 able to then iterate through that list of 10 charges and find a charge with that same randomly generated 8 00:00:24,720 --> 00:00:25,770 amount. 9 00:00:25,770 --> 00:00:29,070 Now the one thing to keep in mind here something that's going to make our lives just slightly slightly 10 00:00:29,070 --> 00:00:29,930 difficult. 11 00:00:29,970 --> 00:00:35,100 Remember that inside the order we are providing a price that is in units of dollars or for the charge 12 00:00:35,130 --> 00:00:37,470 that gets sent off to the stripe API. 13 00:00:37,470 --> 00:00:39,650 It's gonna be in units of sense. 14 00:00:39,780 --> 00:00:41,800 So we are going to generate a price. 15 00:00:41,820 --> 00:00:45,510 And then when we try to find it inside of our list of charges that we just have to remember to multiply 16 00:00:45,510 --> 00:00:48,570 that price by hundred to go from dollars over two cents. 17 00:00:49,290 --> 00:00:49,500 OK. 18 00:00:49,500 --> 00:00:54,460 So let's get to it inside of my test right above the order. 19 00:00:54,590 --> 00:01:04,190 I'm going to randomly generate a price so I will do a math dot random times hundred thousand and I'm 20 00:01:04,200 --> 00:01:11,030 going to wrap that with a math dot for or then provide the price as the price property for the order 21 00:01:11,030 --> 00:01:11,930 itself. 22 00:01:11,990 --> 00:01:14,360 And naturally I can condense that down to just price 23 00:01:17,050 --> 00:01:23,400 now whenever our root handler creates the charge with the just API or seeing the stripe API it's gonna 24 00:01:23,410 --> 00:01:30,570 put that randomly generated amount inside there times 100 so after we make that request we're then going 25 00:01:30,570 --> 00:01:35,910 to write out some code right here to get a list of the 10 most recent charges we have created and inside 26 00:01:35,910 --> 00:01:42,670 that list we will try to find a charge with a price equal to or an amount equal to price times 100. 27 00:01:42,690 --> 00:01:45,270 So how do we list out all the 10 most recent charges. 28 00:01:45,270 --> 00:01:51,800 Well we're just looking at the API for a moment to go so we're going to use stripe charges dot list 29 00:01:52,510 --> 00:01:57,140 and if we want to we could put in the optional Limit amount right there so limit is the number of recent 30 00:01:57,140 --> 00:01:58,320 charges we want to get. 31 00:01:58,400 --> 00:02:03,210 The default is 10 but that can't go up to 100. 32 00:02:03,240 --> 00:02:07,800 Then one thing for you to be aware of is that in the response we get back from that call the list of 33 00:02:07,800 --> 00:02:10,970 charges is going to be nested on this data property. 34 00:02:11,070 --> 00:02:12,560 It's going to take a look at the data property. 35 00:02:12,570 --> 00:02:14,010 We will iterate through that thing. 36 00:02:14,010 --> 00:02:19,190 Take a look at each charge and try to find one with the correct amount OK. 37 00:02:19,220 --> 00:02:28,540 So after we make our request I'll then define stripe charges and that will be a weight stripe that charges 38 00:02:28,900 --> 00:02:29,910 that list. 39 00:02:30,040 --> 00:02:35,260 And again if we wanted to we could increase the limit here say up to 50 the limit that we use right 40 00:02:35,260 --> 00:02:40,300 here really just comes down to how often we expect to be running multiple copies of this test suite 41 00:02:40,330 --> 00:02:41,580 at the same time. 42 00:02:41,680 --> 00:02:46,600 If for some reason we are running 10 copies of this test suite at the same time if we rely upon the 43 00:02:46,600 --> 00:02:52,120 default value of 10 right here as opposed to 50 then it's possible that when we go to fetch our list 44 00:02:52,120 --> 00:02:56,080 of charges we might accidentally push out the charge so we are looking for. 45 00:02:56,080 --> 00:03:03,070 So by increasing this to 50 it pretty much reduces the of that to near zero. 46 00:03:03,110 --> 00:03:07,910 Now we're going to iterate over stripe charges dot data and try to find the appropriate charge. 47 00:03:08,010 --> 00:03:22,010 I'll say stripe charge is stripe charges dot data dot find and for every charge I want to iterate over 48 00:03:22,010 --> 00:03:27,440 each one and I want to return true for the first charge that has an amount to property because remember 49 00:03:27,440 --> 00:03:37,790 they have the amount property equal to our price times 100 language return charged dot amount equal 50 00:03:37,790 --> 00:03:40,880 to Rice times 100. 51 00:03:42,920 --> 00:03:48,080 Now we should be able to expect that stripe charge to be defined 52 00:03:50,810 --> 00:03:51,110 right now. 53 00:03:51,110 --> 00:03:55,220 This test is really just going to make sure that we create a charge with the appropriate amount assigned 54 00:03:55,220 --> 00:03:56,150 to it. 55 00:03:56,210 --> 00:04:00,290 Let's save this and go back over to our terminal and see how we're doing 56 00:04:03,110 --> 00:04:06,530 you'll notice that it takes a little bit longer to run that test file now because we are reaching out 57 00:04:06,530 --> 00:04:08,390 to the real stripe API. 58 00:04:08,390 --> 00:04:11,940 But it does look like our test passed awesome. 59 00:04:11,990 --> 00:04:15,860 Now if you want to try to make this thing fail we could go back over to our right handler itself. 60 00:04:15,860 --> 00:04:17,990 So here's the root handler. 61 00:04:18,120 --> 00:04:22,800 I'll go down to our code where we create the charge and I'll comment that out. 62 00:04:22,990 --> 00:04:25,240 Now we are not creating a charge at all. 63 00:04:25,330 --> 00:04:29,800 And that means that when we tried to go to get our list of most recent charges we should not find one 64 00:04:29,830 --> 00:04:31,410 with the appropriate price. 65 00:04:31,480 --> 00:04:34,030 And of course that should make our test fail. 66 00:04:34,030 --> 00:04:36,870 And there we go. 67 00:04:36,880 --> 00:04:37,080 All right. 68 00:04:37,090 --> 00:04:38,320 Well this definitely works. 69 00:04:38,320 --> 00:04:43,980 Now if we wanted to go too uncommon that by the way and now if we wanted to back inside of our test 70 00:04:43,980 --> 00:04:48,840 file we could take a deeper look at the strike charge object and make some more assertions about it. 71 00:04:49,320 --> 00:04:52,290 So for example we could make sure that it has the appropriate currency. 72 00:04:52,320 --> 00:04:56,800 We can make sure that it's got the correct somehow inspect the token or something like that. 73 00:04:56,940 --> 00:04:59,010 Maybe you just take a look at the currency would be enough. 74 00:04:59,260 --> 00:05:06,590 It's all do and expect stripe charge dot currency to equal USD. 75 00:05:06,630 --> 00:05:10,680 It is possible for strike charge to be undefined by the way because we are using a fine statement right 76 00:05:10,680 --> 00:05:11,070 here. 77 00:05:11,070 --> 00:05:15,450 So once again right after a strike charge will put in the exclamation to tell typescript don't sweat 78 00:05:15,450 --> 00:05:15,820 it. 79 00:05:15,840 --> 00:05:20,490 We technically are checking to make sure that the value is defined online right above typescript just 80 00:05:20,490 --> 00:05:31,330 doesn't really know how to interpret that line they'll save that and run it and there we go. 81 00:05:31,560 --> 00:05:31,810 All right. 82 00:05:31,840 --> 00:05:36,430 So again it is really up to you whether you want to use a mocked version of Stripe or if you want to 83 00:05:36,430 --> 00:05:37,760 use the real version. 84 00:05:37,780 --> 00:05:41,050 Personally I like to use the real version because even though it's a little bit slower. 85 00:05:41,050 --> 00:05:43,810 Well really a lot slower to run. 86 00:05:43,810 --> 00:05:48,430 It definitely is far more realistic and I make sure that my code is working as expected. 87 00:05:48,430 --> 00:05:51,670 The other nice thing here is that we can add in a different token. 88 00:05:51,700 --> 00:05:56,260 So maybe a completely invalid token and we can make sure that our root handler handles that in some 89 00:05:56,260 --> 00:05:57,910 appropriate fashion. 90 00:05:57,910 --> 00:06:02,680 It might be kind of hard to test out an invalid token if we were mocking out the API because we might 91 00:06:02,680 --> 00:06:07,810 not really know how to interpret a response from the API or wow the API is really going to respond if 92 00:06:07,810 --> 00:06:10,810 we give an invalid token OK. 93 00:06:10,860 --> 00:06:12,770 That is it for our more realistic test. 94 00:06:12,770 --> 00:06:14,900 So take a pause right here and move on in just a moment.