You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/5/en/part5c.md
+21-20Lines changed: 21 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Tests will be implemented with the same [Jest](http://jestjs.io/) testing librar
13
13
14
14
In addition to Jest, we also need another testing library that will help us render components for testing purposes. The current best option for this is [react-testing-library](https://github.com/testing-library/react-testing-library) which has seen rapid growth in popularity in recent times.
15
15
16
-
Let's install libraries with the command:
16
+
Let's install the libraries with the command:
17
17
18
18
```js
19
19
npm install --save-dev @testing-library/react @testing-library/jest-dom jest jest-environment-jsdom @babel/preset-env @babel/preset-react
Notice that the <i>li</i> element has the [CSS](https://react.dev/learn#adding-styles)classname <i>note</i>, that could be used to access the component in our tests.
65
+
Notice that the <i>li</i> element has the value <i>note</i> for the [CSS](https://react.dev/learn#adding-styles)attribute className, that could be used to access the component in our tests.
66
66
67
67
### Rendering the component for tests
68
68
@@ -95,7 +95,7 @@ After the initial configuration, the test renders the component with the [render
95
95
render(<Note note={note} />)
96
96
```
97
97
98
-
Normally React components are rendered to the <i>DOM</i>. The render method we used renders the components in a format that is suitable for tests without rendering them to the DOM.
98
+
Normally React components are rendered to the [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model). The render method we used renders the components in a format that is suitable for tests without rendering them to the DOM.
99
99
100
100
We can use the object [screen](https://testing-library.com/docs/queries/about#screen) to access the rendered component. We use screen's method [getByText](https://testing-library.com/docs/queries/bytext) to search for an element that has the note content and ensure that it exists:
101
101
@@ -137,7 +137,7 @@ I do not like this way of storing tests and application code in the same directo
137
137
138
138
### Searching for content in a component
139
139
140
-
The react-testing-library package offers many different ways of investigating the content of the component being tested. In reality, the _expect_ in our test is not needed at all
140
+
The react-testing-library package offers many different ways of investigating the content of the component being tested. In reality, the _expect_ in our test is not needed at all:
There are a few interesting things related to this test. The event handler is a [mock](https://facebook.github.io/jest/docs/en/mock-functions.html) function defined with Jest:
314
+
There are a few interesting things related to this test. The event handler is a [mock](https://jestjs.io/docs/mock-functions) function defined with Jest:
315
315
316
316
```js
317
317
constmockHandler=jest.fn()
@@ -338,7 +338,7 @@ The expectation of the test verifies that the <i>mock function</i> has been call
338
338
expect(mockHandler.mock.calls).toHaveLength(1)
339
339
```
340
340
341
-
[Mock objects and functions](https://en.wikipedia.org/wiki/Mock_object) are commonly used stub components in testing that are used for replacing dependencies of the components being tested. Mocks make it possible to return hardcoded responses, and to verify the number of times the mock functions are called and with what parameters.
341
+
[Mock objects and functions](https://en.wikipedia.org/wiki/Mock_object) are commonly used [stub](https://en.wikipedia.org/wiki/Method_stub) components in testing that are used for replacing dependencies of the components being tested. Mocks make it possible to return hardcoded responses, and to verify the number of times the mock functions are called and with what parameters.
342
342
343
343
In our example, the mock function is a perfect choice since it can be easily used for verifying that the method gets called exactly once.
The _beforeEach_ function gets called before each test, which then renders the <i>Togglable</i> component and saves the field _container_ of the return value.
411
+
The _beforeEach_ function gets called before each test, which then renders the <i>Togglable</i> component and saves the field _container_ of the returned value.
412
412
413
413
The first test verifies that the <i>Togglable</i> component renders its child component
414
414
@@ -418,7 +418,7 @@ The first test verifies that the <i>Togglable</i> component renders its child co
418
418
</div>
419
419
```
420
420
421
-
The remaining tests use the [toHaveStyle](https://www.npmjs.com/package/@testing-library/jest-dom#tohavestyle) method to verify that the child component of the <i>Togglable</i> component is not visible initially, by checking that the style of the <i>div</i> element contains _{ display: 'none' }_. Another test verifies that when the button is pressed the component is visible, meaning that the style for hiding the component <i>is no longer</i> assigned to the component.
421
+
The remaining tests use the [toHaveStyle](https://www.npmjs.com/package/@testing-library/jest-dom#tohavestyle) method to verify that the child component of the <i>Togglable</i> component is not visible initially, by checking that the style of the <i>div</i> element contains _{ display: 'none' }_. Another test verifies that when the button is pressed the component is visible, meaning that the style for hiding it <i>is no longer</i> assigned to the component.
422
422
423
423
Let's also add a test that can be used to verify that the visible content can be hidden by clicking the second button of the component:
Command_getByText_ looks for an element that has the **same text** that it has as a parameter, and nothing more. If we want to look for an element that <i>contains</i> the text, we could use an extra option:
701
+
The_getByText_ method looks for an element that has the **same text** that it has as a parameter, and nothing more. If we want to look for an element that <i>contains</i> the text, we could use an extra option:
702
702
703
703
```js
704
704
constelement=screen.getByText(
705
705
'Does not work anymore :(', { exact:false }
706
706
)
707
707
```
708
708
709
-
or we could use the command _findByText_:
709
+
or we could use the _findByText_ method:
710
710
711
711
```js
712
712
constelement=awaitscreen.findByText('Does not work anymore :(')
713
713
```
714
714
715
-
It is important to notice that, unlike the other _ByText_commands, _findByText_ returns a promise!
715
+
It is important to notice that, unlike the other _ByText_methods, _findByText_ returns a promise!
716
716
717
-
There are situations where yet another form of the command _queryByText_ is useful. The command returns the element but <i>it does not cause an exception</i> if the element is not found.
717
+
There are situations where yet another form of the _queryByText_method is useful. The method returns the element but <i>it does not cause an exception</i> if it is not found.
718
718
719
-
We could eg. use the command to ensure that something <i>is not rendered</i> to the component:
719
+
We could eg. use the method to ensure that something <i>is not rendered</i> to the component:
720
720
721
721
```js
722
722
test('does not render this', () => {
@@ -748,27 +748,28 @@ The report will tell us the lines of untested code in each component:
748
748

749
749
750
750
You can find the code for our current application in its entirety in the <i>part5-8</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes-frontend/tree/part5-8).
751
+
751
752
</div>
752
753
753
754
<divclass="tasks">
754
755
755
756
### Exercises 5.13.-5.16.
756
757
757
-
#### 5.13: Blog list tests, step1
758
+
#### 5.13: Blog List Tests, step 1
758
759
759
760
Make a test, which checks that the component displaying a blog renders the blog's title and author, but does not render its URL or number of likes by default.
760
761
761
762
Add CSS classes to the component to help the testing as necessary.
762
763
763
-
#### 5.14: Blog list tests, step2
764
+
#### 5.14: Blog List Tests, step 2
764
765
765
766
Make a test, which checks that the blog's URL and number of likes are shown when the button controlling the shown details has been clicked.
766
767
767
-
#### 5.15: Blog list tests, step3
768
+
#### 5.15: Blog List Tests, step 3
768
769
769
770
Make a test, which ensures that if the <i>like</i> button is clicked twice, the event handler the component received as props is called twice.
770
771
771
-
#### 5.16: Blog list tests, step4
772
+
#### 5.16: Blog List Tests, step 4
772
773
773
774
Make a test for the new blog form. The test should check, that the form calls the event handler it received as props with the right details when a new blog is created.
774
775
@@ -787,7 +788,7 @@ We chose to concentrate on making end-to-end tests to test the whole application
787
788
788
789
### Snapshot testing
789
790
790
-
Jest offers a completely different alternative to "traditional" testing called [snapshot](https://facebook.github.io/jest/docs/en/snapshot-testing.html) testing. The interesting feature of snapshot testing is that developers do not need to define any tests themselves, it is simple enough to adopt snapshot testing.
791
+
Jest offers a completely different alternative to "traditional" testing called [snapshot](https://jestjs.io/docs/snapshot-testing) testing. The interesting feature of snapshot testing is that developers do not need to define any tests themselves, it is simple enough to adopt snapshot testing.
791
792
792
793
The fundamental principle is to compare the HTML code defined by the component after it has changed to the HTML code that existed before it was changed.
0 commit comments