@@ -6,7 +6,6 @@ import PropTypes from 'prop-types'
6
6
import ReactDOM from 'react-dom'
7
7
import { createStore } from 'redux'
8
8
import { createProvider , connect } from '../../src/index.js'
9
- import { TestRenderer , enzyme } from '../getTestDeps.js'
10
9
import * as rtl from 'react-testing-library'
11
10
import 'jest-dom/extend-expect'
12
11
@@ -1578,19 +1577,24 @@ describe('React', () => {
1578
1577
const decorator = connect ( state => state )
1579
1578
const Decorated = decorator ( Container )
1580
1579
1581
- const testRenderer = enzyme . mount (
1580
+ class Wrapper extends Component {
1581
+ render ( ) {
1582
+ return (
1583
+ < Decorated ref = { comp => comp . getWrappedInstance ( ) } />
1584
+ )
1585
+ }
1586
+ }
1587
+
1588
+ expect ( ( ) => rtl . render (
1582
1589
< ProviderMock store = { store } >
1583
- < Decorated />
1590
+ < Wrapper />
1584
1591
</ ProviderMock >
1585
- )
1586
-
1587
- const decorated = testRenderer . find ( Decorated )
1588
- expect ( ( ) => decorated . instance ( ) . getWrappedInstance ( ) ) . toThrow (
1592
+ ) ) . toThrow (
1589
1593
/ T o a c c e s s t h e w r a p p e d i n s t a n c e , y o u n e e d t o s p e c i f y \{ w i t h R e f : t r u e \} i n t h e o p t i o n s a r g u m e n t o f t h e c o n n e c t \( \) c a l l \. /
1590
1594
)
1591
1595
} )
1592
1596
1593
- it ( 'should return the instance of the wrapped component for use in calling child methods' , ( ) => {
1597
+ it ( 'should return the instance of the wrapped component for use in calling child methods' , async ( done ) => {
1594
1598
const store = createStore ( ( ) => ( { } ) )
1595
1599
1596
1600
const someData = {
@@ -1603,24 +1607,36 @@ describe('React', () => {
1603
1607
}
1604
1608
1605
1609
render ( ) {
1606
- return < Passthrough />
1610
+ return < Passthrough loaded = "yes" />
1607
1611
}
1608
1612
}
1609
1613
1610
1614
const decorator = connect ( state => state , null , null , { withRef : true } )
1611
1615
const Decorated = decorator ( Container )
1612
1616
1613
- const testRenderer = enzyme . mount (
1617
+ let ref
1618
+
1619
+ class Wrapper extends Component {
1620
+ render ( ) {
1621
+ return (
1622
+ < Decorated ref = { comp => {
1623
+ if ( ! comp ) return
1624
+ ref = comp . getWrappedInstance ( )
1625
+ } } />
1626
+ )
1627
+ }
1628
+ }
1629
+
1630
+ const tester = rtl . render (
1614
1631
< ProviderMock store = { store } >
1615
- < Decorated />
1632
+ < Wrapper />
1616
1633
</ ProviderMock >
1617
1634
)
1618
1635
1619
- const decorated = testRenderer . find ( Decorated )
1636
+ await rtl . waitForElement ( ( ) => tester . getByTestId ( 'loaded' ) )
1620
1637
1621
- expect ( ( ) => decorated . someInstanceMethod ( ) ) . toThrow ( )
1622
- expect ( decorated . instance ( ) . getWrappedInstance ( ) . someInstanceMethod ( ) ) . toBe ( someData )
1623
- expect ( decorated . instance ( ) . wrappedInstance . someInstanceMethod ( ) ) . toBe ( someData )
1638
+ expect ( ref . someInstanceMethod ( ) ) . toBe ( someData )
1639
+ done ( )
1624
1640
} )
1625
1641
1626
1642
it ( 'should wrap impure components without supressing updates' , ( ) => {
@@ -1639,10 +1655,12 @@ describe('React', () => {
1639
1655
const decorator = connect ( state => state , null , null , { pure : false } )
1640
1656
const Decorated = decorator ( ImpureComponent )
1641
1657
1658
+ let externalSetState
1642
1659
class StatefulWrapper extends Component {
1643
1660
constructor ( ) {
1644
1661
super ( )
1645
1662
this . state = { value : 0 }
1663
+ externalSetState = this . setState . bind ( this )
1646
1664
}
1647
1665
1648
1666
getChildContext ( ) {
@@ -1660,18 +1678,15 @@ describe('React', () => {
1660
1678
statefulValue : PropTypes . number
1661
1679
}
1662
1680
1663
- const testRenderer = enzyme . mount (
1681
+ const tester = rtl . render (
1664
1682
< ProviderMock store = { store } >
1665
1683
< StatefulWrapper />
1666
1684
</ ProviderMock >
1667
1685
)
1668
1686
1669
- const target = testRenderer . find ( Passthrough )
1670
- const wrapper = testRenderer . find ( StatefulWrapper ) . instance ( )
1671
- expect ( target . prop ( 'statefulValue' ) ) . toEqual ( 0 )
1672
- wrapper . setState ( { value : 1 } )
1673
- testRenderer . update ( )
1674
- expect ( testRenderer . find ( Passthrough ) . prop ( 'statefulValue' ) ) . toEqual ( 1 )
1687
+ expect ( tester . getByTestId ( 'statefulValue' ) ) . toHaveTextContent ( '0' )
1688
+ externalSetState ( { value : 1 } )
1689
+ expect ( tester . getByTestId ( 'statefulValue' ) ) . toHaveTextContent ( '1' )
1675
1690
} )
1676
1691
1677
1692
it ( 'calls mapState and mapDispatch for impure components' , ( ) => {
@@ -1700,41 +1715,40 @@ describe('React', () => {
1700
1715
)
1701
1716
const Decorated = decorator ( ImpureComponent )
1702
1717
1718
+ let externalSetState
1719
+ let storeGetter
1703
1720
class StatefulWrapper extends Component {
1704
1721
constructor ( ) {
1705
1722
super ( )
1723
+ storeGetter = { storeKey : 'foo' }
1706
1724
this . state = {
1707
- storeGetter : { storeKey : 'foo' }
1725
+ storeGetter
1708
1726
}
1727
+ externalSetState = this . setState . bind ( this )
1709
1728
}
1710
1729
render ( ) {
1711
1730
return < Decorated storeGetter = { this . state . storeGetter } />
1712
1731
}
1713
1732
}
1714
1733
1715
1734
1716
- const testRenderer = enzyme . mount (
1735
+ const tester = rtl . render (
1717
1736
< ProviderMock store = { store } >
1718
1737
< StatefulWrapper />
1719
1738
</ ProviderMock >
1720
1739
)
1721
1740
1722
- const target = testRenderer . find ( Passthrough )
1723
- const wrapper = testRenderer . find ( StatefulWrapper ) . instance ( )
1724
-
1725
1741
expect ( mapStateSpy ) . toHaveBeenCalledTimes ( 2 )
1726
1742
expect ( mapDispatchSpy ) . toHaveBeenCalledTimes ( 2 )
1727
- expect ( target . prop ( 'statefulValue' ) ) . toEqual ( 'foo' )
1743
+ expect ( tester . getByTestId ( 'statefulValue' ) ) . toHaveTextContent ( 'foo' )
1728
1744
1729
1745
// Impure update
1730
- const storeGetter = wrapper . state . storeGetter
1731
1746
storeGetter . storeKey = 'bar'
1732
- wrapper . setState ( { storeGetter } )
1733
- testRenderer . update ( )
1747
+ externalSetState ( { storeGetter } )
1734
1748
1735
1749
expect ( mapStateSpy ) . toHaveBeenCalledTimes ( 3 )
1736
1750
expect ( mapDispatchSpy ) . toHaveBeenCalledTimes ( 3 )
1737
- expect ( testRenderer . find ( Passthrough ) . prop ( 'statefulValue' ) ) . toEqual ( 'bar' )
1751
+ expect ( tester . getByTestId ( 'statefulValue' ) ) . toHaveTextContent ( 'bar' )
1738
1752
} )
1739
1753
1740
1754
it ( 'should pass state consistently to mapState' , ( ) => {
@@ -1772,7 +1786,7 @@ describe('React', () => {
1772
1786
}
1773
1787
}
1774
1788
1775
- const testRenderer = enzyme . mount (
1789
+ const tester = rtl . render (
1776
1790
< ProviderMock store = { store } >
1777
1791
< Container />
1778
1792
</ ProviderMock >
@@ -1787,8 +1801,8 @@ describe('React', () => {
1787
1801
expect ( childMapStateInvokes ) . toBe ( 2 )
1788
1802
1789
1803
// setState calls DOM handlers are batched
1790
- const button = testRenderer . find ( 'button ')
1791
- button . prop ( 'onClick' ) ( )
1804
+ const button = tester . getByText ( 'change ')
1805
+ rtl . fireEvent . click ( button )
1792
1806
expect ( childMapStateInvokes ) . toBe ( 3 )
1793
1807
1794
1808
store . dispatch ( { type : 'APPEND' , body : 'd' } )
@@ -1811,7 +1825,7 @@ describe('React', () => {
1811
1825
}
1812
1826
}
1813
1827
1814
- enzyme . mount (
1828
+ rtl . render (
1815
1829
< ProviderMock store = { store } >
1816
1830
< Container />
1817
1831
</ ProviderMock >
@@ -1844,7 +1858,7 @@ describe('React', () => {
1844
1858
}
1845
1859
}
1846
1860
1847
- enzyme . mount (
1861
+ rtl . render (
1848
1862
< ProviderMock store = { store } >
1849
1863
< Container />
1850
1864
</ ProviderMock >
@@ -1894,7 +1908,7 @@ describe('React', () => {
1894
1908
}
1895
1909
}
1896
1910
1897
- enzyme . mount (
1911
+ rtl . render (
1898
1912
< ProviderMock store = { store } >
1899
1913
< Container />
1900
1914
</ ProviderMock >
@@ -1937,7 +1951,7 @@ describe('React', () => {
1937
1951
}
1938
1952
}
1939
1953
1940
- enzyme . mount (
1954
+ rtl . render (
1941
1955
< ProviderMock store = { store } >
1942
1956
< div >
1943
1957
< Container name = "a" />
@@ -1973,7 +1987,7 @@ describe('React', () => {
1973
1987
}
1974
1988
}
1975
1989
1976
- enzyme . mount (
1990
+ rtl . render (
1977
1991
< ProviderMock store = { store } >
1978
1992
< div >
1979
1993
< Container name = "a" />
@@ -2037,7 +2051,7 @@ describe('React', () => {
2037
2051
}
2038
2052
}
2039
2053
2040
- enzyme . mount (
2054
+ rtl . render (
2041
2055
< ProviderMock store = { store } >
2042
2056
< Container />
2043
2057
</ ProviderMock >
@@ -2061,7 +2075,7 @@ describe('React', () => {
2061
2075
}
2062
2076
}
2063
2077
2064
- enzyme . mount (
2078
+ rtl . render (
2065
2079
< ProviderMock store = { store } >
2066
2080
< Container />
2067
2081
</ ProviderMock >
@@ -2097,7 +2111,7 @@ describe('React', () => {
2097
2111
}
2098
2112
}
2099
2113
2100
- enzyme . mount (
2114
+ rtl . render (
2101
2115
< ProviderMock store = { store } >
2102
2116
< Parent >
2103
2117
< Container />
@@ -2176,7 +2190,7 @@ describe('React', () => {
2176
2190
}
2177
2191
}
2178
2192
2179
- enzyme . mount (
2193
+ rtl . render (
2180
2194
< ProviderMock store = { store } >
2181
2195
< ImpureComponent />
2182
2196
</ ProviderMock >
@@ -2192,7 +2206,7 @@ describe('React', () => {
2192
2206
const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
2193
2207
2194
2208
try {
2195
- enzyme . mount (
2209
+ rtl . render (
2196
2210
< ProviderMock store = { store } >
2197
2211
< Component pass = "through" />
2198
2212
</ ProviderMock >
@@ -2253,7 +2267,7 @@ describe('React', () => {
2253
2267
}
2254
2268
2255
2269
const store = createStore ( ( state = 0 , action ) => ( action . type === 'INC' ? state + 1 : state ) )
2256
- enzyme . mount ( < ProviderMock store = { store } > < Parent /> </ ProviderMock > )
2270
+ rtl . render ( < ProviderMock store = { store } > < Parent /> </ ProviderMock > )
2257
2271
2258
2272
expect ( mapStateToProps ) . toHaveBeenCalledTimes ( 1 )
2259
2273
store . dispatch ( { type : 'INC' } )
@@ -2275,7 +2289,7 @@ describe('React', () => {
2275
2289
class C extends React . Component { render ( ) { return < div > { this . props . count } </ div > } }
2276
2290
2277
2291
const store = createStore ( ( state = 0 , action ) => ( action . type === 'INC' ? state += 1 : state ) )
2278
- enzyme . mount ( < ProviderMock store = { store } > < A /> </ ProviderMock > )
2292
+ rtl . render ( < ProviderMock store = { store } > < A /> </ ProviderMock > )
2279
2293
2280
2294
store . dispatch ( { type : 'INC' } )
2281
2295
} )
@@ -2307,7 +2321,7 @@ describe('React', () => {
2307
2321
render ( ) { return < div > { this . props . count } </ div > }
2308
2322
}
2309
2323
2310
- enzyme . mount ( < ProviderMock store = { store1 } > < A /> </ ProviderMock > )
2324
+ rtl . render ( < ProviderMock store = { store1 } > < A /> </ ProviderMock > )
2311
2325
expect ( mapStateToPropsB ) . toHaveBeenCalledTimes ( 1 )
2312
2326
expect ( mapStateToPropsC ) . toHaveBeenCalledTimes ( 1 )
2313
2327
expect ( mapStateToPropsD ) . toHaveBeenCalledTimes ( 1 )
@@ -2337,7 +2351,7 @@ describe('React', () => {
2337
2351
}
2338
2352
}
2339
2353
2340
- TestRenderer . create (
2354
+ rtl . render (
2341
2355
< React . StrictMode >
2342
2356
< ProviderMock store = { store } >
2343
2357
< Container />
@@ -2350,6 +2364,7 @@ describe('React', () => {
2350
2364
2351
2365
it ( 'should receive the store in the context using a custom store key' , ( ) => {
2352
2366
const store = createStore ( ( ) => ( { } ) )
2367
+ store . dispatch . mine = 'hi'
2353
2368
const CustomProvider = createProvider ( 'customStoreKey' )
2354
2369
const connectOptions = { storeKey : 'customStoreKey' }
2355
2370
@@ -2360,14 +2375,13 @@ describe('React', () => {
2360
2375
}
2361
2376
}
2362
2377
2363
- const testRenderer = enzyme . mount (
2378
+ const tester = rtl . render (
2364
2379
< CustomProvider store = { store } >
2365
2380
< Container />
2366
2381
</ CustomProvider >
2367
2382
)
2368
2383
2369
- const container = testRenderer . find ( Container )
2370
- expect ( container . instance ( ) . store ) . toBe ( store )
2384
+ expect ( tester . getByTestId ( 'dispatch' ) ) . toHaveTextContent ( '[my function dispatch]' )
2371
2385
} )
2372
2386
} )
2373
2387
} )
0 commit comments