Skip to content

Commit ece3bfd

Browse files
author
Peter Hozak
committed
exercise 05
1 parent b919fde commit ece3bfd

File tree

9 files changed

+138
-4
lines changed

9 files changed

+138
-4
lines changed

08-testing-hooks/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ yarn test
2020

2121
- https://reactjs.org/docs/thinking-in-react.html
2222
- https://reactjs.org/docs/hooks-intro.html
23-
- https://reactjs.org/docs/testing-recipes.html#events
23+
- https://reactjs.org/docs/testing-recipes.html
2424

2525
Questions possible at any time.
2626

08-testing-hooks/src/04/04-exercise.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Main = () => {
1010

1111
return (
1212
<div className="Main">
13-
<h3>Adding State</h3>
13+
<h3>Libraries for testing</h3>
1414
<input placeholder={defaultColor} value={color} onChange={handleChange} />
1515
<div className="Main-box" style={{ backgroundColor: color || defaultColor }} />
1616
</div>

08-testing-hooks/src/04/04-solution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Main = () => {
1010

1111
return (
1212
<div className="Main">
13-
<h3>Adding State</h3>
13+
<h3>Libraries for testing</h3>
1414
<input placeholder={defaultColor} value={color} onChange={handleChange} />
1515
<div className="Main-box" style={{ backgroundColor: color || defaultColor }} />
1616
</div>
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { useState, useEffect } from 'react'
2+
3+
const Main = () => {
4+
const [color, setColor] = useState('orange')
5+
6+
// useEffect(() => {
7+
// window.fetch('https://jsonplaceholder.typicode.com/users/1').then((data) => {
8+
// setColor('green')
9+
// })
10+
// }, [])
11+
12+
useEffect(() => {
13+
const fetchPromise = window.fetch('https://jsonplaceholder.typicode.com/users/1')
14+
const timeoutPromise = new Promise((resolve, reject) => {
15+
setTimeout(reject, 1000)
16+
})
17+
Promise.race([fetchPromise, timeoutPromise]).then((data) => {
18+
setColor('green')
19+
}).catch(() => {
20+
setColor('red')
21+
})
22+
}, [])
23+
24+
return (
25+
<div className="Main">
26+
<h3>Fetch and timeout</h3>
27+
<div className="Main-box" style={{ backgroundColor: color }} />
28+
</div>
29+
)
30+
}
31+
32+
export default Main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react'
2+
import { render, unmountComponentAtNode } from 'react-dom'
3+
import { act } from "react-dom/test-utils"
4+
import Main from './05-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+
// ???
25+
})
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React, { useState, useEffect } from 'react'
2+
3+
const Main = () => {
4+
const [color, setColor] = useState('orange')
5+
6+
useEffect(() => {
7+
const fetchPromise = window.fetch('https://jsonplaceholder.typicode.com/users/1')
8+
let cancellable
9+
const timeoutPromise = new Promise((resolve, reject) => {
10+
cancellable = setTimeout(reject, 1000)
11+
})
12+
Promise.race([fetchPromise, timeoutPromise]).then((data) => {
13+
setColor('green')
14+
}).catch(() => {
15+
setColor('red')
16+
})
17+
return () => {
18+
clearTimeout(cancellable)
19+
}
20+
}, [])
21+
22+
return (
23+
<div className="Main">
24+
<h3>Fetch and timeout</h3>
25+
<div className="Main-box" style={{ backgroundColor: color }} />
26+
</div>
27+
)
28+
}
29+
30+
export default Main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react'
2+
import { render, unmountComponentAtNode } from 'react-dom'
3+
import { act } from "react-dom/test-utils"
4+
import Main from './05-solution'
5+
6+
jest.useFakeTimers()
7+
8+
let div
9+
beforeEach(() => {
10+
div = document.createElement('div')
11+
document.body.appendChild(div)
12+
})
13+
afterEach(() => {
14+
unmountComponentAtNode(div)
15+
div.remove()
16+
})
17+
18+
test('default color', () => {
19+
act(() => {
20+
render(<Main />, div)
21+
})
22+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('orange')
23+
})
24+
25+
test('success', async () => {
26+
jest.spyOn(global, "fetch").mockImplementation(() =>
27+
Promise.resolve({
28+
json: () => Promise.resolve({ id: 0, name: 'John' })
29+
})
30+
);
31+
32+
await act(async () => {
33+
render(<Main />, div)
34+
})
35+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('green')
36+
37+
global.fetch.mockRestore()
38+
})
39+
40+
test('timeout', async () => {
41+
await act(async () => {
42+
render(<Main />, div)
43+
jest.advanceTimersByTime(1000)
44+
})
45+
expect(div.querySelector('.Main-box').style.backgroundColor).toBe('red')
46+
})

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(4)).map((_, i) => `0${i + 1}`)
3+
const prefixes = Array.from(Array(5)).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>

08-testing-hooks/src/index.css

+1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ input {
6868
display: flex;
6969
justify-content: center;
7070
align-items: center;
71+
transition: 200ms;
7172
}

0 commit comments

Comments
 (0)