1 00:00:06,480 --> 00:00:08,730 - In this section, we're going to see how to test 2 00:00:08,730 --> 00:00:12,540 for collections and the elements held in those collections. 3 00:00:12,540 --> 00:00:14,220 So we'll see how to test 4 00:00:14,220 --> 00:00:18,194 for an item in an array by object reference. 5 00:00:18,194 --> 00:00:21,565 Then we'll see how to test for objects, values 6 00:00:21,565 --> 00:00:23,880 being in an array. 7 00:00:23,880 --> 00:00:27,510 Then we'll see how to test maps in EMCA 8 00:00:27,510 --> 00:00:30,600 Script six, a map is a key value collection 9 00:00:30,600 --> 00:00:33,540 and you can test whether a key is present in the map. 10 00:00:33,540 --> 00:00:35,220 You can, you can also test the value 11 00:00:35,220 --> 00:00:37,500 of an item in the map as well. 12 00:00:37,500 --> 00:00:39,968 ECMA Script six also has a set collection 13 00:00:39,968 --> 00:00:42,810 which is a unique distinct collection. 14 00:00:42,810 --> 00:00:45,210 If you try to insert the same value intercept 15 00:00:45,210 --> 00:00:47,580 multiple times, it only stores one 16 00:00:47,580 --> 00:00:49,620 copy it doesn't have duplicates. 17 00:00:49,620 --> 00:00:53,912 So you can test to see if a set contains a particular item. 18 00:00:53,912 --> 00:00:56,850 Okay? So the examples are in the same place 19 00:00:56,850 --> 00:00:59,640 as usual in the standard jest matchers folder. 20 00:00:59,640 --> 00:01:03,752 In my example test and it is located here, 21 00:01:03,752 --> 00:01:07,912 JS T D D less than two standard jest matchers. 22 00:01:07,912 --> 00:01:11,677 And as before, if you open that folder in the code editor 23 00:01:11,677 --> 00:01:13,320 this is how it looks. 24 00:01:13,320 --> 00:01:16,620 In my example test, we have a test suite matchers 25 00:01:16,620 --> 00:01:19,620 for collections that's for this section. 26 00:01:19,620 --> 00:01:23,490 And we have several tests in here to test for items being 27 00:01:23,490 --> 00:01:28,140 in an array by object, identity, or by value, looking 28 00:01:28,140 --> 00:01:32,730 for keys and values in a map and looking for items in a set. 29 00:01:32,730 --> 00:01:35,840 Okay. So 1, 2, 3, 5 tests there altogether 30 00:01:35,840 --> 00:01:39,120 that we're going to be looking at in this section. 31 00:01:39,120 --> 00:01:40,140 So first things first 32 00:01:40,140 --> 00:01:42,840 I guess we should run those tests just to be 33 00:01:42,840 --> 00:01:43,980 on the safe side. 34 00:01:43,980 --> 00:01:47,096 So to run the tests, usual arrangement, NPM 35 00:01:47,096 --> 00:01:50,970 or yarn run test specify the collections 36 00:01:50,970 --> 00:01:54,720 selected there for the suite, just to make sure that we only 37 00:01:54,720 --> 00:01:57,090 run the tests that we're actually interested in. 38 00:01:57,090 --> 00:01:58,710 So let's do that now. 39 00:01:58,710 --> 00:02:01,489 So I'm in the standard jest matchers folder, NPM run 40 00:02:01,489 --> 00:02:05,703 test parameters minus T collections. 41 00:02:08,112 --> 00:02:09,650 Okay. So it'll find the suite container 42 00:02:09,650 --> 00:02:13,620 my collections tests, five of them and it'll run them 43 00:02:13,620 --> 00:02:17,910 and all being well, they will succeed. 44 00:02:17,910 --> 00:02:19,773 Let's give it a quick look here. 45 00:02:20,940 --> 00:02:24,000 So it's doing it towards the end of the example. 46 00:02:24,000 --> 00:02:25,620 There's my suite. 47 00:02:25,620 --> 00:02:28,410 And there's the five tests which have all succeeded. 48 00:02:28,410 --> 00:02:29,243 So that's good. 49 00:02:29,243 --> 00:02:32,016 Now let's have a look at the code for those tests. 50 00:02:32,016 --> 00:02:37,016 So first of all, if you want to check, if an array 51 00:02:37,519 --> 00:02:42,033 contains an object, you can use the to Contain matcher 52 00:02:42,033 --> 00:02:44,550 and it compares object references. 53 00:02:44,550 --> 00:02:46,410 So let, let's have a look at this. 54 00:02:46,410 --> 00:02:50,040 This is important that you understand what it is doing 55 00:02:50,040 --> 00:02:51,270 and what it isn't doing. 56 00:02:51,270 --> 00:02:55,156 So in this test, P1 is one person. 57 00:02:55,156 --> 00:02:58,440 That's our daughter, Emily who's 24 58 00:02:58,440 --> 00:03:00,930 and P2 is another person object, 59 00:03:00,930 --> 00:03:05,850 Tom that's our son they're twins and Jane that's my wife. 60 00:03:05,850 --> 00:03:09,120 Oh, when I wrote this slide, she was 56. 61 00:03:09,120 --> 00:03:10,512 She's had a birthday since then. 62 00:03:10,512 --> 00:03:12,330 And here's me. 63 00:03:12,330 --> 00:03:15,630 I've also had a birthday since then, but anyway 64 00:03:15,630 --> 00:03:19,918 P1, P2, P3, P4, those are pointers to objects. 65 00:03:19,918 --> 00:03:23,360 I created an array of the first three people. 66 00:03:23,360 --> 00:03:26,117 So P one is appointed to that object. 67 00:03:26,117 --> 00:03:27,343 P two points there. 68 00:03:27,343 --> 00:03:28,906 P three points there. 69 00:03:28,906 --> 00:03:30,510 I can check. 70 00:03:30,510 --> 00:03:34,208 I'm expecting my people array to contain P one. 71 00:03:34,208 --> 00:03:35,400 Okay. 72 00:03:35,400 --> 00:03:40,400 So does my people array contain a reference to this object? 73 00:03:40,686 --> 00:03:42,960 Yes, it does. 74 00:03:42,960 --> 00:03:45,674 Does the people array not contain a reference 75 00:03:45,674 --> 00:03:46,954 to that object? 76 00:03:46,954 --> 00:03:51,690 Correct? People does not contain a reference to P four. 77 00:03:51,690 --> 00:03:54,360 It contains a reference to P one P two P three 78 00:03:54,360 --> 00:03:57,150 but it doesn't contain a pointer to P four. 79 00:03:57,150 --> 00:03:59,190 So it does not contain P four. 80 00:03:59,190 --> 00:04:01,470 Neither does it contain this object. 81 00:04:01,470 --> 00:04:03,870 Now here's an important point when you use to Contain, 82 00:04:03,870 --> 00:04:07,683 it's checking object references, not object values. 83 00:04:08,681 --> 00:04:12,623 So this object here P three, okay. 84 00:04:13,920 --> 00:04:17,730 Is so my array contains appointed to P three, person three. 85 00:04:17,730 --> 00:04:20,400 So it contains appointed to this object. 86 00:04:20,400 --> 00:04:23,220 The value in that object is the same 87 00:04:23,220 --> 00:04:26,640 as the value in this object, but they are different objects. 88 00:04:26,640 --> 00:04:29,864 This object here has one address in memory. 89 00:04:29,864 --> 00:04:32,790 This object has a different address in memory. 90 00:04:32,790 --> 00:04:36,600 It isn't, it it's checking object addresses. 91 00:04:36,600 --> 00:04:37,873 The address of this object 92 00:04:37,873 --> 00:04:42,030 in memory is not the same as the address of this object. 93 00:04:42,030 --> 00:04:47,030 Okay. So people does not contain a reference to this object. 94 00:04:47,460 --> 00:04:51,390 It looks at object pointers, not object values. 95 00:04:51,390 --> 00:04:53,640 If you want to check the values of objects 96 00:04:53,640 --> 00:04:58,110 then don't use to Contain, use to Contain equal instead. 97 00:04:58,110 --> 00:04:59,640 And that looks at the values 98 00:04:59,640 --> 00:05:02,730 of objects rather than worrying about their addresses. 99 00:05:02,730 --> 00:05:05,370 So if you had two objects, two different objects 100 00:05:05,370 --> 00:05:08,650 that have the same values they would now compare equal. 101 00:05:08,650 --> 00:05:10,860 So let's have a look at this one then. 102 00:05:10,860 --> 00:05:12,480 P one is Emily 103 00:05:12,480 --> 00:05:15,989 P two is Thomas and P three is my wife Jane. 104 00:05:15,989 --> 00:05:20,010 So all of those three people are in my array. 105 00:05:20,010 --> 00:05:23,400 People contains a reference to this object, that object 106 00:05:23,400 --> 00:05:24,260 and that object. 107 00:05:24,260 --> 00:05:28,830 If I say to Contain equal, is there any element 108 00:05:28,830 --> 00:05:32,063 in this array that has the same value as this? 109 00:05:32,063 --> 00:05:33,660 Yes. 110 00:05:33,660 --> 00:05:38,010 This object up here has the same value as this object here. 111 00:05:38,010 --> 00:05:42,150 They are different objects, but they have the same values, 112 00:05:42,150 --> 00:05:45,120 the same property names and the same property values. 113 00:05:45,120 --> 00:05:49,503 Okay. So this object has the same values as this object. 114 00:05:50,430 --> 00:05:55,430 So my array does contain an element which equals that data. 115 00:05:57,000 --> 00:05:59,280 If I use to Contain, 116 00:05:59,280 --> 00:06:02,141 then I'm back to comparing references again. 117 00:06:02,141 --> 00:06:02,974 Okay. 118 00:06:02,974 --> 00:06:07,830 So this object here is not held as a reference in here. 119 00:06:07,830 --> 00:06:10,500 So to Contain equal checks the value 120 00:06:10,500 --> 00:06:14,171 of that object against the value of that object. 121 00:06:14,171 --> 00:06:16,590 To Contain compares the address 122 00:06:16,590 --> 00:06:19,650 of that object against the address of that, of that object. 123 00:06:19,650 --> 00:06:23,493 Okay. And then clearly the second one fails. 124 00:06:24,676 --> 00:06:29,250 Okay. So to contain checks for object references 125 00:06:29,250 --> 00:06:31,290 to contain equal says, I 126 00:06:31,290 --> 00:06:33,030 I don't care if they different objects 127 00:06:33,030 --> 00:06:35,530 as long as they have the same values, that's fine. 128 00:06:37,410 --> 00:06:38,850 Right. What about maps? 129 00:06:38,850 --> 00:06:43,850 So a map contains keys and values in the map class 130 00:06:45,150 --> 00:06:46,680 in ECMA script. 131 00:06:46,680 --> 00:06:50,820 You can use the has function to see if a map has a key 132 00:06:50,820 --> 00:06:54,960 and then we can check if the result is true. 133 00:06:54,960 --> 00:06:56,430 Have a look at this test here. 134 00:06:56,430 --> 00:06:58,860 I've got some international telephone dialing codes 135 00:06:58,860 --> 00:07:01,200 in my map, my map, oh, this is how 136 00:07:01,200 --> 00:07:03,900 or this is the easiest way to create a map. 137 00:07:03,900 --> 00:07:06,870 You give it an array of items. 138 00:07:06,870 --> 00:07:10,500 Each item is another array with a key and a value. 139 00:07:10,500 --> 00:07:13,710 So South Africa dialing code is 27. 140 00:07:13,710 --> 00:07:17,940 Norway is 47, Singapore 65. 141 00:07:17,940 --> 00:07:19,620 Okay. So those are the keys. 142 00:07:19,620 --> 00:07:23,070 And those are the values for my three items. 143 00:07:23,070 --> 00:07:26,130 Check my dialing codes map. 144 00:07:26,130 --> 00:07:29,640 Does it have this key Singapore? 145 00:07:29,640 --> 00:07:34,260 I'm expecting that the has function should return true. 146 00:07:34,260 --> 00:07:35,093 Okay. 147 00:07:35,093 --> 00:07:37,056 So does the has function return true? 148 00:07:37,056 --> 00:07:41,100 It should do because Singapore is present as a key. 149 00:07:41,100 --> 00:07:42,690 And it, it is. 150 00:07:42,690 --> 00:07:47,690 If I check for the UK, does my map have the UK key? 151 00:07:49,770 --> 00:07:51,450 No. So I'm expecting that to be false. 152 00:07:51,450 --> 00:07:54,180 The UK is not one of the present keys in here. 153 00:07:54,180 --> 00:07:56,776 So call the has function, which retains true or false 154 00:07:56,776 --> 00:08:00,303 and then check whether it was true or whether it was false. 155 00:08:01,230 --> 00:08:03,690 Okay. Let's check in whether a key exists. 156 00:08:03,690 --> 00:08:05,700 If you wanna check the value 157 00:08:05,700 --> 00:08:08,880 then you can do this, call get on the map. 158 00:08:08,880 --> 00:08:11,757 When you call the get function, you pass in a key 159 00:08:11,757 --> 00:08:15,390 and it gives you back the value for that key. 160 00:08:15,390 --> 00:08:17,520 And you can then test if the value is what you 161 00:08:17,520 --> 00:08:18,420 expected it to be. 162 00:08:18,420 --> 00:08:19,470 Let's have a look. 163 00:08:19,470 --> 00:08:22,350 So here's my map of the three countries again. 164 00:08:22,350 --> 00:08:26,700 If I look up the S3 key, Singapore, 165 00:08:26,700 --> 00:08:29,700 get the value associated with that key. 166 00:08:29,700 --> 00:08:31,830 The value is plus six five. 167 00:08:31,830 --> 00:08:34,980 So I'm expecting the value to be plus six five. 168 00:08:34,980 --> 00:08:37,050 That's the dialing code for Singapore. 169 00:08:37,050 --> 00:08:37,883 Correct. 170 00:08:38,850 --> 00:08:40,500 If you, now this is a thing. 171 00:08:40,500 --> 00:08:44,940 If you try to look up a key that doesn't exist in the 172 00:08:44,940 --> 00:08:48,876 in the map class, in JavaScript, it doesn't throw an error 173 00:08:48,876 --> 00:08:50,940 or anything, you know, dramatic. 174 00:08:50,940 --> 00:08:52,689 It just returns undefined. 175 00:08:52,689 --> 00:08:56,520 So if I try to look up a key that doesn't exist 176 00:08:56,520 --> 00:08:58,890 I would expect to get back undefined. 177 00:08:58,890 --> 00:09:02,640 In other words, no, no such key in the map. 178 00:09:02,640 --> 00:09:05,250 Okay. So UK is not present as a key. 179 00:09:05,250 --> 00:09:08,636 If I look it up, I'd get back undefined. 180 00:09:08,636 --> 00:09:12,710 Okay. Right last then in this section, a set. 181 00:09:12,710 --> 00:09:15,363 A set is a unique collection. 182 00:09:16,200 --> 00:09:20,340 You can call the has function on a set to see 183 00:09:20,340 --> 00:09:24,690 if it has an item and then check if the result is, is true. 184 00:09:24,690 --> 00:09:29,100 So here I've created a set, I've added three countries, 185 00:09:29,100 --> 00:09:34,100 South Africa, Norway, Singapore, take my set and does check. 186 00:09:35,940 --> 00:09:38,655 Does my set contain Singapore? 187 00:09:38,655 --> 00:09:40,080 Yes, it does. 188 00:09:40,080 --> 00:09:41,610 So that should return true. 189 00:09:41,610 --> 00:09:43,980 The has function returns true or false. 190 00:09:43,980 --> 00:09:46,440 If I look up a key, that's a value that's present. 191 00:09:46,440 --> 00:09:48,120 It should return true. 192 00:09:48,120 --> 00:09:49,322 And if I look up a, 193 00:09:49,322 --> 00:09:53,970 a value that's not present, then it should return false. 194 00:09:53,970 --> 00:09:58,710 Okay. So using these set related and map 195 00:09:58,710 --> 00:10:02,720 and array related matches, you can test anything you want to 196 00:10:02,720 --> 00:10:05,103 in a collection in modern JavaScript.