Skip to content

Commit a8a46bb

Browse files
authored
Fix navigateToApp logic when navigating to the current app. (#80809) (#80833)
1 parent a9dfaee commit a8a46bb

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

src/core/public/application/application_service.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,17 @@ export class ApplicationService {
242242
appId,
243243
{ path, state, replace = false }: NavigateToAppOptions = {}
244244
) => {
245-
if (await this.shouldNavigate(overlays)) {
245+
const currentAppId = this.currentAppId$.value;
246+
const navigatingToSameApp = currentAppId === appId;
247+
const shouldNavigate = navigatingToSameApp ? true : await this.shouldNavigate(overlays);
248+
249+
if (shouldNavigate) {
246250
if (path === undefined) {
247251
path = applications$.value.get(appId)?.defaultPath;
248252
}
249-
this.appInternalStates.delete(this.currentAppId$.value!);
253+
if (!navigatingToSameApp) {
254+
this.appInternalStates.delete(this.currentAppId$.value!);
255+
}
250256
this.navigate!(getAppUrl(availableMounters, appId, path), state, replace);
251257
this.currentAppId$.next(appId);
252258
}

src/core/public/application/integration_tests/application_service.test.tsx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,34 @@ describe('ApplicationService', () => {
258258
expect(history.entries.length).toEqual(2);
259259
expect(history.entries[1].pathname).toEqual('/app/app1');
260260
});
261+
262+
it('does not trigger navigation check if navigating to the current app', async () => {
263+
startDeps.overlays.openConfirm.mockResolvedValue(false);
264+
265+
const { register } = service.setup(setupDeps);
266+
267+
register(Symbol(), {
268+
id: 'app1',
269+
title: 'App1',
270+
mount: ({ onAppLeave }: AppMountParameters) => {
271+
onAppLeave((actions) => actions.confirm('confirmation-message', 'confirmation-title'));
272+
return () => undefined;
273+
},
274+
});
275+
276+
const { navigateToApp, getComponent } = await service.start(startDeps);
277+
278+
update = createRenderer(getComponent());
279+
280+
await act(async () => {
281+
await navigate('/app/app1');
282+
await navigateToApp('app1', { path: '/internal-path' });
283+
});
284+
285+
expect(startDeps.overlays.openConfirm).not.toHaveBeenCalled();
286+
expect(history.entries.length).toEqual(3);
287+
expect(history.entries[2].pathname).toEqual('/app/app1/internal-path');
288+
});
261289
});
262290

263291
describe('registering action menus', () => {
@@ -331,6 +359,48 @@ describe('ApplicationService', () => {
331359
expect(await getValue(currentActionMenu$)).toBe(mounter2);
332360
});
333361

362+
it('does not update the observable value when navigating to the current app', async () => {
363+
const { register } = service.setup(setupDeps);
364+
365+
let initialMount = true;
366+
register(Symbol(), {
367+
id: 'app1',
368+
title: 'App1',
369+
mount: async ({ setHeaderActionMenu }: AppMountParameters) => {
370+
if (initialMount) {
371+
setHeaderActionMenu(mounter1);
372+
initialMount = false;
373+
}
374+
return () => undefined;
375+
},
376+
});
377+
378+
const { navigateToApp, getComponent, currentActionMenu$ } = await service.start(startDeps);
379+
update = createRenderer(getComponent());
380+
381+
let mountedMenuCount = 0;
382+
currentActionMenu$.subscribe(() => {
383+
mountedMenuCount++;
384+
});
385+
386+
await act(async () => {
387+
await navigateToApp('app1');
388+
await flushPromises();
389+
});
390+
391+
expect(await getValue(currentActionMenu$)).toBe(mounter1);
392+
393+
await act(async () => {
394+
await navigateToApp('app1');
395+
await flushPromises();
396+
});
397+
398+
expect(await getValue(currentActionMenu$)).toBe(mounter1);
399+
400+
// there is an initial 'undefined' emission
401+
expect(mountedMenuCount).toBe(2);
402+
});
403+
334404
it('updates the observable value to undefined when switching to an application without action menu', async () => {
335405
const { register } = service.setup(setupDeps);
336406

0 commit comments

Comments
 (0)