28
28
TableWithSuspense
29
29
} = composeStories ( stories ) ;
30
30
31
+ const mockCollectionUpdate = jest . fn ( ) ;
32
+
33
+ jest . mock ( 'react' , ( ) => {
34
+ const actual = jest . requireActual ( 'react' ) ;
35
+
36
+ const useSyncExternalStore = ( subscribe , ...args ) => {
37
+ const fn = actual . useCallback ( ( onStoreChange ) => {
38
+ subscribe ( ( ) => {
39
+ mockCollectionUpdate ( ) ;
40
+ onStoreChange ( ) ;
41
+ } ) ;
42
+ } , [ subscribe ] ) ;
43
+
44
+ return actual . useSyncExternalStore ( fn , ...args ) ;
45
+ } ;
46
+
47
+ return actual . use ? { ...actual , useSyncExternalStore} : actual ;
48
+ } ) ;
49
+
31
50
function MyColumn ( props ) {
32
51
return (
33
52
< Column { ...props } >
@@ -211,6 +230,7 @@ describe('Table', () => {
211
230
212
231
beforeEach ( ( ) => {
213
232
jest . useFakeTimers ( ) ;
233
+ mockCollectionUpdate . mockClear ( ) ;
214
234
} ) ;
215
235
216
236
afterEach ( ( ) => {
@@ -2274,6 +2294,8 @@ describe('Table', () => {
2274
2294
2275
2295
let promise = act ( ( ) => button . click ( ) ) ;
2276
2296
2297
+ expect ( button ) . toHaveTextContent ( 'Loading' ) ;
2298
+
2277
2299
rows = tree . getAllByRole ( 'row' ) ;
2278
2300
expect ( rows ) . toHaveLength ( 3 ) ;
2279
2301
expect ( rows [ 1 ] ) . toHaveTextContent ( 'FalconSat' ) ;
@@ -2293,6 +2315,26 @@ describe('Table', () => {
2293
2315
expect ( rows [ 3 ] ) . toHaveTextContent ( 'Trailblazer' ) ;
2294
2316
expect ( rows [ 4 ] ) . toHaveTextContent ( 'RatSat' ) ;
2295
2317
} ) ;
2318
+
2319
+ it ( 'should not render excessively in React Suspense with transitions' , async ( ) => {
2320
+ // Only supported in React 19.
2321
+ if ( ! React . use ) {
2322
+ return ;
2323
+ }
2324
+
2325
+ jest . useRealTimers ( ) ;
2326
+
2327
+ let tree = await act ( ( ) => render ( < TableWithSuspense reactTransition /> ) ) ;
2328
+
2329
+ await act ( ( ) => stories . makePromise ( [ ] ) ) ;
2330
+
2331
+ expect ( mockCollectionUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
2332
+
2333
+ let button = tree . getByRole ( 'button' ) ;
2334
+ await act ( ( ) => button . click ( ) ) ;
2335
+
2336
+ expect ( mockCollectionUpdate ) . toHaveBeenCalledTimes ( 2 ) ;
2337
+ } ) ;
2296
2338
} ) ;
2297
2339
2298
2340
describe ( 'shouldSelectOnPressUp' , ( ) => {
0 commit comments