File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
packages/react-dom/src/__tests__ Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -1295,4 +1295,48 @@ describe('ReactDOMForm', () => {
12951295 assertLog ( [ 'B' ] ) ;
12961296 expect ( container . textContent ) . toBe ( 'B' ) ;
12971297 } ) ;
1298+
1299+
1300+ // @gate enableFormActions
1301+ // @gate enableAsyncActions
1302+ test ( 'useFormState works in StrictMode' , async ( ) => {
1303+ let actionCounter = 0 ;
1304+ async function action ( state , type ) {
1305+ actionCounter ++ ;
1306+
1307+ Scheduler . log ( `Async action started [${ actionCounter } ]` ) ;
1308+ await getText ( `Wait [${ actionCounter } ]` ) ;
1309+
1310+ switch ( type ) {
1311+ case 'increment' :
1312+ return state + 1 ;
1313+ case 'decrement' :
1314+ return state - 1 ;
1315+ default :
1316+ return state ;
1317+ }
1318+ }
1319+
1320+ let dispatch ;
1321+ function App ( ) {
1322+ const [ state , _dispatch , isPending ] = useFormState ( action , 0 ) ;
1323+ dispatch = _dispatch ;
1324+ const pending = isPending ? 'Pending ' : '' ;
1325+ return < Text text = { pending + state } /> ;
1326+ }
1327+
1328+ const root = ReactDOMClient . createRoot ( container ) ;
1329+ await act ( ( ) => root . render ( < React . StrictMode > < App /> </ React . StrictMode > ) ) ;
1330+ assertLog ( [ '0' ] ) ;
1331+ expect ( container . textContent ) . toBe ( '0' ) ;
1332+
1333+ await act ( ( ) => dispatch ( 'increment' ) ) ;
1334+ assertLog ( [ 'Async action started [1]' , 'Pending 0' ] ) ;
1335+ expect ( container . textContent ) . toBe ( 'Pending 0' ) ;
1336+
1337+
1338+ await act ( ( ) => resolveText ( 'Wait [1]' ) ) ;
1339+ assertLog ( [ '1' ] ) ;
1340+ expect ( container . textContent ) . toBe ( '1' ) ;
1341+ } ) ;
12981342} ) ;
You can’t perform that action at this time.
0 commit comments