Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue): go back to correct view with memory history #25732

Merged
merged 2 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
})