|
1 | 1 | /**
|
2 | 2 | * @vitest-environment happy-dom
|
3 | 3 | */
|
4 |
| -import { type App, defineComponent, inject, type Plugin } from 'vue' |
| 4 | +import { |
| 5 | + type App, |
| 6 | + defineComponent, |
| 7 | + h, |
| 8 | + inject, |
| 9 | + nextTick, |
| 10 | + type Plugin, |
| 11 | + ref, |
| 12 | +} from 'vue' |
5 | 13 | import { beforeEach, describe, expect, it, vi } from 'vitest'
|
6 | 14 | import { flushPromises, mount } from '@vue/test-utils'
|
7 | 15 | import { getRouter } from 'vue-router-mock'
|
@@ -1109,6 +1117,58 @@ export function testDefineLoader<Context = void>(
|
1109 | 1117 | it.todo('can be first non-lazy then lazy', async () => {})
|
1110 | 1118 | it.todo('can be first non-lazy then lazy', async () => {})
|
1111 | 1119 |
|
| 1120 | + // https://github.com/posva/unplugin-vue-router/issues/495 |
| 1121 | + // in the issue above we have one page with a loader |
| 1122 | + // this page is conditionally rendered based on an error state |
| 1123 | + // when resetting the error state, there is also a duplicated navigation |
| 1124 | + // that invalidates any pendingLoad and renders the page again |
| 1125 | + // since there is no navigation, loaders are not called again and |
| 1126 | + // there is no pendingLoad |
| 1127 | + it('gracefully handles a loader without a pendingLoad', async () => { |
| 1128 | + const l1 = mockedLoader({ lazy: false, key: 'l1' }) |
| 1129 | + const router = getRouter() |
| 1130 | + router.addRoute({ |
| 1131 | + name: 'a', |
| 1132 | + path: '/a', |
| 1133 | + component: defineComponent({ |
| 1134 | + setup() { |
| 1135 | + const { data } = l1.loader() |
| 1136 | + return { data } |
| 1137 | + }, |
| 1138 | + template: `<p>{{ data }}</p>`, |
| 1139 | + }), |
| 1140 | + meta: { |
| 1141 | + loaders: [l1.loader], |
| 1142 | + }, |
| 1143 | + }) |
| 1144 | + l1.spy.mockResolvedValue('ok') |
| 1145 | + |
| 1146 | + const isVisible = ref(true) |
| 1147 | + |
| 1148 | + |
| 1149 | + const wrapper = mount( |
| 1150 | + () => (isVisible.value ? h(RouterViewMock) : h('p', ['hidden'])), |
| 1151 | + { |
| 1152 | + global: { |
| 1153 | + plugins: [ |
| 1154 | + [DataLoaderPlugin, { router }], |
| 1155 | + ...(plugins?.(customContext!) || []), |
| 1156 | + ], |
| 1157 | + }, |
| 1158 | + } |
| 1159 | + ) |
| 1160 | + |
| 1161 | + await router.push('/a') |
| 1162 | + expect(wrapper.text()).toBe('ok') |
| 1163 | + isVisible.value = false |
| 1164 | + await nextTick() |
| 1165 | + expect(wrapper.text()).toBe('hidden') |
| 1166 | + await router.push('/a') // failed duplicated navigation |
| 1167 | + isVisible.value = true |
| 1168 | + await nextTick() |
| 1169 | + expect(wrapper.text()).toBe('ok') |
| 1170 | + }) |
| 1171 | + |
1112 | 1172 | describe('app.runWithContext()', () => {
|
1113 | 1173 | it('can inject globals', async () => {
|
1114 | 1174 | const { router, useData, app } = singleLoaderOneRoute(
|
|
0 commit comments