Skip to content

Commit 4a91878

Browse files
committed
🎨 improve: テスト用のpiniaインスタンスをオプショナルに変更
1 parent d54ee87 commit 4a91878

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/helpers/test/mountSuspendedComponent.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { RouterLinkStub, type VueWrapper } from '@vue/test-utils';
44
import type { bindTestingPinia } from './bindTestingPinia';
55

66
interface MountOptions {
7+
testingPinia?: ReturnType<typeof bindTestingPinia>;
78
attachTo?: Element | string;
89
props?: Record<string, unknown>;
910
slots?: Record<string, () => VNode | VNode[] | string> | Slots;
@@ -18,6 +19,7 @@ const DEFAULT_STUBS = {
1819
} as const;
1920

2021
const DEFAULT_OPTIONS = {
22+
testingPinia: undefined,
2123
attachTo: undefined,
2224
props: {},
2325
slots: {},
@@ -33,8 +35,9 @@ const DEFAULT_OPTIONS = {
3335
*
3436
* @template VMValue - コンポーネントインスタンスの型。コンポーネントのプロパティやメソッドへの型安全なアクセスを提供します
3537
* @param component - テスト対象のVueコンポーネント
36-
* @param testingPinia - テスト用のPiniaインスタンス(bindTestingPinia関数で作成したもの)
3738
* @param options - マウントオプション(任意)
39+
* @param options.testingPinia - テスト用のPiniaインスタンス(bindTestingPinia関数で作成したもの)
40+
* @param options.attachTo - コンポーネントをマウントするDOM要素
3841
* @param options.props - コンポーネントに渡すprops
3942
* @param options.slots - コンポーネントのスロット
4043
* @param options.shallow - 浅いレンダリングを行うかどうか
@@ -49,8 +52,9 @@ const DEFAULT_OPTIONS = {
4952
* const useUserStore = pinia.stores.user;
5053
* useUserStore.user = { name: 'Test User' };
5154
*
52-
* const wrapper = await mountSuspendedComponent<{ computedProperty: string }>(MyComponent, pinia, {
53-
* props: { message: 'Hello' }
55+
* const wrapper = await mountSuspendedComponent<{ computedProperty: string }>(MyComponent, {
56+
* testingPinia: pinia
57+
* props: { message: 'Hello' },
5458
* });
5559
*
5660
* // コンポーネントの検証
@@ -63,11 +67,10 @@ const DEFAULT_OPTIONS = {
6367
*/
6468
export async function mountSuspendedComponent<VMValue>(
6569
component: Component,
66-
testingPinia: ReturnType<typeof bindTestingPinia>,
6770
options: Partial<MountOptions> = DEFAULT_OPTIONS,
6871
): Promise<VueWrapper<ComponentPublicInstance & VMValue>> {
6972
const mergedOptions = { ...DEFAULT_OPTIONS, ...options };
70-
const { attachTo, props, slots, shallow, stubs, mocks, options: additionalOptions } = mergedOptions;
73+
const { testingPinia, attachTo, props, slots, shallow, stubs, mocks, options: additionalOptions } = mergedOptions;
7174

7275
return await mountSuspended(component, {
7376
...additionalOptions,
@@ -76,7 +79,7 @@ export async function mountSuspendedComponent<VMValue>(
7679
slots,
7780
shallow,
7881
global: {
79-
plugins: [testingPinia],
82+
plugins: testingPinia ? [testingPinia] : [],
8083
stubs: {
8184
...DEFAULT_STUBS,
8285
...stubs,

0 commit comments

Comments
 (0)