Skip to content

Commit 1ed7d1f

Browse files
committed
feat: attempt at unified stateful test
1 parent 031eeb0 commit 1ed7d1f

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { DefineComponent } from 'vue'
2+
import { getStoryId } from '../interaction-utils/storySelector'
3+
import { expect } from '@storybook/jest'
4+
import { StoryFn } from '@storybook/vue3'
5+
6+
export const UseStatefulTemplate = (
7+
component: Record<string, DefineComponent>,
8+
input: (el: HTMLElement) => void = el => el.click(),
9+
defaultValue = false
10+
): StoryFn => {
11+
const story = () => ({
12+
setup () {
13+
return {
14+
component,
15+
defaultValue,
16+
}
17+
},
18+
template: `
19+
<p>[true]</p>
20+
<component
21+
data-testid="true"
22+
:is="component"
23+
:stateful="defaultValue === true ? undefined : true"
24+
/>
25+
26+
<p>[false]</p>
27+
<component
28+
data-testid="false"
29+
:is="component"
30+
:stateful="defaultValue === false ? undefined : false"
31+
/>
32+
`,
33+
})
34+
story.play = async () => {
35+
/**
36+
* To test stateful we need to test 2 things:
37+
* * On user input for stateful="true" - there should be change
38+
* * On user input for stateful="false" - there should be no change
39+
*/
40+
input(getStoryId('true'))
41+
input(getStoryId('false'))
42+
43+
// We rely on visual tests here
44+
expect(true).toBe(true)
45+
}
46+
return story
47+
}

Diff for: packages/ui/src/components/va-checkbox/VaCheckbox.stories.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { VaCheckbox } from './'
22
import { VaButton } from '../va-button'
33
import { defineStory } from '../../../.storybook/types'
4+
import {
5+
UseStatefulTemplate,
6+
} from '../../../.storybook/composable-templates/useStateful-template'
47

58
export default {
69
title: 'VaCheckbox',
@@ -16,15 +19,10 @@ export const Default = () => ({
1619
`,
1720
})
1821

19-
export const Stateful = () => ({
20-
components: { VaCheckbox },
21-
template: `
22-
[true]
23-
<VaCheckbox stateful/>
24-
[false]
25-
<VaCheckbox :stateful="false"/>
26-
`,
27-
})
22+
export const Stateful = UseStatefulTemplate(
23+
VaCheckbox,
24+
(el) => (el.children[0] as HTMLInputElement).click(), // Clicking on checkbox container doesn't change value. see #4187
25+
)
2826

2927
export const Color = () => ({
3028
components: { VaCheckbox },

Diff for: packages/ui/src/components/va-switch/VaSwitch.demo.vue

-6
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,6 @@
207207
indeterminate-value="middle"
208208
/>
209209
</VbCard>
210-
<VbCard title="Stateless switch without v-model">
211-
<va-switch />
212-
</VbCard>
213-
<VbCard title="Stateful">
214-
<va-switch stateful />
215-
</VbCard>
216210
</VbDemo>
217211
</template>
218212

Diff for: packages/ui/src/components/va-switch/VaSwitch.stories.ts

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { defineComponent } from 'vue'
33
import VaSwitchDemo from './VaSwitch.demo.vue'
44
import VaSwitch from './VaSwitch.vue'
55
import { defineStory } from '../../../.storybook/types'
6+
import { expect } from '@storybook/jest'
7+
import {
8+
UseStatefulTemplate,
9+
} from '../../../.storybook/composable-templates/useStateful-template'
610

711
export default {
812
title: 'VaSwitch',
@@ -14,6 +18,11 @@ export const Default = () => defineComponent({
1418
template: '<VaSwitch/>',
1519
})
1620

21+
export const Stateful = UseStatefulTemplate(
22+
VaSwitch,
23+
el => (el.children[0].children[0].children[0] as HTMLInputElement).click(), // Clicking on checkbox container doesn't change value. see #4187
24+
)
25+
1726
export const ChangeEvent = defineStory({
1827
story: () => ({
1928
components: { VaSwitch },

0 commit comments

Comments
 (0)