@@ -14,6 +14,7 @@ import { createRoot } from "react-dom/client";
1414import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
1515
1616const state = vi . hoisted ( ( ) => ( {
17+ transitions : new Set < ( event : { data : { closing : boolean } } ) => void > ( ) ,
1718 scopes : new Set < string > ( ) ,
1819 baseDirectory : "" ,
1920 projects : [ ] as string [ ] ,
@@ -74,6 +75,10 @@ vi.mock("react-native", () => ({
7475vi . mock ( "@react-navigation/native" , ( ) => ( {
7576 useNavigation : ( ) => ( {
7677 dispatch : state . dispatch ,
78+ addListener : ( _name : string , listener : ( event : { data : { closing : boolean } } ) => void ) => {
79+ state . transitions . add ( listener ) ;
80+ return ( ) => state . transitions . delete ( listener ) ;
81+ } ,
7782 getState : ( ) =>
7883 state . navigationState ?? {
7984 index : 4 ,
@@ -88,6 +93,7 @@ vi.mock("@react-navigation/native", () => ({
8893 } ) ,
8994 CommonActions : { reset : ( input : unknown ) => input } ,
9095 StackActions : {
96+ popToTop : ( ) => ( { type : "POP_TO_TOP" } ) ,
9197 push : ( name : string , params : unknown ) => ( { type : "PUSH" , payload : { name, params } } ) ,
9298 } ,
9399} ) ) ;
@@ -329,7 +335,9 @@ function mountProjectFlow(
329335 state . navigationState = current ;
330336 state . dispatch . mockImplementation ( ( action ) =>
331337 apply (
332- action . type === "PUSH" || action . type === "SELECT_WORKSPACE_DETAIL"
338+ action . type === "PUSH" ||
339+ action . type === "POP_TO_TOP" ||
340+ action . type === "SELECT_WORKSPACE_DETAIL"
333341 ? action
334342 : CommonActions . reset ( action ) ,
335343 ) ,
@@ -495,6 +503,7 @@ describe("add project source environment", () => {
495503
496504describe ( "local project navigation" , ( ) => {
497505 function setup ( ) {
506+ state . transitions . clear ( ) ;
498507 state . navigationState = undefined ;
499508 state . scopes = new Set ( [ AuthOrchestrationOperateScope ] ) ;
500509 state . baseDirectory = "/tmp/t3-mobile-audit-fixture" ;
@@ -655,6 +664,21 @@ describe("local project navigation", () => {
655664 }
656665 } ) ;
657666
667+ it ( "does not replace a destination opened while the chooser is closing" , async ( ) => {
668+ const { created, projected, submit } = setup ( ) ;
669+ const navigation = mountProjectFlow ( false , false , true ) ;
670+ const submission = submit ( ) ;
671+ created . resolve ( AsyncResult . success ( undefined ) ) ;
672+ projected . resolve ( { environmentId : "environment" , id : "project" } ) ;
673+ await submission ;
674+ expect ( navigation . getState ( ) . routes . map ( ( route ) => route . name ) ) . toEqual ( [ "Home" ] ) ;
675+ navigation . push ( "PullRequest" ) ;
676+ const selected = navigation . getState ( ) ;
677+ for ( const listener of state . transitions ) listener ( { data : { closing : true } } ) ;
678+ expect ( navigation . getState ( ) ) . toBe ( selected ) ;
679+ expect ( state . transitions . size ) . toBe ( 0 ) ;
680+ } ) ;
681+
658682 it . each ( [
659683 { origin : "Thread" , withDraft : true , withChooser : true } ,
660684 { origin : "NewTaskProject" , withDraft : false , withChooser : true } ,
@@ -672,6 +696,10 @@ describe("local project navigation", () => {
672696 expect ( navigation . getState ( ) ) . toBe ( before ) ;
673697 projected . resolve ( { environmentId : "environment" , id : "project" } ) ;
674698 await submission ;
699+ if ( ! withDraft ) {
700+ expect ( navigation . getState ( ) . routes . map ( ( route ) => route . name ) ) . toEqual ( [ "Home" ] ) ;
701+ for ( const listener of state . transitions ) listener ( { data : { closing : true } } ) ;
702+ }
675703 expect ( navigation . getState ( ) . routes . map ( ( route ) => route . name ) ) . toEqual ( [ "Home" , "Thread" ] ) ;
676704 expect ( navigation . getState ( ) . routes [ 1 ] ?. params ) . toEqual ( {
677705 draft : {
0 commit comments