Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/mockRouter.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ Default.decorators = [
})
]

export const ParamsObject = () => ({
args: {
path: '/'
},
/* create template and pass Storybook args to <router-view> using props */
template: `
<div class="my-wrapper">
<div>
<h2>$route:</h2>
<pre>&lt;pre&gt;&#123;&#123;&nbsp;&sect;route&nbsp;&#125;&#125;&lt;/pre&gt;</pre>
<pre>{{ $route }}</pre>
</div>
</div>
`
})

ParamsObject.decorators = [
mockRouter({
meta: { some: 'meta' },
params:{ "type": "custom", "projectId": "1", "tab": "products", "vProduct": "1" },
query: { 'some': 'query' }
})
]

export const DynamicTemplate = () => ({
/* create template and pass Storybook args to <router-view> using props */
template: `
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storybook-vue3-router",
"version": "6.0.0",
"version": "6.0.1",
"description": "A Storybook decorator that allows you to build stories for your routing-aware components.",
"keywords": [
"storybook-addons",
Expand Down
18 changes: 13 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ export function resetRoutes(router: Router, newRoutes: RouteRecordRaw[]): void {
}

type argObjectKeys = Record<string, unknown>
export function getFromArgs(args: argObjectKeys, options: Array<string>) {
export type ArgsArrayOrObject = Array<string> | { [key: string]: string }
export function getFromArgs(args: argObjectKeys, options: ArgsArrayOrObject): argObjectKeys {
let filtered: argObjectKeys = {}
options.forEach((option) => {
filtered = { ...filtered, [option]: args[option] }
})

if (Array.isArray(options)) {
options.forEach((option) => {
filtered = { ...filtered, [option]: args[option] }
})
} else if (typeof options === 'object' && options !== null) {
Object.keys(options).forEach((option) => {
filtered = { ...filtered, [option]: options[option] }
})
} else {
filtered = { ...filtered }
}
return filtered
}
4 changes: 2 additions & 2 deletions src/withMockRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
RouteLocationNormalizedLoaded
} from 'vue-router'

import { getFromArgs } from './utils'
import { getFromArgs, type ArgsArrayOrObject } from './utils'

type MockRouter = Router & { isMocked?: boolean }
type MockRoute = RouteLocationNormalizedLoaded & { isMocked?: boolean }
Expand All @@ -26,7 +26,7 @@ type MockRoute = RouteLocationNormalizedLoaded & { isMocked?: boolean }
*/
export function withMockRouter (
/* optional: router options - used to pass `initialRoute` value, `beforeEach()` navigation guard methods and vue-router `createRouter` options */
options: { meta?: Array<string>, params?: Array<string>, query?: Array<string> }
options: { meta?: ArgsArrayOrObject, params?: ArgsArrayOrObject, query?: ArgsArrayOrObject }
): Decorator {
return (_, ctx) => ({
setup () {
Expand Down
Loading