Skip to content

Commit 296c466

Browse files
author
Peter Hozak
committed
exercise 04
1 parent e2a931a commit 296c466

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-3
lines changed

08-testing-hooks/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ Move the color logic to a reusable custom hook:
5959

6060
> [src/04](src/04)
6161
62-
Simplify tests to react-testing-library:
62+
Simplify tests with react-testing-library:
6363
- replace `act()`
64-
- replace `modify()`
64+
- replace modification of input value
6565

6666

6767
## 05 react-redux
+20
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 handleChange = (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={handleChange} />
15+
<div className="Main-box" style={{ backgroundColor: color || defaultColor }} />
16+
</div>
17+
)
18+
}
19+
20+
export default Main
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 './04-exercise'
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+
})
+20
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 handleChange = (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={handleChange} />
15+
<div className="Main-box" style={{ backgroundColor: color || defaultColor }} />
16+
</div>
17+
)
18+
}
19+
20+
export default Main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react'
2+
import { render, fireEvent } from '@testing-library/react'
3+
import Main from './04-solution'
4+
5+
test('default color', () => {
6+
const { container: div } = render(<Main />)
7+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('orange')
8+
})
9+
10+
test('modified color', () => {
11+
const { container: div } = render(<Main />)
12+
const input = div.querySelector('input')
13+
expect(input).toHaveProperty('value', '')
14+
15+
fireEvent.change(input, { target: { value: 'green' } })
16+
expect(input).toHaveProperty('value', 'green')
17+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('green')
18+
})

08-testing-hooks/src/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useEffect } from 'react';
22

3-
const prefixes = Array.from(Array(3)).map((_, i) => `0${i + 1}`)
3+
const prefixes = Array.from(Array(4)).map((_, i) => `0${i + 1}`)
44
const links = prefixes.map((i) =>
55
<li key={i}>
66
{i}: <a href={`#${i}-exercise`} id={`${i}-exercise`}>exercise</a>

0 commit comments

Comments
 (0)