Comments (2)
** Disclaimer** This information might be inaccurate, due to it being generated automatically
To verify that a component is emitting an event in Storybook for Angular, you can use the args
parameter in the play
function to listen for the event. Here's how you can modify your story to check if clickCount
was incremented:
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);
}
};
This code uses Jest to create a spy function that listens for the clickCount
event and verifies that it was called with the expected value.
References
/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
About Greptile
This response provides a starting point for your research, not a precise solution.
Help us improve! Please leave a ๐ if this is helpful and ๐ if it is irrelevant.
from storybook.
Ok, that wasn't entirely correct, but it pointed me in the correct direction to solve it.
The Component
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)
}
}
The Story
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)
}
}
This example needs to be incorporated into the main documentation somewhere.
from storybook.
Related Issues (20)
- [Tracking]: Replace Lodash Usage with Native Functionality or Alternatives HOT 10
- CLI: Add SvelteKit to storybook@latest init in empty directory HOT 1
- [Bug]: @storybook/core is (wrongly) specifying @storybook/global as a devDependency HOT 5
- [Bug]: Storybook doesn't work with `Yarn 4 (nodeLinker: pnpm)`, `Vue 3`, `Vite 5`, `Storybook 8`
- [Bug]: Onboarding fails when PascalCase story is created HOT 1
- Vitest: Create "Component Coverage" per story file HOT 1
- Vitest|Storybook: Create communication channel to gather coverage information HOT 1
- File Selection: Show coverage for all relevant files HOT 1
- Storybook Coverage Viewer Addon - Create UI for Displaying Coverage Data HOT 1
- [Bug]: Boolean controls break when attributes names use hyphens(-) in Lit HOT 1
- [Bug]: error when starting `storybook` using `postcss-url` plugin to inline assets HOT 1
- [Bug]: Cannot access Next router in custom container when using NextJS preset with Docs add-on
- [Bug]: Docs broken with high CPU usage HOT 3
- [Bug]: Docs page menu item cannot be clicked; the page does not respond. HOT 4
- [Bug]: Docs page renders the Toast multiple times
- [Bug]: Addon build fails due to missing `@storybook/manager/globals` on 8.2.x
- [Bug]: links on docs page for config point to 404 HOT 2
- [Bug]: @storybook/addon-coverage does not work with CRA (Create React App) setup HOT 1
- [Bug]: Seeing warning about CJS even though my project is ESM HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from storybook.