|
2 | 2 | import { VDialog } from '../VDialog' |
3 | 3 |
|
4 | 4 | // Utilities |
5 | | -import { commands, render, screen, userEvent } from '@test' |
6 | | -import { nextTick, ref } from 'vue' |
| 5 | +import { commands, render, screen, userEvent, wait } from '@test' |
| 6 | +import { h, nextTick, ref } from 'vue' |
| 7 | +import { createMemoryHistory, createRouter } from 'vue-router' |
7 | 8 |
|
8 | 9 | // Tests |
9 | 10 | describe('VDialog', () => { |
@@ -88,4 +89,106 @@ describe('VDialog', () => { |
88 | 89 | await userEvent.tab() |
89 | 90 | await expect.poll(() => document.activeElement).toBe(first) |
90 | 91 | }) |
| 92 | + |
| 93 | + describe('routing back', () => { |
| 94 | + function createTestRouter () { |
| 95 | + return createRouter({ |
| 96 | + history: createMemoryHistory(), |
| 97 | + routes: [ |
| 98 | + { path: '/', component: { setup: () => () => h('h1', 'home') } }, |
| 99 | + { path: '/page1', component: { setup: () => () => h('h1', 'page1') } }, |
| 100 | + { path: '/page2', component: { setup: () => () => h('h1', 'page2') } }, |
| 101 | + { path: '/page3', component: { setup: () => () => h('h1', 'page3') } }, |
| 102 | + ], |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + async function simulateBackNavigation (router: ReturnType<typeof createTestRouter>) { |
| 107 | + router.back() |
| 108 | + for (let i = 0; i < 10; i++) await nextTick() // flush microtasks |
| 109 | + window.dispatchEvent(new PopStateEvent('popstate', { state: {} })) |
| 110 | + await wait() |
| 111 | + } |
| 112 | + |
| 113 | + it('should block back with persistent dialog, allow after close, and block again when reopened', async () => { |
| 114 | + const router = createTestRouter() |
| 115 | + await router.push('/page1') |
| 116 | + await router.push('/page2') |
| 117 | + await router.push('/page3') |
| 118 | + |
| 119 | + const model = ref(true) |
| 120 | + render(() => ( |
| 121 | + <VDialog v-model={ model.value } persistent data-testid="dialog"> |
| 122 | + <div data-testid="content">Content</div> |
| 123 | + </VDialog> |
| 124 | + ), { global: { plugins: [router] } }) |
| 125 | + |
| 126 | + await expect(screen.findByTestId('dialog')).resolves.toBeVisible() |
| 127 | + await wait() |
| 128 | + |
| 129 | + // 1st back: persistent dialog blocks navigation |
| 130 | + await simulateBackNavigation(router) |
| 131 | + expect(model.value).toBeTruthy() |
| 132 | + expect(router.currentRoute.value.path).toBe('/page3') |
| 133 | + |
| 134 | + // close the dialog |
| 135 | + model.value = false |
| 136 | + await nextTick() |
| 137 | + |
| 138 | + // 2nd back: no dialog blocking, navigation proceeds |
| 139 | + await simulateBackNavigation(router) |
| 140 | + expect(router.currentRoute.value.path).toBe('/page2') |
| 141 | + |
| 142 | + // reopen |
| 143 | + model.value = true |
| 144 | + await nextTick() |
| 145 | + await expect(screen.findByTestId('dialog')).resolves.toBeVisible() |
| 146 | + await wait() |
| 147 | + |
| 148 | + // 3rd back: persistent dialog blocks again |
| 149 | + await simulateBackNavigation(router) |
| 150 | + expect(model.value).toBeTruthy() |
| 151 | + expect(router.currentRoute.value.path).toBe('/page2') |
| 152 | + }) |
| 153 | + |
| 154 | + it('should close non-persistent dialog on back and block navigation', async () => { |
| 155 | + const router = createTestRouter() |
| 156 | + await router.push('/page1') |
| 157 | + await router.push('/page2') |
| 158 | + await router.push('/page3') |
| 159 | + |
| 160 | + const model = ref(false) |
| 161 | + render(() => ( |
| 162 | + <VDialog v-model={ model.value } data-testid="dialog"> |
| 163 | + <div data-testid="content">Content</div> |
| 164 | + </VDialog> |
| 165 | + ), { global: { plugins: [router] } }) |
| 166 | + |
| 167 | + // Open dialog |
| 168 | + model.value = true |
| 169 | + await nextTick() |
| 170 | + await expect(screen.findByTestId('dialog')).resolves.toBeVisible() |
| 171 | + await wait() |
| 172 | + |
| 173 | + // 1st back: dialog closes but route stays (navigation blocked) |
| 174 | + await simulateBackNavigation(router) |
| 175 | + await expect.poll(() => model.value).toBeFalsy() |
| 176 | + expect(router.currentRoute.value.path).toBe('/page3') |
| 177 | + |
| 178 | + // 2nd back: no dialog, navigation proceeds |
| 179 | + await simulateBackNavigation(router) |
| 180 | + expect(router.currentRoute.value.path).toBe('/page2') |
| 181 | + |
| 182 | + // reopen |
| 183 | + model.value = true |
| 184 | + await nextTick() |
| 185 | + await expect(screen.findByTestId('dialog')).resolves.toBeVisible() |
| 186 | + await wait() |
| 187 | + |
| 188 | + // 3rd back: dialog closes again, route stays |
| 189 | + await simulateBackNavigation(router) |
| 190 | + await expect.poll(() => model.value).toBeFalsy() |
| 191 | + expect(router.currentRoute.value.path).toBe('/page2') |
| 192 | + }) |
| 193 | + }) |
91 | 194 | }) |
0 commit comments