describe('warnings', () => {
mockWarn()
- // TODO: add warnings for invalid records
- it.skip('warns if alias is missing a required param', () => {
+ it('warns if alias is missing a required param', () => {
createRouterMatcher([{ path: '/:id', alias: '/no-id', component }], {})
- expect('TODO').toHaveBeenWarned()
+ expect('same param named "id"').toHaveBeenWarned()
+ })
+
+ it('does not warn for optional param on alias', () => {
+ createRouterMatcher(
+ [{ path: '/:id', alias: '/:id-:suffix?', component }],
+ {}
+ )
+ expect('same param named').not.toHaveBeenWarned()
+ })
+
+ it('does not warn for optional param on main record', () => {
+ createRouterMatcher(
+ [{ alias: '/:id', path: '/:id-:suffix?', component }],
+ {}
+ )
+ expect('same param named').not.toHaveBeenWarned()
})
})
})
)
}
+/**
+ * Check if a path and its alias have the same required params
+ *
+ * @param a - original record
+ * @param b - alias record
+ */
function checkSameParams(a: RouteRecordMatcher, b: RouteRecordMatcher) {
for (let key of a.keys) {
- if (!b.keys.find(isSameParam.bind(null, key)))
+ if (!key.optional && !b.keys.find(isSameParam.bind(null, key)))
return warn(
`Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"`
)
}
for (let key of b.keys) {
- if (!a.keys.find(isSameParam.bind(null, key)))
+ if (!key.optional && !a.keys.find(isSameParam.bind(null, key)))
return warn(
`Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"`
)