Skip to content

Commit b096353

Browse files
author
Peter Hozak
committed
add tests for 01
1 parent 7715320 commit b096353

File tree

7 files changed

+203
-16
lines changed

7 files changed

+203
-16
lines changed

08-testing-hooks/README.md

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,62 @@ yarn
1111
yarn start
1212
```
1313

14+
And in a separate terminal window:
15+
```
16+
yarn test
17+
```
18+
1419
## 00 introduction
1520

21+
- https://reactjs.org/docs/thinking-in-react.html
1622
- https://reactjs.org/docs/hooks-intro.html
23+
- https://reactjs.org/docs/testing-recipes.html#events
24+
25+
## 01 testing useState
26+
> [src/01](src/01)
1727
18-
> Following is a list of TODO items, not course content yet:
28+
Starting with a stateless component with a test:
29+
- add state to component - render background color based on the input value
30+
- update test
1931

20-
## 01 testing useState (exercise)
2132

22-
- code+test without useState
23-
- add state to component
24-
- add test
33+
## 02 TDD
34+
> [src/02](src/02)
2535
26-
## 02 low priority for unit-testing effects (demo)
36+
Starting with the above solution and boilerplate code, display the last valid color name:
37+
- add validation test
38+
- implement validation
39+
- update component tests
40+
- update component
41+
42+
## 03 low priority for unit-testing effects
43+
> [src/03](src/03)
2744
2845
- code+test without state or effect
2946
- add effect that modifies state after fetching data
3047
- test 2 states (initial render, with data) without unit-testing the effect itself
3148

32-
## 03 TDD (exercise)
33-
34-
- code+test without state, dummy event handler
35-
- add test of the event
36-
- implement event handler
37-
38-
## 04 custom hooks (demo)
49+
## 04 custom hooks
50+
> [src/04](src/04)
3951
4052
- code of component with lots of logic + test for just initial render
4153
- move to a new custom hook
4254
- add test for the hook, using dummy component
4355

44-
## 05 testing libraries (overview)
56+
## 05 testing libraries
57+
> [src/05](src/05)
4558
4659
- links to useful libraries (disclaimer whether or not I had time to try them)
4760

48-
## 06 react-redux (demo)
61+
## 06 react-redux
62+
> [src/06](src/06)
4963
5064
- existing actions, reducer, ... and simple tests for them
5165
- add useDispatch, useSelector to component
5266
- mock the selectors, use dummy dispatch and connect
5367

54-
## 07 add a feature using redux (exercise or homework)
68+
## 07 add a feature using redux
69+
> [src/07](src/07)
5570
5671
- working solution from 06
5772
- add new JSON property to data
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
import { render, unmountComponentAtNode } from 'react-dom'
3+
import { act } from "react-dom/test-utils"
4+
import Main from './01-exercise'
5+
6+
let div
7+
beforeEach(() => {
8+
div = document.createElement('div')
9+
})
10+
afterEach(() => {
11+
unmountComponentAtNode(div)
12+
})
13+
14+
test('default color', () => {
15+
act(() => {
16+
render(<Main />, div)
17+
})
18+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('orange')
19+
})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react'
2+
import { render, unmountComponentAtNode } from 'react-dom'
3+
import { act } from "react-dom/test-utils"
4+
import Main from './01-solution'
5+
6+
let div
7+
beforeEach(() => {
8+
div = document.createElement('div')
9+
document.body.appendChild(div)
10+
})
11+
afterEach(() => {
12+
unmountComponentAtNode(div)
13+
div.remove()
14+
})
15+
16+
test('default color', () => {
17+
act(() => {
18+
render(<Main />, div)
19+
})
20+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('orange')
21+
})
22+
23+
test('modified color', () => {
24+
act(() => {
25+
render(<Main />, div)
26+
})
27+
const input = div.querySelector('input')
28+
expect(input).toHaveProperty('value', '')
29+
act(() => {
30+
// input.value = "green" does not work in React 16, see https://stackoverflow.com/a/46012210/1176601
31+
const inputValueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set;
32+
inputValueSetter.call(input, 'green');
33+
input.dispatchEvent(new Event('input', { bubbles: true }))
34+
})
35+
expect(input).toHaveProperty('value', 'green')
36+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('green')
37+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { useState } from 'react'
2+
3+
const Main = () => {
4+
const defaultColor = 'orange'
5+
6+
const [color, setColor] = useState('')
7+
const handleHange = (event) => {
8+
setColor(event.target.value)
9+
}
10+
11+
return (
12+
<div className="Main">
13+
<h3>Adding State</h3>
14+
<input placeholder={defaultColor} value={color} onChange={handleHange} />
15+
<div className="Main-box" style={{ backgroundColor: color || defaultColor }} />
16+
</div>
17+
)
18+
}
19+
20+
export default Main
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react'
2+
import { render, unmountComponentAtNode } from 'react-dom'
3+
import { act } from "react-dom/test-utils"
4+
import Main from './02-exercise'
5+
6+
describe('component', () => {
7+
let div
8+
beforeEach(() => {
9+
div = document.createElement('div')
10+
document.body.appendChild(div)
11+
})
12+
afterEach(() => {
13+
unmountComponentAtNode(div)
14+
div.remove()
15+
})
16+
17+
test('default color', () => {
18+
act(() => {
19+
render(<Main />, div)
20+
})
21+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('orange')
22+
});
23+
24+
test('modified color', () => {
25+
act(() => {
26+
render(<Main />, div)
27+
})
28+
const input = div.querySelector('input')
29+
expect(input).toHaveProperty('value', '')
30+
act(() => {
31+
// input.value = "green" does not work in React 16, see https://stackoverflow.com/a/46012210/1176601
32+
const inputValueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set;
33+
inputValueSetter.call(input, 'green');
34+
input.dispatchEvent(new Event('input', { bubbles: true }))
35+
})
36+
expect(input).toHaveProperty('value', 'green')
37+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('green')
38+
})
39+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { useState } from 'react'
2+
3+
const Main = () => {
4+
const defaultColor = 'orange'
5+
6+
const [color, setColor] = useState('')
7+
const handleHange = (event) => {
8+
setColor(event.target.value)
9+
}
10+
11+
return (
12+
<div className="Main">
13+
<h3>Adding State</h3>
14+
<input placeholder={defaultColor} value={color} onChange={handleHange} />
15+
<div className="Main-box" style={{ backgroundColor: color || defaultColor }} />
16+
</div>
17+
)
18+
}
19+
20+
export default Main
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react'
2+
import { render, unmountComponentAtNode } from 'react-dom'
3+
import { act } from "react-dom/test-utils"
4+
import Main from './02-solution'
5+
6+
let div
7+
beforeEach(() => {
8+
div = document.createElement('div')
9+
document.body.appendChild(div)
10+
})
11+
afterEach(() => {
12+
unmountComponentAtNode(div)
13+
div.remove()
14+
})
15+
16+
test('default color', () => {
17+
act(() => {
18+
render(<Main />, div)
19+
})
20+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('orange')
21+
});
22+
23+
test('modified color', () => {
24+
act(() => {
25+
render(<Main />, div)
26+
})
27+
const input = div.querySelector('input')
28+
expect(input).toHaveProperty('value', '')
29+
act(() => {
30+
// input.value = "green" does not work in React 16, see https://stackoverflow.com/a/46012210/1176601
31+
const inputValueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set;
32+
inputValueSetter.call(input, 'green');
33+
input.dispatchEvent(new Event('input', { bubbles: true }))
34+
})
35+
expect(input).toHaveProperty('value', 'green')
36+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('green')
37+
});

0 commit comments

Comments
 (0)