From 29ff689e8e3f07e967aacfa9156cc3873d729e7d Mon Sep 17 00:00:00 2001
From: Yann Braga
Date: Wed, 14 Feb 2024 12:08:36 +0100
Subject: [PATCH] Portable stories: Add tests for React and Vue renderers
---
.../src/modules/store/csf/portable-stories.ts | 2 +-
.../react/src/__test__/Button.stories.tsx | 33 +++++++++-
.../portable-stories.test.tsx.snap | 19 ++++++
.../src/__test__/portable-stories.test.tsx | 21 +++++--
.../composeStories/Button.stories.ts | 36 ++++++++++-
.../portable-stories.test.ts.snap | 19 ++++++
.../composeStories/portable-stories.test.ts | 61 +++++++++++--------
.../src/stories/Button.stories.tsx | 29 ++++++++-
8 files changed, 186 insertions(+), 34 deletions(-)
diff --git a/code/lib/preview-api/src/modules/store/csf/portable-stories.ts b/code/lib/preview-api/src/modules/store/csf/portable-stories.ts
index bd2dd421ccfe..de45ab6af59b 100644
--- a/code/lib/preview-api/src/modules/store/csf/portable-stories.ts
+++ b/code/lib/preview-api/src/modules/store/csf/portable-stories.ts
@@ -89,7 +89,7 @@ export function composeStory> = Object.assign(
function storyFn(extraArgs?: Partial) {
context.args = {
- ...context.args,
+ ...context.initialArgs,
...extraArgs,
};
diff --git a/code/renderers/react/src/__test__/Button.stories.tsx b/code/renderers/react/src/__test__/Button.stories.tsx
index 6882b957b136..412f55048c2f 100644
--- a/code/renderers/react/src/__test__/Button.stories.tsx
+++ b/code/renderers/react/src/__test__/Button.stories.tsx
@@ -1,5 +1,5 @@
import React from 'react';
-import { within, userEvent } from '@storybook/testing-library';
+import { within, userEvent, fn, expect } from '@storybook/test';
import type { StoryFn as CSF2Story, StoryObj as CSF3Story, Meta } from '..';
import type { ButtonProps } from './Button';
@@ -84,7 +84,36 @@ export const CSF3InputFieldFilled: CSF3Story = {
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
await step('Step label', async () => {
- await userEvent.type(canvas.getByTestId('input'), 'Hello world!');
+ const inputEl = canvas.getByTestId('input');
+ await userEvent.type(inputEl, 'Hello world!');
+ await expect(inputEl).toHaveValue('Hello world!');
});
},
};
+
+const spyFn = fn();
+export const LoaderStory: CSF3Story<{ spyFn: (val: string) => string }> = {
+ args: {
+ spyFn,
+ },
+ render: (args, { loaded }) => {
+ const data = args.spyFn('foo');
+ return (
+
+
{loaded.value}
+
{String(data)}
+
+ );
+ },
+ loaders: [
+ async () => {
+ spyFn.mockReturnValueOnce('baz');
+ return {
+ value: 'bar',
+ };
+ },
+ ],
+ play: async () => {
+ expect(spyFn).toHaveBeenCalledWith('foo');
+ },
+};
diff --git a/code/renderers/react/src/__test__/__snapshots__/portable-stories.test.tsx.snap b/code/renderers/react/src/__test__/__snapshots__/portable-stories.test.tsx.snap
index 2b92b1d68424..018406c3581d 100644
--- a/code/renderers/react/src/__test__/__snapshots__/portable-stories.test.tsx.snap
+++ b/code/renderers/react/src/__test__/__snapshots__/portable-stories.test.tsx.snap
@@ -94,3 +94,22 @@ exports[`Renders CSF3Primary story 1`] = `
+
+
+
+