|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { createRouter, createWebHistory } from '@ionic/vue-router'; |
| 4 | +import { |
| 5 | + IonicVue, |
| 6 | + IonPage, |
| 7 | + IonRouterOutlet, |
| 8 | + onIonViewDidEnter, |
| 9 | + onIonViewDidLeave, |
| 10 | + onIonViewWillEnter, |
| 11 | + onIonViewWillLeave, |
| 12 | +} from '@ionic/vue'; |
| 13 | +import { defineComponent, h, onErrorCaptured, ref } from 'vue'; |
| 14 | +import type { ComponentPublicInstance } from 'vue'; |
| 15 | +import { waitForRouter } from './utils'; |
| 16 | +import LifecycleErrorBoundary from '@/views/LifecycleErrorBoundary.vue'; |
| 17 | +import LifecycleErrorPage from '@/views/LifecycleErrorPage.vue'; |
| 18 | + |
| 19 | +const lifecycles = [ |
| 20 | + ['ionViewWillEnter', onIonViewWillEnter], |
| 21 | + ['ionViewDidEnter', onIonViewDidEnter], |
| 22 | + ['ionViewWillLeave', onIonViewWillLeave], |
| 23 | + ['ionViewDidLeave', onIonViewDidLeave], |
| 24 | +] as const; |
| 25 | + |
| 26 | +describe.each(['composition', 'options', 'methods'] as const)('%s lifecycle errors', (api) => { |
| 27 | + describe.each(lifecycles)('%s', (lifecycle, registerHook) => { |
| 28 | + it.each([ |
| 29 | + ['synchronous', 'capture'], |
| 30 | + ['asynchronous', 'capture'], |
| 31 | + ['synchronous', 'app'], |
| 32 | + ['asynchronous', 'app'], |
| 33 | + ])('handles %s errors with the %s handler and completes navigation', async (timing, handler) => { |
| 34 | + const error = new Error(`${api} ${lifecycle} ${timing}`); |
| 35 | + const captured: unknown[] = []; |
| 36 | + const reported: unknown[] = []; |
| 37 | + const sources: (ComponentPublicInstance | null)[] = []; |
| 38 | + const messages = ref<string[]>([]); |
| 39 | + let receiver: ComponentPublicInstance | undefined; |
| 40 | + const fail = () => { throw error; }; |
| 41 | + const hook = timing === 'asynchronous' ? async () => fail() : fail; |
| 42 | + const optionHook = function (this: ComponentPublicInstance) { |
| 43 | + receiver = this; |
| 44 | + return hook(); |
| 45 | + }; |
| 46 | + const Page = defineComponent({ |
| 47 | + name: 'LifecycleErrorPage', |
| 48 | + ...(api === 'options' ? { [lifecycle]: optionHook } : {}), |
| 49 | + ...(api === 'methods' ? { methods: { [lifecycle]: optionHook } } : {}), |
| 50 | + setup() { |
| 51 | + if (api === 'composition') { |
| 52 | + registerHook(hook); |
| 53 | + } |
| 54 | + return { marker: 'error-page' }; |
| 55 | + }, |
| 56 | + render: () => h(IonPage, { 'data-pageid': 'error' }, () => 'Error page'), |
| 57 | + }); |
| 58 | + const OtherPage = defineComponent({ |
| 59 | + render: () => h(IonPage, { 'data-pageid': 'other' }, () => 'Other page'), |
| 60 | + }); |
| 61 | + const Boundary = defineComponent({ |
| 62 | + setup() { |
| 63 | + onErrorCaptured((err, instance) => { |
| 64 | + captured.push(err); |
| 65 | + sources.push(instance); |
| 66 | + if (handler === 'capture') { |
| 67 | + messages.value.push((err as Error).message); |
| 68 | + return false; |
| 69 | + } |
| 70 | + }); |
| 71 | + return () => [h('p', { id: 'errors' }, messages.value.join(', ')), h(IonRouterOutlet)]; |
| 72 | + }, |
| 73 | + }); |
| 74 | + const router = createRouter({ |
| 75 | + history: createWebHistory(), |
| 76 | + routes: [ |
| 77 | + { path: '/', component: OtherPage }, |
| 78 | + { path: '/error', component: Page }, |
| 79 | + { path: '/other', component: OtherPage }, |
| 80 | + ], |
| 81 | + }); |
| 82 | + await router.push('/'); |
| 83 | + await router.isReady(); |
| 84 | + const wrapper = mount(Boundary, { |
| 85 | + global: { |
| 86 | + plugins: [router, [IonicVue, { animated: false }]], |
| 87 | + config: { |
| 88 | + errorHandler(err) { |
| 89 | + reported.push(err); |
| 90 | + messages.value.push((err as Error).message); |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }); |
| 95 | + try { |
| 96 | + await router.push('/error'); |
| 97 | + await waitForRouter(); |
| 98 | + const page = wrapper.findComponent(Page); |
| 99 | + expect(page.classes()).not.toContain('ion-page-invisible'); |
| 100 | + await router.push('/other'); |
| 101 | + await waitForRouter(); |
| 102 | + expect(wrapper.find('[data-pageid="other"]').classes()).not.toContain('ion-page-invisible'); |
| 103 | + expect(captured).toEqual([error]); |
| 104 | + expect(reported).toEqual(handler === 'app' ? [error] : []); |
| 105 | + expect(sources).toEqual([page.vm.$.proxy]); |
| 106 | + expect(wrapper.find('#errors').text()).toBe(error.message); |
| 107 | + if (api !== 'composition') { |
| 108 | + expect(receiver).toBe(page.vm.$.proxy); |
| 109 | + } |
| 110 | + } finally { |
| 111 | + wrapper.unmount(); |
| 112 | + } |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('Lifecycle error reproduction', () => { |
| 118 | + it.each([ |
| 119 | + ['composition', 'false'], |
| 120 | + ['composition', 'true'], |
| 121 | + ['options', 'false'], |
| 122 | + ['options', 'true'], |
| 123 | + ])('captures %s errors with async=%s', async (api, asynchronous) => { |
| 124 | + const router = createRouter({ |
| 125 | + history: createWebHistory(), |
| 126 | + routes: [{ |
| 127 | + path: '/lifecycle-errors', |
| 128 | + component: LifecycleErrorBoundary, |
| 129 | + children: [{ path: '', component: LifecycleErrorPage }], |
| 130 | + }], |
| 131 | + }); |
| 132 | + await router.push(`/lifecycle-errors?api=${api}&async=${asynchronous}`); |
| 133 | + await router.isReady(); |
| 134 | + const wrapper = mount(IonRouterOutlet, { |
| 135 | + global: { plugins: [router, [IonicVue, { animated: false }]] }, |
| 136 | + }); |
| 137 | + try { |
| 138 | + await waitForRouter(); |
| 139 | + expect(wrapper.find('#lifecycle-error').text()).toBe('Ionic lifecycle error'); |
| 140 | + expect(wrapper.find('[data-pageid="lifecycle-error"]').classes()).not.toContain('ion-page-invisible'); |
| 141 | + } finally { |
| 142 | + wrapper.unmount(); |
| 143 | + } |
| 144 | + }); |
| 145 | +}); |
0 commit comments