@@ -183,6 +183,70 @@ describe('ReactHooks', () => {
183183 expect ( root ) . toFlushAndYield ( [ 'Parent: 1, 2 (dark)' ] ) ;
184184 } ) ;
185185
186+ it ( 'warns about setState second argument' , ( ) => {
187+ const { useState} = React ;
188+
189+ let setCounter ;
190+ function Counter ( ) {
191+ const [ counter , _setCounter ] = useState ( 0 ) ;
192+ setCounter = _setCounter ;
193+
194+ ReactTestRenderer . unstable_yield ( `Count: ${ counter } ` ) ;
195+ return counter ;
196+ }
197+
198+ const root = ReactTestRenderer . create ( null , { unstable_isConcurrent : true } ) ;
199+ root . update ( < Counter /> ) ;
200+ expect ( root ) . toFlushAndYield ( [ 'Count: 0' ] ) ;
201+ expect ( root ) . toMatchRenderedOutput ( '0' ) ;
202+
203+ expect ( ( ) => {
204+ setCounter ( 1 , ( ) => {
205+ throw new Error ( 'Expected to ignore the callback.' ) ;
206+ } ) ;
207+ } ) . toWarnDev (
208+ 'State updates from the useState() and useReducer() Hooks ' +
209+ "don't support the second callback argument. " +
210+ 'To execute a side effect after rendering, ' +
211+ 'declare it in the component body with useEffect().' ,
212+ { withoutStack : true } ,
213+ ) ;
214+ expect ( root ) . toFlushAndYield ( [ 'Count: 1' ] ) ;
215+ expect ( root ) . toMatchRenderedOutput ( '1' ) ;
216+ } ) ;
217+
218+ it ( 'warns about dispatch second argument' , ( ) => {
219+ const { useReducer} = React ;
220+
221+ let dispatch ;
222+ function Counter ( ) {
223+ const [ counter , _dispatch ] = useReducer ( ( s , a ) => a , 0 ) ;
224+ dispatch = _dispatch ;
225+
226+ ReactTestRenderer . unstable_yield ( `Count: ${ counter } ` ) ;
227+ return counter ;
228+ }
229+
230+ const root = ReactTestRenderer . create ( null , { unstable_isConcurrent : true } ) ;
231+ root . update ( < Counter /> ) ;
232+ expect ( root ) . toFlushAndYield ( [ 'Count: 0' ] ) ;
233+ expect ( root ) . toMatchRenderedOutput ( '0' ) ;
234+
235+ expect ( ( ) => {
236+ dispatch ( 1 , ( ) => {
237+ throw new Error ( 'Expected to ignore the callback.' ) ;
238+ } ) ;
239+ } ) . toWarnDev (
240+ 'State updates from the useState() and useReducer() Hooks ' +
241+ "don't support the second callback argument. " +
242+ 'To execute a side effect after rendering, ' +
243+ 'declare it in the component body with useEffect().' ,
244+ { withoutStack : true } ,
245+ ) ;
246+ expect ( root ) . toFlushAndYield ( [ 'Count: 1' ] ) ;
247+ expect ( root ) . toMatchRenderedOutput ( '1' ) ;
248+ } ) ;
249+
186250 it ( 'never bails out if context has changed' , ( ) => {
187251 const { useState, useLayoutEffect, useContext} = React ;
188252
0 commit comments