@@ -21,6 +21,7 @@ const mocks = vi.hoisted(() => ({
2121 tokenCompletion : null as Promise < boolean > | null ,
2222 onDebug : undefined as NativeReviewDiffViewProps [ "onDebug" ] ,
2323 autoViewport : false ,
24+ contentResetKeyLandsLate : false ,
2425 rows : vi . fn ( ) ,
2526 tokens : vi . fn ( ) ,
2627 highlight : vi . fn ( ) ,
@@ -30,22 +31,30 @@ function RawView({
3031 ref : nativeRef ,
3132 onDebug,
3233 initialFileId,
33- contentResetKey,
34+ contentResetKey = "" ,
3435} : NativeReviewDiffViewProps & { ref : Ref < unknown > } ) {
3536 const [ content , setContent ] = useState ( { resetKey : contentResetKey , rows : "" } ) ;
36- const rows = content . resetKey === contentResetKey ? content . rows : "" ;
37+ // Native setContentResetKey clears rows unless the key is unchanged.
38+ const setContentResetKey = ( resetKey : string ) =>
39+ setContent ( ( current ) => ( current . resetKey === resetKey ? current : { resetKey, rows : "" } ) ) ;
3740 const [ tokenRowIds , setTokenRowIds ] = useState < string [ ] > ( [ ] ) ;
41+ // Android Fabric can apply a prop update after an AsyncFunction call made later.
42+ if ( ! mocks . contentResetKeyLandsLate && content . resetKey !== contentResetKey ) {
43+ setContentResetKey ( contentResetKey ) ;
44+ }
3845 useLayoutEffect ( ( ) => {
3946 mocks . onDebug = onDebug ;
4047 } ) ;
4148 useImperativeHandle ( nativeRef , ( ) => ( {
42- async setRowsJson ( value : string ) {
49+ async setRowsJson ( value : string , resetKey ?: string ) {
4350 mocks . rows ( value ) ;
4451 if ( mocks . registrationFailures > 0 ) {
4552 mocks . registrationFailures -= 1 ;
4653 throw new Error ( "Unable to find the 'T3ReviewDiffSurface' view for this native tag" ) ;
4754 }
48- setContent ( { resetKey : contentResetKey , rows : value } ) ;
55+ if ( resetKey !== undefined ) setContentResetKey ( resetKey ) ;
56+ setContent ( ( current ) => ( { ...current , rows : value } ) ) ;
57+ setContentResetKey ( contentResetKey ) ;
4958 if ( mocks . autoViewport ) {
5059 const decoded = JSON . parse ( value ) as NativeReviewDiffData [ "rows" ] ;
5160 onDebug ?.( {
@@ -79,7 +88,7 @@ function RawView({
7988 } ) ) ;
8089 return (
8190 < div data-token-rows = { tokenRowIds . join ( "," ) } >
82- { rows ? "Rendered diff" : "Blank native surface" }
91+ { content . rows ? "Rendered diff" : "Blank native surface" }
8392 </ div >
8493 ) ;
8594}
@@ -141,6 +150,7 @@ function setup(integrated = false) {
141150 mocks . tokenCompletion = null ;
142151 mocks . onDebug = undefined ;
143152 mocks . autoViewport = false ;
153+ mocks . contentResetKeyLandsLate = false ;
144154 mocks . rows . mockClear ( ) ;
145155 mocks . tokens . mockClear ( ) ;
146156 mocks . highlight
@@ -251,6 +261,20 @@ function setup(integrated = false) {
251261 async mountPullRequest ( targetFileId ?: string ) {
252262 await act ( async ( ) => root . render ( < PullRequestDiff targetFileId = { targetFileId } /> ) ) ;
253263 } ,
264+ async showSection ( contentResetKey : string ) {
265+ await act ( async ( ) =>
266+ root . render (
267+ < NativeDiff
268+ rowsJson = '[{"kind":"line","id":"r0"}]'
269+ contentResetKey = { contentResetKey }
270+ appearanceScheme = "light"
271+ themeJson = "{}"
272+ rowHeight = { 20 }
273+ contentWidth = { 300 }
274+ /> ,
275+ ) ,
276+ ) ;
277+ } ,
254278 async collapseFiles ( fileIds : ReadonlyArray < string > ) {
255279 collapsedFileIds = fileIds ;
256280 await act ( async ( ) => root . render ( < PullRequestDiff /> ) ) ;
@@ -321,6 +345,18 @@ it("retries transient native registration without replacing the diff with an err
321345 expect ( screen . container . textContent ) . toBe ( "Rendered diff" ) ;
322346} ) ;
323347
348+ it ( "renders identical rows after switching sections when the reset key prop lands late" , async ( ) => {
349+ using screen = setup ( true ) ;
350+ await screen . showSection ( "thread:git:changes" ) ;
351+ await screen . deliverPayloads ( ) ;
352+ expect ( screen . container . textContent ) . toBe ( "Rendered diff" ) ;
353+
354+ mocks . contentResetKeyLandsLate = true ;
355+ await screen . showSection ( "thread:git:working-tree" ) ;
356+ await screen . deliverPayloads ( ) ;
357+ expect ( screen . container . textContent ) . toBe ( "Rendered diff" ) ;
358+ } ) ;
359+
324360it . each ( [ false , undefined ] ) (
325361 "keeps colors queued until native accepts them and recovers rejected patches (%s)" ,
326362 async ( rejected ) => {
0 commit comments