Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,090 --> 00:00:03,360
To dive a bit deeper
2
00:00:03,360 --> 00:00:05,830
let's create a new component.
3
00:00:05,830 --> 00:00:08,703
In the Components folder which I also created now.
4
00:00:09,780 --> 00:00:13,810
And here all create a rather straightforward
5
00:00:13,810 --> 00:00:17,410
dummy component, which I'll name, Greeting.js.
6
00:00:17,410 --> 00:00:19,050
So that's the file name.
7
00:00:19,050 --> 00:00:23,770
And then there I'll create my component function, greeting,
8
00:00:23,770 --> 00:00:26,190
which I'll export here
9
00:00:26,190 --> 00:00:30,550
and in there I'll just return a div with a h2 tag
10
00:00:30,550 --> 00:00:32,910
where I say, "Hello World!"
11
00:00:32,910 --> 00:00:36,157
And below that a paragraph where I say,
12
00:00:36,157 --> 00:00:38,427
"It's good to see you."
13
00:00:39,610 --> 00:00:41,550
So of course, just a dummy component
14
00:00:41,550 --> 00:00:44,010
with some static output here.
15
00:00:44,010 --> 00:00:47,543
No state, no props, nothing fancy for the moment.
16
00:00:48,920 --> 00:00:50,540
Now this greeting component of course
17
00:00:50,540 --> 00:00:54,120
can be used in App.js now, and therefore here
18
00:00:54,120 --> 00:00:58,110
instead of that logo, I'll import greeting
19
00:00:58,110 --> 00:01:01,223
from ./components/Greeting,
20
00:01:02,360 --> 00:01:04,830
and then here, instead of rendering that header
21
00:01:04,830 --> 00:01:08,040
and all that stuff, I'll get rid of that,
22
00:01:08,040 --> 00:01:11,883
and I'll instead render greeting, that component.
23
00:01:13,200 --> 00:01:15,680
This of course implies that our test
24
00:01:15,680 --> 00:01:18,230
in App.test.js will now fail
25
00:01:18,230 --> 00:01:20,620
because we won't find any element
26
00:01:20,620 --> 00:01:23,433
with a text of learn React anymore.
27
00:01:24,330 --> 00:01:26,200
But that's fine because that was just
28
00:01:26,200 --> 00:01:27,880
the starting code anyways
29
00:01:27,880 --> 00:01:30,710
and it's not the code I want here.
30
00:01:30,710 --> 00:01:32,000
Instead for the moment,
31
00:01:32,000 --> 00:01:34,210
I'll comment out this test here
32
00:01:34,210 --> 00:01:37,463
because I don't need a tested App.test.js anymore.
33
00:01:38,370 --> 00:01:42,790
Instead, I wanna now write my own first test
34
00:01:42,790 --> 00:01:45,290
for this custom component we added here,
35
00:01:45,290 --> 00:01:46,623
the greeting component.
36
00:01:47,740 --> 00:01:51,720
And we could write that test an App.test.js
37
00:01:51,720 --> 00:01:54,380
and render the greeting component from there
38
00:01:54,380 --> 00:01:56,610
or render the app component
39
00:01:56,610 --> 00:01:59,900
but the convention is to write this test
40
00:01:59,900 --> 00:02:03,870
as close as possible to the thing you want to test.
41
00:02:03,870 --> 00:02:06,550
So if I wanna test the greeting component,
42
00:02:06,550 --> 00:02:10,710
and for example, test that it's rendering the correct text,
43
00:02:10,710 --> 00:02:15,710
then we should create a separate Greeting.test.js file
44
00:02:16,380 --> 00:02:18,123
and write our test in there.
45
00:02:19,710 --> 00:02:23,800
Now, any here, we write a test by using this test function,
46
00:02:23,800 --> 00:02:26,040
which is globally available.
47
00:02:26,040 --> 00:02:27,640
You don't need to import it,
48
00:02:27,640 --> 00:02:28,953
it's available like this.
49
00:02:29,890 --> 00:02:33,960
And then as we saw, we give this test a description.
50
00:02:33,960 --> 00:02:37,190
Now the text here is up to you, but generally
51
00:02:37,190 --> 00:02:40,980
you wanna briefly describe what your tests does.
52
00:02:40,980 --> 00:02:44,130
And here I wanna test whether we have the hello world
53
00:02:44,130 --> 00:02:45,830
text on the screen.
54
00:02:45,830 --> 00:02:48,350
So of course that's quite similar to what we tested
55
00:02:48,350 --> 00:02:52,253
in App.test.js before, but we will get more fancy later.
56
00:02:53,180 --> 00:02:58,180
So here I'll just say, "renders Hello World as a text."
57
00:02:59,770 --> 00:03:03,890
Then we need to add a second argument to this test function,
58
00:03:03,890 --> 00:03:05,700
which is an anonymous function,
59
00:03:05,700 --> 00:03:08,193
which will contain the actual testing code.
60
00:03:09,670 --> 00:03:14,260
Now in here, we typically wanna do three things
61
00:03:14,260 --> 00:03:16,240
when we write a test.
62
00:03:16,240 --> 00:03:20,330
We wanna write a test by using the three A's.
63
00:03:20,330 --> 00:03:22,980
The first A stands for arrange.
64
00:03:22,980 --> 00:03:24,640
We wanna set up our tests.
65
00:03:24,640 --> 00:03:27,240
For example, we wanna render the component
66
00:03:27,240 --> 00:03:29,300
which we wanna test.
67
00:03:29,300 --> 00:03:32,343
We can also do additional setup work if it's required.
68
00:03:33,210 --> 00:03:34,910
Then we wanna act.
69
00:03:34,910 --> 00:03:39,230
So we wanna do the thing, which we wanna actually test.
70
00:03:39,230 --> 00:03:42,610
For example, if we wanna simulate a button click,
71
00:03:42,610 --> 00:03:45,490
we want to do that as a second step.
72
00:03:45,490 --> 00:03:47,260
It's not something we do here
73
00:03:47,260 --> 00:03:51,520
but it is something that you often will do in some tests,
74
00:03:51,520 --> 00:03:54,313
and it is something which we also will do later.
75
00:03:55,470 --> 00:03:56,740
Last but not least,
76
00:03:56,740 --> 00:03:58,980
we want to assert the results.
77
00:03:58,980 --> 00:04:02,040
So we wanna have a look at the output
78
00:04:02,040 --> 00:04:04,600
that's visible in the browser, for example
79
00:04:04,600 --> 00:04:08,580
and then see if that matches our expectations.
80
00:04:08,580 --> 00:04:10,903
So these are our three A's.
81
00:04:11,850 --> 00:04:15,390
Translated to this test, if we want to arrange,
82
00:04:15,390 --> 00:04:17,743
we wanna render greeting component.
83
00:04:19,010 --> 00:04:22,780
And a for that, just as an App.js,
84
00:04:22,780 --> 00:04:27,213
just as an App.test.js, we wanna import the component
85
00:04:27,213 --> 00:04:29,290
that we do want to test.
86
00:04:29,290 --> 00:04:34,290
So I will import greeting from the sibling Greeting.js file.
87
00:04:35,690 --> 00:04:38,050
And then we can call render,
88
00:04:38,050 --> 00:04:43,050
which you need to import from testing-libraries/react
89
00:04:43,200 --> 00:04:45,580
and you then render your component.
90
00:04:45,580 --> 00:04:49,020
And to be precise here, you pass JSX code.
91
00:04:49,020 --> 00:04:52,623
So you create your component elements, so to say.
92
00:04:54,470 --> 00:04:56,210
That's the arranged part.
93
00:04:56,210 --> 00:04:59,090
Now we could do more here if more is required,
94
00:04:59,090 --> 00:05:02,000
but here, that's all we need.
95
00:05:02,000 --> 00:05:03,690
Then we want to act.
96
00:05:03,690 --> 00:05:06,630
We want to perform the main action that's interesting to us.
97
00:05:06,630 --> 00:05:09,363
And here that's basically nothing.
98
00:05:11,380 --> 00:05:12,580
Last but not least,
99
00:05:12,580 --> 00:05:17,070
we wanna assert and for that, I wanna look
100
00:05:17,070 --> 00:05:20,970
into that virtual Dom, if you want to call it like this,
101
00:05:20,970 --> 00:05:23,920
so that rendered component content.
102
00:05:23,920 --> 00:05:27,030
And since I want to test whether hello world
103
00:05:27,030 --> 00:05:28,450
is rendered as a text,
104
00:05:28,450 --> 00:05:31,490
I want to select an element by that text.
105
00:05:31,490 --> 00:05:33,330
And if we find such an element
106
00:05:33,330 --> 00:05:35,290
we know that the test succeeded.
107
00:05:35,290 --> 00:05:38,193
If we don't find it, we know that the test failed.
108
00:05:39,150 --> 00:05:41,530
For this, we can import screen,
109
00:05:41,530 --> 00:05:43,420
which gives us access to this,
110
00:05:43,420 --> 00:05:47,430
a virtual screen to this virtual Dom, which was rendered
111
00:05:47,430 --> 00:05:49,030
if you want to call it like that
112
00:05:50,118 --> 00:05:53,080
and we can use that screen to then find elements
113
00:05:53,080 --> 00:05:54,480
on that screen.
114
00:05:54,480 --> 00:05:56,860
Now for this, you've got various functions available.
115
00:05:56,860 --> 00:06:01,690
You've get functions, find functions and query functions.
116
00:06:01,690 --> 00:06:05,670
The main difference is when these functions throw errors
117
00:06:05,670 --> 00:06:09,850
and if they return a promise or not.
118
00:06:09,850 --> 00:06:12,860
Get functions, will for example, throw an error
119
00:06:12,860 --> 00:06:14,840
if an element is not found,
120
00:06:14,840 --> 00:06:17,010
query functions won't do that
121
00:06:17,010 --> 00:06:19,840
and find functions will return a promise.
122
00:06:19,840 --> 00:06:23,180
So there it's enough if an element is eventually
123
00:06:23,180 --> 00:06:24,203
on the screen.
124
00:06:25,180 --> 00:06:28,860
Here, I will get an element by text.
125
00:06:28,860 --> 00:06:31,990
So we'll use screen get by text.
126
00:06:31,990 --> 00:06:35,740
And then here we need to define the texts
127
00:06:35,740 --> 00:06:37,420
for which we're looking.
128
00:06:37,420 --> 00:06:40,280
Now, here, we can use a regular expression
129
00:06:40,280 --> 00:06:43,100
as it was the case in App.test.js,
130
00:06:43,100 --> 00:06:45,803
but you can also just, hard-code a string.
131
00:06:47,100 --> 00:06:49,530
It's just a little bit less flexible
132
00:06:49,530 --> 00:06:51,260
than a regular expression.
133
00:06:51,260 --> 00:06:53,180
But if you are looking for a full string
134
00:06:53,180 --> 00:06:55,993
as I'm doing it here, then it's absolutely fine.
135
00:06:57,080 --> 00:06:59,640
You can also pass a second argument here
136
00:06:59,640 --> 00:07:03,740
to get by text and configure if you want an exact match,
137
00:07:03,740 --> 00:07:06,450
which is the default or not.
138
00:07:06,450 --> 00:07:10,140
If you say false here, casing won't matter for example,
139
00:07:10,140 --> 00:07:13,070
and it will also match sub-strings.
140
00:07:13,070 --> 00:07:15,210
Now here, an exact match is okay though,
141
00:07:15,210 --> 00:07:16,600
and since that's the default
142
00:07:16,600 --> 00:07:18,363
we don't need to do anything else.
143
00:07:19,700 --> 00:07:24,060
Now get by text will hopefully return an element.
144
00:07:24,060 --> 00:07:27,200
If it doesn't find an element, it will throw an error.
145
00:07:27,200 --> 00:07:31,880
And therefore here I'll then get my hello world element.
146
00:07:35,410 --> 00:07:38,470
And now we can make the actual assertion.
147
00:07:38,470 --> 00:07:41,983
We can check whether that element exists.
148
00:07:42,860 --> 00:07:46,740
For this we got the globally available expect function
149
00:07:46,740 --> 00:07:50,970
to which we can pass our testing result value
150
00:07:50,970 --> 00:07:54,570
and that can be anything, a number, a string
151
00:07:54,570 --> 00:07:59,033
or like in this case, a Dom node in the end, a HTML element.
152
00:07:59,890 --> 00:08:03,840
And then on this result of this expect function,
153
00:08:03,840 --> 00:08:05,810
we've got various matrix,
154
00:08:05,810 --> 00:08:09,760
like this to be in the document function
155
00:08:09,760 --> 00:08:13,170
which checks whether the thing we expect here,
156
00:08:13,170 --> 00:08:16,563
the HTML element we expect here is in the document.
157
00:08:18,000 --> 00:08:19,600
You can also check for opposites
158
00:08:19,600 --> 00:08:23,340
by adding dot not and then your matching functions
159
00:08:23,340 --> 00:08:25,870
like not to be in the document.
160
00:08:25,870 --> 00:08:28,750
If you want this element not to be in the document
161
00:08:28,750 --> 00:08:30,620
though, in that case you would have to use
162
00:08:30,620 --> 00:08:32,760
the query by a text function,
163
00:08:32,760 --> 00:08:36,623
since get by text would fail if no element would be found.
164
00:08:37,600 --> 00:08:38,990
But that's not what I wanna check here.
165
00:08:38,990 --> 00:08:43,210
Anyways, I just wanna check if this is in the document.
166
00:08:43,210 --> 00:08:47,310
So basically the same test as we had it, an App.test.js
167
00:08:48,580 --> 00:08:51,883
and that's now our first dummy test.
168
00:08:53,410 --> 00:08:56,220
If we now run NPM test again,
169
00:08:56,220 --> 00:08:57,890
this will start up the testing again
170
00:08:57,890 --> 00:09:01,620
and now it also does run all these tests out of the box
171
00:09:01,620 --> 00:09:05,700
and you see that we got two tests
172
00:09:05,700 --> 00:09:07,633
and actually both failed.
173
00:09:08,510 --> 00:09:12,020
Now, App.test.js failed because as we see here,
174
00:09:12,020 --> 00:09:14,240
we must at least have one test,
175
00:09:14,240 --> 00:09:16,013
and that's an empty file right now.
176
00:09:16,940 --> 00:09:19,280
So therefore, to avoid this error,
177
00:09:19,280 --> 00:09:21,563
I will delete App.test.js.
178
00:09:23,070 --> 00:09:26,860
Now it re-runs, but the other test here also fails
179
00:09:26,860 --> 00:09:28,890
because it's unable to find an element
180
00:09:28,890 --> 00:09:30,883
with that text, hello world.
181
00:09:32,930 --> 00:09:33,860
And the reason for that
182
00:09:33,860 --> 00:09:36,300
is that here I have an exclamation mark.
183
00:09:36,300 --> 00:09:39,760
And I did mention that it looks for an exact match here
184
00:09:39,760 --> 00:09:41,640
out of the box.
185
00:09:41,640 --> 00:09:45,110
So we either add exact false here
186
00:09:46,150 --> 00:09:48,870
to be a bit more forgiving,
187
00:09:48,870 --> 00:09:51,733
in which case does test now passes as you see,
188
00:09:52,780 --> 00:09:54,840
or we look for the exact match
189
00:09:54,840 --> 00:09:57,030
by adding the exclamation mark here,
190
00:09:57,030 --> 00:09:59,023
in which case it also passes.
191
00:10:00,500 --> 00:10:04,010
But this is now how we can write our own first test
192
00:10:04,010 --> 00:10:06,730
even though it's basically the same as before
193
00:10:06,730 --> 00:10:09,600
but we learn how we can generally write our tests
194
00:10:09,600 --> 00:10:12,833
and with that knowledge, we can now dive a bit deeper.
15258
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.