Skip to content

Commit 342a2e8

Browse files
committed
part 5c
1 parent d5c6389 commit 342a2e8

File tree

2 files changed

+454
-248
lines changed

2 files changed

+454
-248
lines changed

src/content/5/en/part5c.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Tests will be implemented with the same [Jest](http://jestjs.io/) testing librar
1313

1414
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.
1515

16-
Let's install libraries with the command:
16+
Let's install the libraries with the command:
1717

1818
```js
1919
npm install --save-dev @testing-library/react @testing-library/jest-dom jest jest-environment-jsdom @babel/preset-env @babel/preset-react
@@ -62,7 +62,7 @@ const Note = ({ note, toggleImportance }) => {
6262
}
6363
```
6464

65-
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.
6666

6767
### Rendering the component for tests
6868

@@ -95,7 +95,7 @@ After the initial configuration, the test renders the component with the [render
9595
render(<Note note={note} />)
9696
```
9797

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.
9999

100100
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:
101101

@@ -137,7 +137,7 @@ I do not like this way of storing tests and application code in the same directo
137137

138138
### Searching for content in a component
139139

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:
141141

142142
```js
143143
import React from 'react'
@@ -311,7 +311,7 @@ test('clicking the button calls event handler once', async () => {
311311
})
312312
```
313313

314-
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:
315315

316316
```js
317317
const mockHandler = jest.fn()
@@ -338,7 +338,7 @@ The expectation of the test verifies that the <i>mock function</i> has been call
338338
expect(mockHandler.mock.calls).toHaveLength(1)
339339
```
340340

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.
342342

343343
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.
344344

@@ -408,7 +408,7 @@ describe('<Togglable />', () => {
408408
})
409409
```
410410

411-
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.
412412

413413
The first test verifies that the <i>Togglable</i> component renders its child component
414414

@@ -418,7 +418,7 @@ The first test verifies that the <i>Togglable</i> component renders its child co
418418
</div>
419419
```
420420

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.
422422

423423
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:
424424

@@ -493,7 +493,7 @@ const NoteForm = ({ createNote }) => {
493493
export default NoteForm
494494
```
495495

496-
The form works by calling the _createNote_ function it received as props with the details of the new note.
496+
The form works by calling the function received as props _createNote_, with the details of the new note.
497497

498498
The test is as follows:
499499

@@ -681,7 +681,7 @@ const Note = ({ note, toggleImportance }) => {
681681
export default Note
682682
```
683683

684-
the _getByText_ command that the test uses does <i>not</i> find the element
684+
the _getByText_ method that the test uses does <i>not</i> find the element
685685

686686
```js
687687
test('renders content', () => {
@@ -698,25 +698,25 @@ test('renders content', () => {
698698
})
699699
```
700700

701-
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:
702702

703703
```js
704704
const element = screen.getByText(
705705
'Does not work anymore :(', { exact: false }
706706
)
707707
```
708708

709-
or we could use the command _findByText_:
709+
or we could use the _findByText_ method:
710710

711711
```js
712712
const element = await screen.findByText('Does not work anymore :(')
713713
```
714714

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!
716716

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.
718718

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:
720720

721721
```js
722722
test('does not render this', () => {
@@ -748,27 +748,28 @@ The report will tell us the lines of untested code in each component:
748748
![HTML report of the test coverage](../../images/5/19new.png)
749749

750750
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+
751752
</div>
752753

753754
<div class="tasks">
754755

755756
### Exercises 5.13.-5.16.
756757

757-
#### 5.13: Blog list tests, step1
758+
#### 5.13: Blog List Tests, step 1
758759

759760
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.
760761

761762
Add CSS classes to the component to help the testing as necessary.
762763

763-
#### 5.14: Blog list tests, step2
764+
#### 5.14: Blog List Tests, step 2
764765

765766
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.
766767

767-
#### 5.15: Blog list tests, step3
768+
#### 5.15: Blog List Tests, step 3
768769

769770
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.
770771

771-
#### 5.16: Blog list tests, step4
772+
#### 5.16: Blog List Tests, step 4
772773

773774
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.
774775

@@ -787,7 +788,7 @@ We chose to concentrate on making end-to-end tests to test the whole application
787788

788789
### Snapshot testing
789790

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.
791792

792793
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.
793794

0 commit comments

Comments
 (0)