1
1
import React from 'react'
2
2
import { createStore } from 'redux'
3
- import * as rtl from 'react-testing-library'
3
+ import { renderHook } from 'react-hooks -testing-library'
4
4
import { Provider as ProviderMock , useActions } from '../../src/index.js'
5
5
6
6
describe ( 'React' , ( ) => {
@@ -28,58 +28,29 @@ describe('React', () => {
28
28
dispatchedActions = [ ]
29
29
} )
30
30
31
- afterEach ( ( ) => rtl . cleanup ( ) )
32
-
33
31
it ( 'supports a single action creator' , ( ) => {
34
- const Comp = ( ) => {
35
- const inc1 = useActions ( ( ) => ( { type : 'inc1' } ) )
36
-
37
- return (
38
- < >
39
- < button id = "bInc1" onClick = { inc1 } />
40
- </ >
41
- )
42
- }
43
-
44
- const { container } = rtl . render (
45
- < ProviderMock store = { store } >
46
- < Comp />
47
- </ ProviderMock >
32
+ const { result } = renderHook (
33
+ ( ) => useActions ( ( ) => ( { type : 'inc1' } ) ) ,
34
+ { wrapper : props => < ProviderMock { ...props } store = { store } /> }
48
35
)
49
36
50
- const bInc1 = container . querySelector ( '#bInc1' )
51
-
52
- rtl . fireEvent . click ( bInc1 )
37
+ result . current ( )
53
38
54
39
expect ( dispatchedActions ) . toEqual ( [ { type : 'inc1' } ] )
55
40
} )
56
41
57
42
it ( 'supports an object of action creators' , ( ) => {
58
- const Comp = ( ) => {
59
- const { inc1, inc2 } = useActions ( {
60
- inc1 : ( ) => ( { type : 'inc1' } ) ,
61
- inc2 : ( ) => ( { type : 'inc' , amount : 2 } )
62
- } )
63
-
64
- return (
65
- < >
66
- < button id = "bInc1" onClick = { inc1 } />
67
- < button id = "bInc2" onClick = { inc2 } />
68
- </ >
69
- )
70
- }
71
-
72
- const { container } = rtl . render (
73
- < ProviderMock store = { store } >
74
- < Comp />
75
- </ ProviderMock >
43
+ const { result } = renderHook (
44
+ ( ) =>
45
+ useActions ( {
46
+ inc1 : ( ) => ( { type : 'inc1' } ) ,
47
+ inc2 : ( ) => ( { type : 'inc' , amount : 2 } )
48
+ } ) ,
49
+ { wrapper : props => < ProviderMock { ...props } store = { store } /> }
76
50
)
77
51
78
- const bInc1 = container . querySelector ( '#bInc1' )
79
- const bInc2 = container . querySelector ( '#bInc2' )
80
-
81
- rtl . fireEvent . click ( bInc1 )
82
- rtl . fireEvent . click ( bInc2 )
52
+ result . current . inc1 ( )
53
+ result . current . inc2 ( )
83
54
84
55
expect ( dispatchedActions ) . toEqual ( [
85
56
{ type : 'inc1' } ,
@@ -88,31 +59,17 @@ describe('React', () => {
88
59
} )
89
60
90
61
it ( 'supports an array of action creators' , ( ) => {
91
- const Comp = ( ) => {
92
- const [ inc1 , inc2 ] = useActions ( [
93
- ( ) => ( { type : 'inc1' } ) ,
94
- ( ) => ( { type : 'inc' , amount : 2 } )
95
- ] )
96
-
97
- return (
98
- < >
99
- < button id = "bInc1" onClick = { inc1 } />
100
- < button id = "bInc2" onClick = { inc2 } />
101
- </ >
102
- )
103
- }
104
-
105
- const { container } = rtl . render (
106
- < ProviderMock store = { store } >
107
- < Comp />
108
- </ ProviderMock >
62
+ const { result } = renderHook (
63
+ ( ) =>
64
+ useActions ( [
65
+ ( ) => ( { type : 'inc1' } ) ,
66
+ ( ) => ( { type : 'inc' , amount : 2 } )
67
+ ] ) ,
68
+ { wrapper : props => < ProviderMock { ...props } store = { store } /> }
109
69
)
110
70
111
- const bInc1 = container . querySelector ( '#bInc1' )
112
- const bInc2 = container . querySelector ( '#bInc2' )
113
-
114
- rtl . fireEvent . click ( bInc1 )
115
- rtl . fireEvent . click ( bInc2 )
71
+ result . current [ 0 ] ( )
72
+ result . current [ 1 ] ( )
116
73
117
74
expect ( dispatchedActions ) . toEqual ( [
118
75
{ type : 'inc1' } ,
@@ -133,37 +90,21 @@ describe('React', () => {
133
90
const store = createStore ( reducer )
134
91
dispatchedActions = [ ]
135
92
136
- const Comp = ( ) => {
137
- const { adjust } = useActions ( {
138
- adjust : ( amount , isAdd = true ) => ( {
139
- type : 'adjust' ,
140
- amount,
141
- isAdd
142
- } )
143
- } )
144
-
145
- return (
146
- < >
147
- < button id = "bInc1" onClick = { ( ) => adjust ( 1 ) } />
148
- < button id = "bInc2" onClick = { ( ) => adjust ( 2 ) } />
149
- < button id = "bDec1" onClick = { ( ) => adjust ( 1 , false ) } />
150
- </ >
151
- )
152
- }
153
-
154
- const { container } = rtl . render (
155
- < ProviderMock store = { store } >
156
- < Comp />
157
- </ ProviderMock >
93
+ const { result } = renderHook (
94
+ ( ) =>
95
+ useActions ( {
96
+ adjust : ( amount , isAdd = true ) => ( {
97
+ type : 'adjust' ,
98
+ amount,
99
+ isAdd
100
+ } )
101
+ } ) ,
102
+ { wrapper : props => < ProviderMock { ...props } store = { store } /> }
158
103
)
159
104
160
- const bInc1 = container . querySelector ( '#bInc1' )
161
- const bInc2 = container . querySelector ( '#bInc2' )
162
- const bDec1 = container . querySelector ( '#bDec1' )
163
-
164
- rtl . fireEvent . click ( bInc1 )
165
- rtl . fireEvent . click ( bInc2 )
166
- rtl . fireEvent . click ( bDec1 )
105
+ result . current . adjust ( 1 )
106
+ result . current . adjust ( 2 )
107
+ result . current . adjust ( 1 , false )
167
108
168
109
expect ( dispatchedActions ) . toEqual ( [
169
110
{ type : 'adjust' , amount : 1 , isAdd : true } ,
0 commit comments