11import React from 'react'
22import { createStore } from 'redux'
3- import * as rtl from 'react-testing-library'
3+ import { renderHook } from 'react-hooks -testing-library'
44import { Provider as ProviderMock , useActions } from '../../src/index.js'
55
66describe ( 'React' , ( ) => {
@@ -28,58 +28,29 @@ describe('React', () => {
2828 dispatchedActions = [ ]
2929 } )
3030
31- afterEach ( ( ) => rtl . cleanup ( ) )
32-
3331 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 } /> }
4835 )
4936
50- const bInc1 = container . querySelector ( '#bInc1' )
51-
52- rtl . fireEvent . click ( bInc1 )
37+ result . current ( )
5338
5439 expect ( dispatchedActions ) . toEqual ( [ { type : 'inc1' } ] )
5540 } )
5641
5742 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 } /> }
7650 )
7751
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 ( )
8354
8455 expect ( dispatchedActions ) . toEqual ( [
8556 { type : 'inc1' } ,
@@ -88,31 +59,17 @@ describe('React', () => {
8859 } )
8960
9061 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 } /> }
10969 )
11070
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 ] ( )
11673
11774 expect ( dispatchedActions ) . toEqual ( [
11875 { type : 'inc1' } ,
@@ -133,37 +90,21 @@ describe('React', () => {
13390 const store = createStore ( reducer )
13491 dispatchedActions = [ ]
13592
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 } /> }
158103 )
159104
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 )
167108
168109 expect ( dispatchedActions ) . toEqual ( [
169110 { type : 'adjust' , amount : 1 , isAdd : true } ,
0 commit comments