- In this section, we're going to see how to test
for collections and the elements held in those collections.
So we'll see how to test
for an item in an array by object reference.
Then we'll see how to test for objects, values
being in an array.
Then we'll see how to test maps in EMCA
Script six, a map is a key value collection
and you can test whether a key is present in the map.
You can, you can also test the value
of an item in the map as well.
ECMA Script six also has a set collection
which is a unique distinct collection.
If you try to insert the same value intercept
multiple times, it only stores one
copy it doesn't have duplicates.
So you can test to see if a set contains a particular item.
Okay? So the examples are in the same place
as usual in the standard jest matchers folder.
In my example test and it is located here,
JS T D D less than two standard jest matchers.
And as before, if you open that folder in the code editor
this is how it looks.
In my example test, we have a test suite matchers
for collections that's for this section.
And we have several tests in here to test for items being
in an array by object, identity, or by value, looking
for keys and values in a map and looking for items in a set.
Okay. So 1, 2, 3, 5 tests there altogether
that we're going to be looking at in this section.
So first things first
I guess we should run those tests just to be
on the safe side.
So to run the tests, usual arrangement, NPM
or yarn run test specify the collections
selected there for the suite, just to make sure that we only
run the tests that we're actually interested in.
So let's do that now.
So I'm in the standard jest matchers folder, NPM run
test parameters minus T collections.
Okay. So it'll find the suite container
my collections tests, five of them and it'll run them
and all being well, they will succeed.
Let's give it a quick look here.
So it's doing it towards the end of the example.
There's my suite.
And there's the five tests which have all succeeded.
So that's good.
Now let's have a look at the code for those tests.
So first of all, if you want to check, if an array
contains an object, you can use the to Contain matcher
and it compares object references.
So let, let's have a look at this.
This is important that you understand what it is doing
and what it isn't doing.
So in this test, P1 is one person.
That's our daughter, Emily who's 24
and P2 is another person object,
Tom that's our son they're twins and Jane that's my wife.
Oh, when I wrote this slide, she was 56.
She's had a birthday since then.
And here's me.
I've also had a birthday since then, but anyway
P1, P2, P3, P4, those are pointers to objects.
I created an array of the first three people.
So P one is appointed to that object.
P two points there.
P three points there.
I can check.
I'm expecting my people array to contain P one.
Okay.
So does my people array contain a reference to this object?
Yes, it does.
Does the people array not contain a reference
to that object?
Correct? People does not contain a reference to P four.
It contains a reference to P one P two P three
but it doesn't contain a pointer to P four.
So it does not contain P four.
Neither does it contain this object.
Now here's an important point when you use to Contain,
it's checking object references, not object values.
So this object here P three, okay.
Is so my array contains appointed to P three, person three.
So it contains appointed to this object.
The value in that object is the same
as the value in this object, but they are different objects.
This object here has one address in memory.
This object has a different address in memory.
It isn't, it it's checking object addresses.
The address of this object
in memory is not the same as the address of this object.
Okay. So people does not contain a reference to this object.
It looks at object pointers, not object values.
If you want to check the values of objects
then don't use to Contain, use to Contain equal instead.
And that looks at the values
of objects rather than worrying about their addresses.
So if you had two objects, two different objects
that have the same values they would now compare equal.
So let's have a look at this one then.
P one is Emily
P two is Thomas and P three is my wife Jane.
So all of those three people are in my array.
People contains a reference to this object, that object
and that object.
If I say to Contain equal, is there any element
in this array that has the same value as this?
Yes.
This object up here has the same value as this object here.
They are different objects, but they have the same values,
the same property names and the same property values.
Okay. So this object has the same values as this object.
So my array does contain an element which equals that data.
If I use to Contain,
then I'm back to comparing references again.
Okay.
So this object here is not held as a reference in here.
So to Contain equal checks the value
of that object against the value of that object.
To Contain compares the address
of that object against the address of that, of that object.
Okay. And then clearly the second one fails.
Okay. So to contain checks for object references
to contain equal says, I
I don't care if they different objects
as long as they have the same values, that's fine.
Right. What about maps?
So a map contains keys and values in the map class
in ECMA script.
You can use the has function to see if a map has a key
and then we can check if the result is true.
Have a look at this test here.
I've got some international telephone dialing codes
in my map, my map, oh, this is how
or this is the easiest way to create a map.
You give it an array of items.
Each item is another array with a key and a value.
So South Africa dialing code is 27.
Norway is 47, Singapore 65.
Okay. So those are the keys.
And those are the values for my three items.
Check my dialing codes map.
Does it have this key Singapore?
I'm expecting that the has function should return true.
Okay.
So does the has function return true?
It should do because Singapore is present as a key.
And it, it is.
If I check for the UK, does my map have the UK key?
No. So I'm expecting that to be false.
The UK is not one of the present keys in here.
So call the has function, which retains true or false
and then check whether it was true or whether it was false.
Okay. Let's check in whether a key exists.
If you wanna check the value
then you can do this, call get on the map.
When you call the get function, you pass in a key
and it gives you back the value for that key.
And you can then test if the value is what you
expected it to be.
Let's have a look.
So here's my map of the three countries again.
If I look up the S3 key, Singapore,
get the value associated with that key.
The value is plus six five.
So I'm expecting the value to be plus six five.
That's the dialing code for Singapore.
Correct.
If you, now this is a thing.
If you try to look up a key that doesn't exist in the
in the map class, in JavaScript, it doesn't throw an error
or anything, you know, dramatic.
It just returns undefined.
So if I try to look up a key that doesn't exist
I would expect to get back undefined.
In other words, no, no such key in the map.
Okay. So UK is not present as a key.
If I look it up, I'd get back undefined.
Okay. Right last then in this section, a set.
A set is a unique collection.
You can call the has function on a set to see
if it has an item and then check if the result is, is true.
So here I've created a set, I've added three countries,
South Africa, Norway, Singapore, take my set and does check.
Does my set contain Singapore?
Yes, it does.
So that should return true.
The has function returns true or false.
If I look up a key, that's a value that's present.
It should return true.
And if I look up a,
a value that's not present, then it should return false.
Okay. So using these set related and map
and array related matches, you can test anything you want to
in a collection in modern JavaScript.