Skip to content

Commit

Permalink
fix(vue): go back to correct view with memory history (#25732)
Browse files Browse the repository at this point in the history
resolves #25705
  • Loading branch information
liamdebeasi committed Aug 8, 2022
1 parent 36bea1c commit 8327889
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/vue-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ export const createIonRouter = (opts: IonicVueRouterOptions, router: Router) =>
* will go back in a linear fashion.
*/
prevInfo.pathname === routeInfo.pushedByRoute &&
routeInfo.tab === '' && prevInfo.tab === ''

/**
* Tab info can be undefined or '' (empty string)
* both are false-y values, so we can just use !.
*/
!routeInfo.tab && !prevInfo.tab
)
) {
router.back();
Expand Down
76 changes: 76 additions & 0 deletions packages/vue/test-app/tests/unit/memory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { mount } from '@vue/test-utils';
import { createRouter, createMemoryHistory } from '@ionic/vue-router';
import {
IonContent,
IonHeader,
IonToolbar,
IonBackButton,
IonicVue,
IonApp,
IonRouterOutlet,
IonPage,
} from '@ionic/vue';
import { waitForRouter } from './utils';

const App = {
components: { IonApp, IonRouterOutlet },
template: '<ion-app><ion-router-outlet /></ion-app>',
}

describe('createMemoryHistory', () => {
beforeAll(() => {
(HTMLElement.prototype as HTMLIonRouterOutletElement).commit = jest.fn();
});
it('should not error when going back with memory router', async () => {
const PageTemplate = {
template: `
<ion-page>
<ion-header>
<ion-toolbar>
<ion-back-button></ion-back-button>
</ion-toolbar>
</ion-header>
<ion-content></ion-content>
</ion-page>
`,
components: { IonPage, IonContent, IonHeader, IonToolbar, IonBackButton }
}

const router = createRouter({
history: createMemoryHistory(process.env.BASE_URL),
routes: [
{ path: '/', component: PageTemplate },
{ path: '/page2', component: PageTemplate },
{ path: '/page3', component: PageTemplate }
]
});
const push = jest.spyOn(router, 'back')

router.push('/');
await router.isReady();
const wrapper = mount(App, {
global: {
plugins: [router, IonicVue]
}
});

router.push('/page2');
await waitForRouter();

router.push('/page3');
await waitForRouter();


const backButtons = wrapper.findAllComponents(IonBackButton);
const pageTwoButton = backButtons[1];
const pageThreeButton = backButtons[2];

await pageThreeButton.trigger('click');
await waitForRouter();

await pageTwoButton.trigger('click');
await waitForRouter();

expect(push).toHaveBeenCalledTimes(2);
});
})

0 comments on commit 8327889

Please sign in to comment.