generated from unplugin/unplugin-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for play functions (#57)
* Add suport for play function * Added test for play function support * Fixed typo in test name * Added play to StoryProps * Update test/index.test.ts * Added examples * Moved code into new line and updated tests
- Loading branch information
1 parent
4993db7
commit e309234
Showing
7 changed files
with
246 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<template> | ||
<div class="container"> | ||
<form @submit.prevent="handleSubmit"> | ||
<label for="email">Email</label> | ||
<input | ||
v-model="email" | ||
id="email" | ||
type="text" | ||
data-testid="email" | ||
/> | ||
<label for="password">Password</label> | ||
<input | ||
v-model="password" | ||
id="password" | ||
type="password" | ||
data-testid="password" | ||
/> | ||
<Button | ||
label="Submit" | ||
type="submit" | ||
primary | ||
></Button> | ||
</form> | ||
|
||
<p> | ||
{{ message }} | ||
</p> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
import { ref } from 'vue' | ||
import Button from './Button.vue' | ||
const message = ref('') | ||
const email = ref('') | ||
const password = ref('') | ||
function handleSubmit() { | ||
if (!email.value) { | ||
message.value = 'Please enter your email' | ||
return | ||
} | ||
if (!password.value) { | ||
message.value = 'Please enter your password' | ||
return | ||
} | ||
message.value = | ||
'Everything is perfect. Your account is ready and we should probably get you started!' | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
form > input { | ||
margin: 7px; | ||
} | ||
.container, | ||
form { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
</style> |
60 changes: 60 additions & 0 deletions
60
examples/vite/src/writing-stories/5-using-the-play-function.stories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { Meta, StoryObj } from '@storybook/vue3' | ||
|
||
import { userEvent, within } from '@storybook/testing-library' | ||
|
||
import { expect } from '@storybook/jest' | ||
|
||
import LoginForm from '../components/LoginForm.vue' | ||
|
||
const meta: Meta<typeof LoginForm> = { | ||
/* 👇 The title prop is optional. | ||
* See https://storybook.js.org/docs/vue/configure/overview#configure-story-loading | ||
* to learn how to generate automatic titles | ||
*/ | ||
title: 'docs/5. Using the play function/classical', | ||
component: LoginForm, | ||
} | ||
|
||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
|
||
/* | ||
*👇 Render functions are a framework specific feature to allow you control on how the component renders. | ||
* See https://storybook.js.org/docs/vue/api/csf | ||
* to learn how to use render functions. | ||
*/ | ||
export const EmptyForm: Story = { | ||
render: () => ({ | ||
components: { LoginForm }, | ||
template: `<LoginForm />`, | ||
}), | ||
} | ||
|
||
/* | ||
* See https://storybook.js.org/docs/vue/writing-stories/play-function#working-with-the-canvas | ||
* to learn more about using the canvasElement to query the DOM | ||
*/ | ||
export const FilledForm: Story = { | ||
render: () => ({ | ||
components: { LoginForm }, | ||
template: `<LoginForm />`, | ||
}), | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement) | ||
|
||
// 👇 Simulate interactions with the component | ||
await userEvent.type(canvas.getByTestId('email'), '[email protected]') | ||
|
||
await userEvent.type(canvas.getByTestId('password'), 'a-random-password') | ||
|
||
// See https://storybook.js.org/docs/vue/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel | ||
await userEvent.click(canvas.getByRole('button')) | ||
|
||
// 👇 Assert DOM structure | ||
await expect( | ||
canvas.getByText( | ||
'Everything is perfect. Your account is ready and we should probably get you started!' | ||
) | ||
).toBeInTheDocument() | ||
}, | ||
} |
47 changes: 47 additions & 0 deletions
47
examples/vite/src/writing-stories/5-using-the-play-function.stories.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<script setup> | ||
import { userEvent, within } from '@storybook/testing-library' | ||
import { expect } from '@storybook/jest' | ||
import LoginForm from '../components/LoginForm.vue' | ||
</script> | ||
|
||
<script> | ||
/* | ||
* See https://storybook.js.org/docs/vue/writing-stories/play-function#working-with-the-canvas | ||
* to learn more about using the canvasElement to query the DOM | ||
*/ | ||
async function playFunction({ canvasElement }) { | ||
const canvas = within(canvasElement) | ||
// 👇 Simulate interactions with the component | ||
await userEvent.type(canvas.getByTestId('email'), '[email protected]') | ||
await userEvent.type(canvas.getByTestId('password'), 'a-random-password') | ||
// See https://storybook.js.org/docs/vue/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel | ||
await userEvent.click(canvas.getByRole('button')) | ||
// 👇 Assert DOM structure | ||
await expect( | ||
canvas.getByText( | ||
'Everything is perfect. Your account is ready and we should probably get you started!' | ||
) | ||
).toBeInTheDocument() | ||
} | ||
</script> | ||
|
||
<template> | ||
<Stories | ||
title="docs/5. Using the play function/native" | ||
:component="LoginForm" | ||
> | ||
<Story title="Empty Form"> | ||
<LoginForm /> | ||
</Story> | ||
<Story | ||
title="Filled Form" | ||
:play="playFunction" | ||
> | ||
<LoginForm /> | ||
</Story> | ||
</Stories> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters