1 00:00:00,740 --> 00:00:04,760 We were able to create a charge with stripes successfully and now we should probably write out some 2 00:00:04,760 --> 00:00:09,170 kind of automated test before we write out the test however I want to mention that there are two different 3 00:00:09,170 --> 00:00:12,770 approaches we can use to really test out this type stuff. 4 00:00:12,770 --> 00:00:17,600 First we could test this out by trying to make use of the actual stripe library. 5 00:00:17,690 --> 00:00:22,940 We can make sure that whenever we run a test or tried to test this handler we could ensure that we reach 6 00:00:22,940 --> 00:00:26,270 out to the stripe API successfully and create a new charge. 7 00:00:26,270 --> 00:00:28,640 And that would definitely be a very realistic test. 8 00:00:28,640 --> 00:00:33,260 However it would also be kind of annoying to put together because remember that environment variable 9 00:00:33,290 --> 00:00:38,600 for our stripe key is only accessible inside of our Cuban I.D. cluster but we are currently running 10 00:00:38,600 --> 00:00:42,530 our tests on our local machine outside of the cluster. 11 00:00:42,560 --> 00:00:45,440 That means we do not have access to that environment variable. 12 00:00:45,680 --> 00:00:50,240 So if we wanted to somehow make sure that we could reach out to the real stripe API we'd have to make 13 00:00:50,240 --> 00:00:53,660 sure that as we run our tests we still have access to that API key. 14 00:00:53,870 --> 00:00:58,070 That would involve just a little bit of setup possibly of creating a new environment variable on our 15 00:00:58,070 --> 00:00:59,270 local machine. 16 00:00:59,270 --> 00:01:03,820 It would definitely be something we could do but again just a little bit annoying. 17 00:01:03,930 --> 00:01:09,120 The second approach we could take is to create another muck around the stripe library itself or really 18 00:01:09,120 --> 00:01:11,040 our striped T S file. 19 00:01:11,100 --> 00:01:13,020 You might recall the inside of our SRT directory. 20 00:01:13,020 --> 00:01:14,580 We've got the MOX folder. 21 00:01:14,710 --> 00:01:17,210 We've already got a mocked Nats wrapper. 22 00:01:17,340 --> 00:01:22,830 We can create a very similar mock or the stripe t s file as well and we can make sure that we export 23 00:01:22,830 --> 00:01:27,650 some kind of mock object that has the same kind of API as this thing right here. 24 00:01:27,660 --> 00:01:33,120 So some kind of object that has a charges property and on that charge is property has a create function 25 00:01:33,570 --> 00:01:37,800 that returns some kind of promise that will eventually be result again. 26 00:01:37,810 --> 00:01:43,270 Two possible approaches we're gonna first take a look at how to implement this using a mock and then 27 00:01:43,330 --> 00:01:44,990 maybe we'll see what happens. 28 00:01:45,070 --> 00:01:49,360 Perhaps we'll refactor it to use the actual stripe API All right. 29 00:01:49,370 --> 00:01:55,280 So to get started inside of our MOX directory inside the s RC folder we're gonna make a new file of 30 00:01:56,490 --> 00:01:59,300 Stripe dot t s it's inside of here. 31 00:01:59,320 --> 00:02:05,040 We're going to make a fake copy of Stripe and this will be executed or imported only when we are running 32 00:02:05,040 --> 00:02:07,560 our code in the test environment. 33 00:02:07,650 --> 00:02:12,420 So ultimately we want to export something that is going to have the same kind of structure as this file 34 00:02:12,420 --> 00:02:13,290 right here. 35 00:02:13,290 --> 00:02:18,600 So we want to make sure we export something called stripe but the lowercase S. and it needs to have 36 00:02:18,630 --> 00:02:24,800 a charges property and on the charges property it has to have that create function so inside of this 37 00:02:24,800 --> 00:02:29,270 mock file I will export bounced stripe. 38 00:02:29,310 --> 00:02:35,610 This is gonna be an object that has a charge is property and on charges we will have a create function 39 00:02:37,120 --> 00:02:38,640 for this great function right here. 40 00:02:38,660 --> 00:02:42,860 Well we could definitely make it a real function but instead we're going to make it a just mock member 41 00:02:42,950 --> 00:02:47,300 mock functions are really fantastic because we can take a look at them make sure they get called with 42 00:02:47,300 --> 00:02:54,030 appropriate arguments we're gonna put in here just f n and this time around this mock function is gonna 43 00:02:54,040 --> 00:02:56,030 be just a little bit more complicated. 44 00:02:56,140 --> 00:03:01,020 You might recall back inside of our Nats wrapper or the mock function we created over here. 45 00:03:01,030 --> 00:03:05,740 We changed on that mock implementation function and that made sure that time we called the public function 46 00:03:05,920 --> 00:03:09,010 we actually executed this function right here. 47 00:03:09,010 --> 00:03:13,600 We're gonna do something very similar this time around but instead of providing a mock implementation 48 00:03:14,110 --> 00:03:18,580 we're going to instead provide a dot mock resolved value 49 00:03:21,530 --> 00:03:23,180 to this mock result value right here. 50 00:03:23,180 --> 00:03:27,550 Make sure that whenever we call the create function that we are going to get back a promise that automatically 51 00:03:27,860 --> 00:03:30,560 resolves itself with an empty object. 52 00:03:30,590 --> 00:03:35,910 The only reason we are doing this is because back inside of our actual root handler our expectation 53 00:03:35,910 --> 00:03:39,750 right here is that when we call create we are going to get back some kind of promise and then we are 54 00:03:39,840 --> 00:03:45,290 awaiting that promise to be result so that's why we are going to mock resolve value. 55 00:03:45,290 --> 00:03:50,320 We're gonna return a promise immediately that automatically resolves itself okay. 56 00:03:50,330 --> 00:03:52,990 So that is it for the mock file believe it or not. 57 00:03:53,000 --> 00:03:57,700 So now we can go back over to our test file inside that test directory and we could write out a new 58 00:03:57,700 --> 00:04:04,830 test that asserts so we can make a request to our endpoints with some valid token or whatever else. 59 00:04:05,020 --> 00:04:10,630 We'll make sure that we call that Mark create function then we could write up some kind of expectation 60 00:04:10,660 --> 00:04:11,320 around it. 61 00:04:12,300 --> 00:04:18,330 At the very top of this file we will tell just to redirect the import to that stripe test file and instead 62 00:04:18,330 --> 00:04:26,090 use our mocked version of these strike teams file we'll do a just dot mock up to directories stripe 63 00:04:29,880 --> 00:04:30,930 then down at the bottom of file 64 00:04:34,100 --> 00:04:41,660 we'll put together a new test and say it returns a about to a fourth because we are technically creating 65 00:04:41,660 --> 00:04:49,640 a record year with valid inputs inside of this test. 66 00:04:49,640 --> 00:04:54,500 We're going to do a little bit of setup called a handler and then maybe also make sure besides the fact 67 00:04:54,500 --> 00:04:55,690 that returns a two a four. 68 00:04:55,760 --> 00:05:01,190 Make sure it actually calls that create function or the charge create function and provide some valid 69 00:05:01,220 --> 00:05:02,280 arguments to it. 70 00:05:02,970 --> 00:05:06,510 So inside of here we're going to do the same kind of setup we did in the previous test. 71 00:05:06,560 --> 00:05:11,740 I to go to the previous test and just take everything from user I.D. down to the order dot save it's 72 00:05:11,890 --> 00:05:19,130 going to copy all that and put it into our new test I will update the status here from canceled to created 73 00:05:19,400 --> 00:05:25,050 to make sure that we can actually pay for this order and then after saving the order I'll make the actual 74 00:05:25,050 --> 00:05:27,160 request so I'll do it in a wait. 75 00:05:27,180 --> 00:05:38,770 Request due at I want to do a post request to API payments I'm going to set my cookie to user or a global 76 00:05:38,770 --> 00:05:39,370 dot sign it. 77 00:05:39,370 --> 00:05:44,570 That's why I want user I.D. and I'm going to send along 78 00:05:48,850 --> 00:05:50,080 a valid token. 79 00:05:50,140 --> 00:05:54,520 Now keep in mind that this token is not actually going to be used because we have mocked out the charge 80 00:05:54,520 --> 00:05:58,960 create function but we will remove that mark and just a little bit to take a look at an alternative 81 00:05:58,960 --> 00:05:59,350 approach. 82 00:05:59,350 --> 00:06:03,180 As I mentioned and so eventually we do want make sure they've got a valid token here. 83 00:06:03,210 --> 00:06:09,010 So since our account is in the test mode we can put in T okay underscore visa and as I mentioned this 84 00:06:09,010 --> 00:06:14,240 is a token that is always going to work for strip accounts that are in the test mode and then our order 85 00:06:14,240 --> 00:06:22,270 I.D. will be the I.D. the order we just created a moment ago to order dot IP all right this video is 86 00:06:22,270 --> 00:06:23,180 running a little bit long. 87 00:06:23,190 --> 00:06:26,170 So let's just take a quick pause right here and then wrap up this test in the next video.