Skip to content

Commit 951c8c1

Browse files
committed
fix(vue): guard late swipe cancellation
1 parent 643584b commit 951c8c1

2 files changed

Lines changed: 120 additions & 2 deletions

File tree

‎packages/vue/src/components/IonRouterOutlet.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,16 @@ export const IonRouterOutlet = /*@__PURE__*/ defineComponent({
213213
{ pathname: routeInfo.pushedByRoute || "" },
214214
id
215215
);
216-
enteringViewItem.ionPageElement.setAttribute("aria-hidden", "true");
217-
enteringViewItem.ionPageElement.classList.add("ion-page-hidden");
216+
/**
217+
* Navigation can change while the cancelled animation finishes,
218+
* leaving no previous view or mounted page in this outlet.
219+
*/
220+
const enteringEl = enteringViewItem?.ionPageElement;
221+
if (!enteringEl) {
222+
return;
223+
}
224+
enteringEl.setAttribute("aria-hidden", "true");
225+
enteringEl.classList.add("ion-page-hidden");
218226
}
219227
};
220228

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { flushPromises, mount } from '@vue/test-utils';
2+
import { IonicVue, IonPage, IonRouterOutlet } from '@ionic/vue';
3+
import { createRouter, createMemoryHistory } from '@ionic/vue-router';
4+
import { h, nextTick, reactive } from 'vue';
5+
import { routeLocationKey } from 'vue-router';
6+
import { expect, it, vi } from 'vitest';
7+
8+
async function mountOutlet(
9+
enteringView: { ionPageElement?: HTMLElement } | undefined
10+
) {
11+
const routeInfo = { pathname: '/page2', pushedByRoute: '/page1' };
12+
const handleNavigateBack = vi.fn();
13+
const findViewItemByRouteInfo = vi.fn(() => enteringView);
14+
const wrapper = mount(IonRouterOutlet, {
15+
global: {
16+
provide: {
17+
[routeLocationKey as symbol]: reactive({ matched: [], path: '/page2' }),
18+
navManager: {
19+
getCurrentRouteInfo: () => routeInfo,
20+
handleNavigateBack,
21+
},
22+
viewStacks: { findViewItemByRouteInfo, clear: vi.fn() },
23+
},
24+
},
25+
});
26+
await nextTick();
27+
const outlet = wrapper.element as HTMLIonRouterOutletElement;
28+
return { wrapper, outlet, handleNavigateBack, findViewItemByRouteInfo };
29+
}
30+
31+
it.each([
32+
['a missing view', undefined],
33+
['an unmounted page', {}],
34+
] as const)(
35+
'cancelled swipe tolerates %s after navigation changes',
36+
async (_, view) => {
37+
const { wrapper, outlet, handleNavigateBack } = await mountOutlet(view);
38+
try {
39+
expect(() => outlet.swipeHandler!.onEnd(false)).not.toThrow();
40+
expect(handleNavigateBack).not.toHaveBeenCalled();
41+
} finally {
42+
wrapper.unmount();
43+
}
44+
}
45+
);
46+
47+
it('cancelled swipe hides the previous page when it is still mounted', async () => {
48+
const page = document.createElement('div');
49+
const { wrapper, outlet, handleNavigateBack } = await mountOutlet({
50+
ionPageElement: page,
51+
});
52+
try {
53+
outlet.swipeHandler!.onEnd(false);
54+
55+
expect(page.getAttribute('aria-hidden')).toBe('true');
56+
expect(page.classList.contains('ion-page-hidden')).toBe(true);
57+
expect(handleNavigateBack).not.toHaveBeenCalled();
58+
} finally {
59+
wrapper.unmount();
60+
}
61+
});
62+
63+
it('completed swipe still navigates back', async () => {
64+
const { wrapper, outlet, handleNavigateBack, findViewItemByRouteInfo } =
65+
await mountOutlet(undefined);
66+
try {
67+
outlet.swipeHandler!.onEnd(true);
68+
69+
expect(handleNavigateBack).toHaveBeenCalledOnce();
70+
expect(findViewItemByRouteInfo).not.toHaveBeenCalled();
71+
} finally {
72+
wrapper.unmount();
73+
}
74+
});
75+
76+
it('ignores a late cancellation after returning to the root page', async () => {
77+
const router = createRouter({
78+
history: createMemoryHistory(),
79+
routes: [
80+
{ path: '/page1', component: { render: () => h(IonPage) } },
81+
{ path: '/page2', component: { render: () => h(IonPage) } },
82+
],
83+
});
84+
await router.push('/page1');
85+
const wrapper = mount(IonRouterOutlet, {
86+
global: { plugins: [router, [IonicVue, { animated: false }]] },
87+
});
88+
try {
89+
const outlet = wrapper.element as HTMLIonRouterOutletElement;
90+
outlet.commit = vi.fn().mockResolvedValue(true);
91+
await router.push('/page2');
92+
await flushPromises();
93+
const swipeHandler = outlet.swipeHandler!;
94+
expect(swipeHandler.canStart()).toBe(true);
95+
96+
await new Promise<void>((resolve) => {
97+
const removeHook = router.afterEach(() => {
98+
removeHook();
99+
resolve();
100+
});
101+
router.back();
102+
});
103+
await flushPromises();
104+
105+
expect(router.currentRoute.value.path).toBe('/page1');
106+
expect(() => swipeHandler.onEnd(false)).not.toThrow();
107+
} finally {
108+
wrapper.unmount();
109+
}
110+
});

0 commit comments

Comments
 (0)