storybook [文档]:Angular - 关于处理事件发射器的文档不存在,

wnrlj8wa  于 4个月前  发布在  Angular
关注(0)|答案(2)|浏览(42)

描述问题

我已经找了10个小时了。没有文档说明如何验证组件是否发出事件。由于这个功能是关于Angular如何工作的,尤其是作为双向绑定的一部分,它的缺失是完全不可接受的。
给定这个组件:

import { Component, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'cv-button',
  standalone: true,
  template: `
    <button (click)="onClick()">Click Me</button>
  `,
  imports: [],
})
export class ButtonComponent {
  private count = 0
  @Output() clickCount = new EventEmitter<number>

  onClick() {
    this.count++
    this.clickCount.emit(this.count)
  }
}

我可以用这个故事来测试点击。

import { Meta, StoryObj, moduleMetadata } from '@storybook/angular';
import { ButtonComponent } from './button.component';
import { userEvent, within } from '@storybook/test';

const meta:Meta<ButtonComponent> = {
  title: 'Button Test',
  component: ButtonComponent,
  tags: ['autodocs'],
 
  decorators: [
    moduleMetadata({
      imports: []
    })
  ]
}

export default meta

type Story = StoryObj<ButtonComponent>

export const Primary: Story = {
  args: {},
  play: async ({canvasElement, args}) => {
    const element = within(canvasElement)
    userEvent.click(element.getByText('Click Me'))
  }
}

但是在这个网站上或者网上的任何地方,我都找不到一个明确说明如何检查clickCount是否递增的方法。我知道它一定是可能的 - Storybook在Actions检查器中显示它正在增加。但那个检查器对于自动化测试完全无用。
请帮帮我。这已经让我非常沮丧,让我几乎要放弃使用Storybook和Angular一起进行测试,因为无法测试组件是否实际发出它们应该发出的值使得整个练习变得毫无意义。

附加上下文

  • 无回复*
i1icjdpr

i1icjdpr1#

免责声明
由于信息是自动生成的,可能存在不准确的情况。
要验证Angular中的组件是否在Storybook中发出事件,可以使用play函数中的args参数来监听该事件。以下是如何修改故事以检查clickCount是否递增的方法:

import { Meta, StoryObj, moduleMetadata } from '@storybook/angular';
import { ButtonComponent } from './button.component';
import { userEvent, within } from '@storybook/testing-library';

const meta: Meta<ButtonComponent> = {
  title: 'Button Test',
  component: ButtonComponent,
  tags: ['autodocs'],
  decorators: [
    moduleMetadata({
      imports: []
    })
  ]
};

export default meta;

type Story = StoryObj<ButtonComponent>;

export const Primary: Story = {
  args: {},
  play: async ({ canvasElement, args }) => {
    const element = within(canvasElement);
    const clickCountSpy = jest.fn();
    args.clickCount = { emit: clickCountSpy };
    userEvent.click(element.getByText('Click Me'));
    expect(clickCountSpy).toHaveBeenCalledWith(1);
  }
};

这段代码使用Jest创建了一个spy函数,用于监听clickCount事件,并验证它是否使用了预期的值被调用。

参考资料

/code/addons/docs/angular
/code/frameworks/angular/src/client/docs/testfixtures/doc-button/argtypes.snapshot
/code/frameworks/angular/template/stories/others/issues/12009-unknown-component.stories.ts
/code/lib/cli/src/automigrate/fixes/angular-builders.ts
/test-storybooks/ember-cli/ember-output/storybook-docgen
/code/frameworks/ember/src/client/preview/docs
/code/renderers/vue3/template/stories_vue3-vite-default-ts/component-meta/reference-type-events
/code/frameworks/angular/src/builders/build-storybook/index.spec.ts
/.github/DISCUSSION_TEMPLATE/ideas.yml
/code/frameworks/angular/template/stories/others/issues
/code/core/src/manager-api/tests/stories.test.ts
/code/frameworks/angular/src/builders/start-storybook/index.spec.ts
/docs/_snippets/storybook-preview-with-angular-polyfills.md
/code/lib/cli/src/automigrate/fixes/angular-builders.test.ts
/docs/_snippets/storybook-interactions-step-function.md
/docs/_snippets/custom-build-script-production.md
/code/lib/cli/src/automigrate/fixes/angular-builders-multiproject.ts
/.github/DISCUSSION_TEMPLATE/help.yml
/docs/_snippets/button-story-hypothetical-example.md
/docs/_snippets/checkbox-story-grouped.md
/code/frameworks/angular/template/stories/basics/component-with-ng-content
/code/lib/cli/src/automigrate/fixes/angular-builders-multiproject.test.ts
/code/frameworks/angular/src/client/docs/testfixtures/doc-button/properties.snapshot
/code/addons/docs/template/stories/docspage/override.stories.ts
/docs/_snippets/my-component-story-mandatory-export.md

关于Greptile

此响应为您提供了研究的起点,而不是精确的解决方案。
帮助我们改进!如果这有帮助请留下一个👍,如果无关请留下一个👎。

r7knjye2

r7knjye22#

好的,那并不是完全正确的,但它指引我朝着正确的方向去解决它。

组件

import { Component, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'cv-button',
  standalone: true,
  template: `
<button (click)="onClick()">Click Me</button>
`,
  imports: [],
})
export class ButtonComponent {
  private count = 0
  @Output() clickCount = new EventEmitter<number>

  onClick() {
    this.count++
    this.clickCount.emit(this.count)
  }
}

故事

import { Meta, StoryObj, moduleMetadata } from '@storybook/angular';
import { ButtonComponent } from './button.component';
import { userEvent, within, expect, fn } from '@storybook/test';

const meta:Meta<ButtonComponent> = {
  title: 'Button Test',
  component: ButtonComponent,
  tags: ['autodocs'],
  args: {
    clickCount: fn()
  },
  decorators: [
    moduleMetadata({
      imports: []
    })
  ]
}

export default meta

type Story = StoryObj<ButtonComponent>

export const Primary: Story = {
  args: {},
  play: async ({canvasElement, args}) => {
    const element = within(canvasElement)   
    await userEvent.click(element.getByText('Click Me'))
    expect(args.clickCount).toHaveBeenCalledWith(1)
    await userEvent.click(element.getByText('Click Me'))
    expect(args.clickCount).toHaveBeenCalledWith(2)
  }
}

这个示例需要被整合到主文档的某个地方。

相关问题