|
| 1 | +/** |
| 2 | + * @vitest-environment happy-dom |
| 3 | + */ |
| 4 | +import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' |
| 5 | +import { defineComponent, h } from 'vue' |
| 6 | +import { afterEach, describe, expect, it } from 'vitest' |
| 7 | +import { |
| 8 | + experimental_createRouter, |
| 9 | + normalizeRouteRecord, |
| 10 | + type EXPERIMENTAL_RouteRecordNormalized_Matchable, |
| 11 | +} from './router' |
| 12 | +import { createFixedResolver } from './route-resolver/resolver-fixed' |
| 13 | +import { |
| 14 | + MatcherPatternPathDynamic, |
| 15 | + MatcherPatternPathStatic, |
| 16 | +} from './route-resolver/matchers/matcher-pattern' |
| 17 | +import { MatcherPatternQueryParam } from './route-resolver/matchers/matcher-pattern-query' |
| 18 | +import { PARAM_PARSER_INT } from './route-resolver/matchers/param-parsers' |
| 19 | +import { _mergeRouteRecordViews } from './runtime' |
| 20 | +import { createMemoryHistory } from '../history/memory' |
| 21 | +import { RouterView } from '../RouterView' |
| 22 | + |
| 23 | +enableAutoUnmount(afterEach) |
| 24 | + |
| 25 | +const User = defineComponent({ |
| 26 | + props: { |
| 27 | + id: { type: Number, required: true }, |
| 28 | + page: { type: Number, default: 0 }, |
| 29 | + label: String, |
| 30 | + }, |
| 31 | + setup: props => () => |
| 32 | + h( |
| 33 | + 'p', |
| 34 | + `${typeof props.id}:${props.id} page:${props.page} label:${props.label}` |
| 35 | + ), |
| 36 | +}) |
| 37 | + |
| 38 | +const Aside = defineComponent({ |
| 39 | + props: { title: String }, |
| 40 | + setup: props => () => h('aside', `title:${props.title}`), |
| 41 | +}) |
| 42 | + |
| 43 | +const userPath = new MatcherPatternPathDynamic( |
| 44 | + /^\/users\/([^/]+?)$/i, |
| 45 | + { id: [PARAM_PARSER_INT] }, |
| 46 | + ['users', 1] |
| 47 | +) |
| 48 | +const pageQuery = [ |
| 49 | + new MatcherPatternQueryParam('page', 'page', 'value', PARAM_PARSER_INT, 1), |
| 50 | +] |
| 51 | + |
| 52 | +async function mountRouter( |
| 53 | + ...records: EXPERIMENTAL_RouteRecordNormalized_Matchable[] |
| 54 | +) { |
| 55 | + const router = experimental_createRouter({ |
| 56 | + history: createMemoryHistory(), |
| 57 | + resolver: createFixedResolver([ |
| 58 | + ...records, |
| 59 | + normalizeRouteRecord({ |
| 60 | + name: 'start', |
| 61 | + path: new MatcherPatternPathStatic('/'), |
| 62 | + components: {}, |
| 63 | + }), |
| 64 | + ]), |
| 65 | + }) |
| 66 | + await router.push('/') |
| 67 | + const wrapper = mount( |
| 68 | + defineComponent({ |
| 69 | + components: { RouterView }, |
| 70 | + template: `<RouterView /><RouterView name="aside" />`, |
| 71 | + }), |
| 72 | + { global: { plugins: [router] } } |
| 73 | + ) |
| 74 | + return { router, wrapper } |
| 75 | +} |
| 76 | + |
| 77 | +describe('Experimental route record props', () => { |
| 78 | + it('passes parsed params as props with `true`', async () => { |
| 79 | + const { router, wrapper } = await mountRouter( |
| 80 | + normalizeRouteRecord({ |
| 81 | + name: 'user', |
| 82 | + path: userPath, |
| 83 | + query: pageQuery, |
| 84 | + components: { default: User }, |
| 85 | + props: { default: true }, |
| 86 | + }) |
| 87 | + ) |
| 88 | + await router.push('/users/42?page=3') |
| 89 | + await flushPromises() |
| 90 | + expect(wrapper.text()).toBe('number:42 page:3 label:undefined') |
| 91 | + }) |
| 92 | + |
| 93 | + it('passes the result of a function', async () => { |
| 94 | + const { router, wrapper } = await mountRouter( |
| 95 | + normalizeRouteRecord({ |
| 96 | + name: 'user', |
| 97 | + path: userPath, |
| 98 | + components: { default: User }, |
| 99 | + props: { |
| 100 | + default: to => ({ id: Number(to.params.id) * 2, label: 'fn' }), |
| 101 | + }, |
| 102 | + }) |
| 103 | + ) |
| 104 | + await router.push('/users/7') |
| 105 | + await flushPromises() |
| 106 | + expect(wrapper.text()).toBe('number:14 page:0 label:fn') |
| 107 | + }) |
| 108 | + |
| 109 | + it('passes props to each named view', async () => { |
| 110 | + const { router, wrapper } = await mountRouter( |
| 111 | + normalizeRouteRecord({ |
| 112 | + name: 'user', |
| 113 | + path: userPath, |
| 114 | + components: { default: User, aside: Aside }, |
| 115 | + props: { default: true, aside: { title: 'static' } }, |
| 116 | + }) |
| 117 | + ) |
| 118 | + await router.push('/users/1') |
| 119 | + await flushPromises() |
| 120 | + expect(wrapper.find('p').text()).toBe('number:1 page:0 label:undefined') |
| 121 | + expect(wrapper.find('aside').text()).toBe('title:static') |
| 122 | + }) |
| 123 | + |
| 124 | + it('passes no params to views without props', async () => { |
| 125 | + const { router, wrapper } = await mountRouter( |
| 126 | + normalizeRouteRecord({ |
| 127 | + name: 'user', |
| 128 | + path: userPath, |
| 129 | + components: { default: User, aside: Aside }, |
| 130 | + props: { default: true }, |
| 131 | + }) |
| 132 | + ) |
| 133 | + await router.push('/users/1') |
| 134 | + await flushPromises() |
| 135 | + expect(wrapper.find('aside').text()).toBe('title:undefined') |
| 136 | + }) |
| 137 | + |
| 138 | + it('passes definePage() props to the view of the page', async () => { |
| 139 | + const { router, wrapper } = await mountRouter( |
| 140 | + normalizeRouteRecord( |
| 141 | + _mergeRouteRecordViews( |
| 142 | + { |
| 143 | + name: 'user', |
| 144 | + path: userPath, |
| 145 | + components: { default: User, aside: Aside }, |
| 146 | + }, |
| 147 | + { |
| 148 | + default: { props: true }, |
| 149 | + aside: { props: to => ({ title: `user ${to.params.id}` }) }, |
| 150 | + } |
| 151 | + ) |
| 152 | + ) |
| 153 | + ) |
| 154 | + await router.push('/users/5') |
| 155 | + await flushPromises() |
| 156 | + expect(wrapper.find('p').text()).toBe('number:5 page:0 label:undefined') |
| 157 | + expect(wrapper.find('aside').text()).toBe('title:user 5') |
| 158 | + }) |
| 159 | +}) |
| 160 | + |
| 161 | +describe('_mergeRouteRecordViews', () => { |
| 162 | + it('sets the props of each page on its view', () => { |
| 163 | + const record = _mergeRouteRecordViews( |
| 164 | + { meta: {}, props: {} }, |
| 165 | + { |
| 166 | + default: { props: true }, |
| 167 | + aside: { props: { title: 'a' } }, |
| 168 | + } |
| 169 | + ) |
| 170 | + expect(record.props).toEqual({ default: true, aside: { title: 'a' } }) |
| 171 | + }) |
| 172 | + |
| 173 | + it('merges meta of all the pages', () => { |
| 174 | + const record = _mergeRouteRecordViews( |
| 175 | + { meta: { a: 1 } }, |
| 176 | + { |
| 177 | + default: { meta: { b: 2 } }, |
| 178 | + aside: { meta: { c: 3 } }, |
| 179 | + } |
| 180 | + ) |
| 181 | + expect(record.meta).toEqual({ a: 1, b: 2, c: 3 }) |
| 182 | + }) |
| 183 | + |
| 184 | + it('keeps the properties extracted at build time', () => { |
| 185 | + const path = new MatcherPatternPathStatic('/about') |
| 186 | + const record = _mergeRouteRecordViews( |
| 187 | + { name: '/about', path }, |
| 188 | + { |
| 189 | + default: { |
| 190 | + name: 'custom', |
| 191 | + path: '/custom', |
| 192 | + alias: ['/other'], |
| 193 | + params: { query: { page: 'int' } }, |
| 194 | + }, |
| 195 | + } |
| 196 | + ) |
| 197 | + expect(record).toEqual({ name: '/about', path }) |
| 198 | + }) |
| 199 | + |
| 200 | + it('keeps other properties of the page', () => { |
| 201 | + const redirect = () => '/' |
| 202 | + const record = _mergeRouteRecordViews({}, { default: { redirect } }) |
| 203 | + expect(record).toEqual({ redirect }) |
| 204 | + }) |
| 205 | +}) |
0 commit comments