)
})
+ it('resolves children with empty paths', () => {
+ const Nested = { path: '', name: 'nested', component }
+ const Foo = {
+ path: '/foo',
+ name: 'Foo',
+ component,
+ children: [Nested],
+ }
+ assertRecordMatch(
+ Foo,
+ { path: '/foo' },
+ {
+ name: 'nested',
+ path: '/foo',
+ params: {},
+ matched: [Foo, { ...Nested, path: `${Foo.path}` }],
+ }
+ )
+ })
+
+ it('resolves nested children with empty paths', () => {
+ const NestedNested = { path: '', name: 'nested', component }
+ const Nested = {
+ path: '',
+ name: 'nested-nested',
+ component,
+ children: [NestedNested],
+ }
+ const Foo = {
+ path: '/foo',
+ name: 'Foo',
+ component,
+ children: [Nested],
+ }
+ assertRecordMatch(
+ Foo,
+ { path: '/foo' },
+ {
+ name: 'nested',
+ path: '/foo',
+ params: {},
+ matched: [
+ Foo,
+ { ...Nested, path: `${Foo.path}` },
+ { ...NestedNested, path: `${Foo.path}` },
+ ],
+ }
+ )
+ })
+
it('resolves nested children', () => {
const Foo = {
path: '/foo',
if (parent) {
// if the child isn't an absolute route
if (record.path[0] !== '/') {
- recordCopy.path = parent.record.path + '/' + record.path // TODO: check for trailing slash?
+ let path = parent.record.path
+ // only add the / delimiter if the child path isn't empty
+ if (recordCopy.path) path += '/'
+ path += record.path // TODO: check for trailing slash?
+ recordCopy.path = path
}
}
// that redirects but ended up not redirecting
if ('redirect' in matcher.record) throw new InvalidRouteMatch(location)
- const matched = extractMatchedRecord(matcher)
+ const matched: MatcherLocationNormalized['matched'] = [matcher.record]
+ let parentMatcher: RouteMatcher | void = matcher.parent
+ while (parentMatcher) {
+ // reversed order so parents are at the beginning
+ // TODO: should be doable by typing RouteMatcher in a different way
+ if ('redirect' in parentMatcher.record) throw new Error('TODO')
+ matched.unshift(parentMatcher.record)
+ parentMatcher = parentMatcher.parent
+ }
return {
name,
}
}
}
-
-/**
- * Generate the array of the matched array. This is an array containing
- * all records matching a route, from parent to child. If there are no children
- * in the matched record matcher, the array only contains one element
- * @param matcher
- * @returns an array of MatcherLocationNormalized
- */
-function extractMatchedRecord(
- matcher: RouteMatcher
-): MatcherLocationNormalized['matched'] {
- if ('redirect' in matcher.record) throw new Error('TODO')
-
- const matched: MatcherLocationNormalized['matched'] = [matcher.record]
- let parentMatcher: RouteMatcher | void = matcher.parent
- while (parentMatcher) {
- // reversed order so parents are at the beginning
- // TODO: should be doable by typing RouteMatcher in a different way
- if ('redirect' in parentMatcher.record) throw new Error('TODO')
- matched.unshift(parentMatcher.record)
- parentMatcher = parentMatcher.parent
- }
-
- return matched
-}