测试单击“ Web-Component”上的事件。故事书的按钮
我在互联网上寻找有关如何测试单击事件的示例在按钮上( Web组件/打字稿),但我找不到清晰的一。 您能建议我一些指南/文章或代码片段吗?
我已经创建了 story.ts 文件;这样的是:
import { html, TemplateResult } from 'lit';
import './index.ts';
import { Properties } from './model/button.interfaces';
export default {
title: 'My button',
component: 'my-button',
};
interface Story<T> {
(args: T): TemplateResult;
args?: Partial<T>;
argTypes?: Record<string, unknown>;
parameters?: any;
}
interface ArgTypes extends Properties{}
const Template: Story<ArgTypes> = (args: ArgTypes) => html`
<my-button
?disabled="${args.disabled}"
color=${args.color}>
${args.text}
</my-button>`
export const Button = Template.bind({});
Button.args = {
label: "This is the text",
color: "primary"
}
Button.argTypes = {
color: {
control: 'select',
options: ['primary', 'secondary'],
table: {
category: 'Modifiers',
},
},
disabled: {
control: 'boolean',
options: [true, false],
table: {
category: 'Modifiers',
},
}
}
谢谢
I'm looking on the Internet for an example about how to test click event on a button (web component/typescript) with Storybook, but I can't find a clear one.
Can you suggest me some guides/articles or code snippets?
I already created the stories.ts file; it's something like this:
import { html, TemplateResult } from 'lit';
import './index.ts';
import { Properties } from './model/button.interfaces';
export default {
title: 'My button',
component: 'my-button',
};
interface Story<T> {
(args: T): TemplateResult;
args?: Partial<T>;
argTypes?: Record<string, unknown>;
parameters?: any;
}
interface ArgTypes extends Properties{}
const Template: Story<ArgTypes> = (args: ArgTypes) => html`
<my-button
?disabled="${args.disabled}"
color=${args.color}>
${args.text}
</my-button>`
export const Button = Template.bind({});
Button.args = {
label: "This is the text",
color: "primary"
}
Button.argTypes = {
color: {
control: 'select',
options: ['primary', 'secondary'],
table: {
category: 'Modifiers',
},
},
disabled: {
control: 'boolean',
options: [true, false],
table: {
category: 'Modifiers',
},
}
}
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于Web组件可作为本机HTML元素起作用,因此您可以将onclick属性附加到
&lt; my-button&gt;
。尝试在组件上方添加脚本标签,在该组件上定义一个单击处理程序,然后将其添加到组件中。
现在,每当单击按钮时,都应该看到日志。
Since web components work as native html elements, you can attach an onclick attribute to your
<my-button>
.Try adding a script tag above your component in which you define an onclick handler and add it to your component.
Now you should see the log whenever you click on the button.