Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: attempt at unified stateful test #4263

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { DefineComponent } from 'vue'
import { getStoryId } from '../interaction-utils/storySelector'
import { expect } from '@storybook/jest'
import { StoryFn } from '@storybook/vue3'

export const UseStatefulTemplate = (
component: Record<string, DefineComponent>,
input: (el: HTMLElement) => void = el => el.click(),
defaultValue = false
): StoryFn => {
const story = () => ({
setup () {
return {
component,
defaultValue,
}
},
template: `
<p>[true]</p>
<component
data-testid="true"
:is="component"
:stateful="defaultValue === true ? undefined : true"
/>

<p>[false]</p>
<component
data-testid="false"
:is="component"
:stateful="defaultValue === false ? undefined : false"
/>
`,
})
story.play = async () => {
/**
* To test stateful we need to test 2 things:
* * On user input for stateful="true" - there should be change
* * On user input for stateful="false" - there should be no change
*/
input(getStoryId('true'))
input(getStoryId('false'))

// We rely on visual tests here
expect(true).toBe(true)
}
return story
}
16 changes: 7 additions & 9 deletions packages/ui/src/components/va-checkbox/VaCheckbox.stories.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { VaCheckbox } from './'
import { VaButton } from '../va-button'
import { defineStory } from '../../../.storybook/types'
import {
UseStatefulTemplate,
} from '../../../.storybook/composable-templates/useStateful-template'

export default {
title: 'VaCheckbox',
Expand All @@ -16,15 +19,10 @@ export const Default = () => ({
`,
})

export const Stateful = () => ({
components: { VaCheckbox },
template: `
[true]
<VaCheckbox stateful/>
[false]
<VaCheckbox :stateful="false"/>
`,
})
export const Stateful = UseStatefulTemplate(
VaCheckbox,
(el) => (el.children[0] as HTMLInputElement).click(), // Clicking on checkbox container doesn't change value. see #4187
)

export const Color = () => ({
components: { VaCheckbox },
Expand Down
6 changes: 0 additions & 6 deletions packages/ui/src/components/va-switch/VaSwitch.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,6 @@
indeterminate-value="middle"
/>
</VbCard>
<VbCard title="Stateless switch without v-model">
<va-switch />
</VbCard>
<VbCard title="Stateful">
<va-switch stateful />
</VbCard>
</VbDemo>
</template>

Expand Down
9 changes: 9 additions & 0 deletions packages/ui/src/components/va-switch/VaSwitch.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { defineComponent } from 'vue'
import VaSwitchDemo from './VaSwitch.demo.vue'
import VaSwitch from './VaSwitch.vue'
import { defineStory } from '../../../.storybook/types'
import { expect } from '@storybook/jest'
import {
UseStatefulTemplate,
} from '../../../.storybook/composable-templates/useStateful-template'

export default {
title: 'VaSwitch',
Expand All @@ -14,6 +18,11 @@ export const Default = () => defineComponent({
template: '<VaSwitch/>',
})

export const Stateful = UseStatefulTemplate(
VaSwitch,
el => (el.children[0].children[0].children[0] as HTMLInputElement).click(), // Clicking on checkbox container doesn't change value. see #4187
)

export const ChangeEvent = defineStory({
story: () => ({
components: { VaSwitch },
Expand Down
Loading