@@ -1495,6 +1495,53 @@ Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
14951495 expect ( stripAnsi ( '' ) ) . toBe ( '' ) ;
14961496 } ) ;
14971497 } ) ;
1498+
1499+ describe ( 'Memoization' , ( ) => {
1500+ it ( 'should keep action references stable across re-renders' , ( ) => {
1501+ // We pass a stable `isValidPath` so that callbacks that depend on it
1502+ // are not recreated on every render.
1503+ const isValidPath = ( ) => false ;
1504+ const { result, rerender } = renderHook ( ( ) =>
1505+ useTextBuffer ( { viewport, isValidPath } ) ,
1506+ ) ;
1507+
1508+ const initialInsert = result . current . insert ;
1509+ const initialBackspace = result . current . backspace ;
1510+ const initialMove = result . current . move ;
1511+ const initialHandleInput = result . current . handleInput ;
1512+
1513+ rerender ( ) ;
1514+
1515+ expect ( result . current . insert ) . toBe ( initialInsert ) ;
1516+ expect ( result . current . backspace ) . toBe ( initialBackspace ) ;
1517+ expect ( result . current . move ) . toBe ( initialMove ) ;
1518+ expect ( result . current . handleInput ) . toBe ( initialHandleInput ) ;
1519+ } ) ;
1520+
1521+ it ( 'should have memoized actions that operate on the latest state' , ( ) => {
1522+ const isValidPath = ( ) => false ;
1523+ const { result } = renderHook ( ( ) =>
1524+ useTextBuffer ( { viewport, isValidPath } ) ,
1525+ ) ;
1526+
1527+ // Store a reference to the memoized insert function.
1528+ const memoizedInsert = result . current . insert ;
1529+
1530+ // Update the buffer state.
1531+ act ( ( ) => {
1532+ result . current . insert ( 'hello' ) ;
1533+ } ) ;
1534+ expect ( getBufferState ( result ) . text ) . toBe ( 'hello' ) ;
1535+
1536+ // Now, call the original memoized function reference.
1537+ act ( ( ) => {
1538+ memoizedInsert ( ' world' ) ;
1539+ } ) ;
1540+
1541+ // It should have operated on the updated state.
1542+ expect ( getBufferState ( result ) . text ) . toBe ( 'hello world' ) ;
1543+ } ) ;
1544+ } ) ;
14981545} ) ;
14991546
15001547describe ( 'offsetToLogicalPos' , ( ) => {
0 commit comments