File tree 9 files changed +138
-4
lines changed
9 files changed +138
-4
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ yarn test
20
20
21
21
- https://reactjs.org/docs/thinking-in-react.html
22
22
- https://reactjs.org/docs/hooks-intro.html
23
- - https://reactjs.org/docs/testing-recipes.html#events
23
+ - https://reactjs.org/docs/testing-recipes.html
24
24
25
25
Questions possible at any time.
26
26
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ const Main = () => {
10
10
11
11
return (
12
12
< div className = "Main" >
13
- < h3 > Adding State </ h3 >
13
+ < h3 > Libraries for testing </ h3 >
14
14
< input placeholder = { defaultColor } value = { color } onChange = { handleChange } />
15
15
< div className = "Main-box" style = { { backgroundColor : color || defaultColor } } />
16
16
</ div >
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ const Main = () => {
10
10
11
11
return (
12
12
< div className = "Main" >
13
- < h3 > Adding State </ h3 >
13
+ < h3 > Libraries for testing </ h3 >
14
14
< input placeholder = { defaultColor } value = { color } onChange = { handleChange } />
15
15
< div className = "Main-box" style = { { backgroundColor : color || defaultColor } } />
16
16
</ div >
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change 1
1
import React , { useState , useEffect } from 'react' ;
2
2
3
- const prefixes = Array . from ( Array ( 4 ) ) . map ( ( _ , i ) => `0${ i + 1 } ` )
3
+ const prefixes = Array . from ( Array ( 5 ) ) . map ( ( _ , i ) => `0${ i + 1 } ` )
4
4
const links = prefixes . map ( ( i ) =>
5
5
< li key = { i } >
6
6
{ i } : < a href = { `#${ i } -exercise` } id = { `${ i } -exercise` } > exercise</ a >
Original file line number Diff line number Diff line change @@ -68,4 +68,5 @@ input {
68
68
display : flex;
69
69
justify-content : center;
70
70
align-items : center;
71
+ transition : 200ms ;
71
72
}
You can’t perform that action at this time.
0 commit comments