diff --git a/packages/angular/src/common/directives/navigation/stack-controller.ts b/packages/angular/src/common/directives/navigation/stack-controller.ts index 94a7faf0b23..4e5067e1d67 100644 --- a/packages/angular/src/common/directives/navigation/stack-controller.ts +++ b/packages/angular/src/common/directives/navigation/stack-controller.ts @@ -182,6 +182,16 @@ export class StackController { if (leavingView) { const views = this.getStack(leavingView.stackId); const enteringView = views[views.length - 2]; + + /** + * The swipe gesture checks canGoBack when the pointer goes down, but + * only starts the transition once the pointer has moved far enough. + * A navigation that finishes in between can leave nothing to go back to. + */ + if (!enteringView) { + return Promise.resolve(); + } + const customAnimation = enteringView.animationBuilder; return this.wait(() => { diff --git a/packages/angular/test/base/e2e/src/standalone/swipe-back-popped-stack.spec.ts b/packages/angular/test/base/e2e/src/standalone/swipe-back-popped-stack.spec.ts new file mode 100644 index 00000000000..1b6b3b227eb --- /dev/null +++ b/packages/angular/test/base/e2e/src/standalone/swipe-back-popped-stack.spec.ts @@ -0,0 +1,56 @@ +import { expect, test } from '@playwright/test'; +import { ionSwipeToGoBack } from '../../utils/drag-utils'; +import { ionPageVisible, ionPageHidden, ionPageDoesNotExist } from '../../utils/test-utils'; + +test.describe('Swipe Back: Popped Stack', () => { + test.beforeEach(async ({ page }) => { + await page.goto('/standalone/swipe-animated-disabled?ionic:mode=ios'); + }); + + test('should ignore a swipe when a navigation pops the stack before the gesture starts', async ({ page }, testInfo) => { + testInfo.annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31291', + }); + + const pageErrors: Error[] = []; + page.on('pageerror', (error) => pageErrors.push(error)); + + await ionPageVisible(page, 'app-swipe-animated-disabled-main'); + + // Only an animated outlet starts the back transition when the gesture starts. + await page.locator('#toggle-animated').click(); + await expect(page.locator('#animated-value')).toHaveText('true'); + + await page.locator('#swipe-animated-disabled-details').click(); + await ionPageVisible(page, 'app-swipe-animated-disabled-details'); + await ionPageHidden(page, 'app-swipe-animated-disabled-main'); + + const y = page.viewportSize()!.height / 2; + + // The gesture checks that it can go back when the pointer goes down. + await page.mouse.move(5, y); + await page.mouse.down(); + + // Leave nothing to go back to before the pointer passes the gesture threshold. + await page.evaluate(() => window.history.back()); + await ionPageVisible(page, 'app-swipe-animated-disabled-main'); + await ionPageDoesNotExist(page, 'app-swipe-animated-disabled-details'); + + await page.mouse.move(35, y, { steps: 5 }); + await page.mouse.up(); + + await ionPageVisible(page, 'app-swipe-animated-disabled-main'); + expect(pageErrors).toEqual([]); + + // The ignored swipe should not leave the gesture stuck in progress. + await page.locator('#swipe-animated-disabled-details').click(); + await ionPageVisible(page, 'app-swipe-animated-disabled-details'); + await ionPageHidden(page, 'app-swipe-animated-disabled-main'); + + await ionSwipeToGoBack(page, true); + + await ionPageVisible(page, 'app-swipe-animated-disabled-main'); + await ionPageDoesNotExist(page, 'app-swipe-animated-disabled-details'); + }); +});