From a39b8530ebdc5ee97d1b36175f67e5accfc82d3f Mon Sep 17 00:00:00 2001 From: Nick McBurney Date: Thu, 24 Jul 2025 22:40:13 +0100 Subject: [PATCH 1/3] support params objects in mock router --- examples/mockRouter.stories.ts | 24 ++++++++++++++++++++++++ src/utils.ts | 17 ++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/examples/mockRouter.stories.ts b/examples/mockRouter.stories.ts index d625d63..9195b64 100644 --- a/examples/mockRouter.stories.ts +++ b/examples/mockRouter.stories.ts @@ -51,6 +51,30 @@ Default.decorators = [ }) ] +export const ParamsObject = () => ({ + args: { + path: '/' + }, + /* create template and pass Storybook args to using props */ + template: ` +
+
+

$route:

+
<pre>{{ §route }}</pre>
+
{{ $route }}
+
+
+ ` +}) + +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 using props */ template: ` diff --git a/src/utils.ts b/src/utils.ts index cae9346..baeec4d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -43,11 +43,18 @@ export function resetRoutes(router: Router, newRoutes: RouteRecordRaw[]): void { } type argObjectKeys = Record -export function getFromArgs(args: argObjectKeys, options: Array) { +export function getFromArgs(args: argObjectKeys, options: Array|{ [key: string]: string }): 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 } \ No newline at end of file From 2532248df88327b1d4b9c17d5eb1ba92f8fe716b Mon Sep 17 00:00:00 2001 From: Nick McBurney Date: Thu, 24 Jul 2025 22:40:17 +0100 Subject: [PATCH 2/3] 6.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7846fd0..1fb2dad 100644 --- a/package.json +++ b/package.json @@ -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", From 59ba2e3eea5055517b215b6c4351cc182abc7cf8 Mon Sep 17 00:00:00 2001 From: Nick McBurney Date: Thu, 24 Jul 2025 22:46:26 +0100 Subject: [PATCH 3/3] Type fixes and enhanced story --- examples/mockRouter.stories.ts | 4 ++-- src/utils.ts | 3 ++- src/withMockRouter.ts | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/mockRouter.stories.ts b/examples/mockRouter.stories.ts index 9195b64..64253e2 100644 --- a/examples/mockRouter.stories.ts +++ b/examples/mockRouter.stories.ts @@ -69,9 +69,9 @@ export const ParamsObject = () => ({ ParamsObject.decorators = [ mockRouter({ - meta: ['some_meta'], + meta: { some: 'meta' }, params:{ "type": "custom", "projectId": "1", "tab": "products", "vProduct": "1" }, - query: ['some_query'] + query: { 'some': 'query' } }) ] diff --git a/src/utils.ts b/src/utils.ts index baeec4d..aadf85f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -43,7 +43,8 @@ export function resetRoutes(router: Router, newRoutes: RouteRecordRaw[]): void { } type argObjectKeys = Record -export function getFromArgs(args: argObjectKeys, options: Array|{ [key: string]: string }): argObjectKeys { +export type ArgsArrayOrObject = Array | { [key: string]: string } +export function getFromArgs(args: argObjectKeys, options: ArgsArrayOrObject): argObjectKeys { let filtered: argObjectKeys = {} if (Array.isArray(options)) { options.forEach((option) => { diff --git a/src/withMockRouter.ts b/src/withMockRouter.ts index edd7829..76177aa 100644 --- a/src/withMockRouter.ts +++ b/src/withMockRouter.ts @@ -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 } @@ -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, params?: Array, query?: Array } + options: { meta?: ArgsArrayOrObject, params?: ArgsArrayOrObject, query?: ArgsArrayOrObject } ): Decorator { return (_, ctx) => ({ setup () {