mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-02 05:03:44 +08:00
Official-storybook: Add hooks-based state example & embed it in jest
This commit is contained in:
parent
a9155e42d2
commit
ea087d02de
@ -1,15 +0,0 @@
|
||||
import { render, fireEvent } from 'react-testing-library';
|
||||
import { withText } from './button.stories';
|
||||
|
||||
const mockAction = jest.fn();
|
||||
jest.mock('@storybook/addon-actions', () => ({
|
||||
action: () => mockAction,
|
||||
}));
|
||||
|
||||
describe('button interactivity', () => {
|
||||
it('should handle clicks', () => {
|
||||
const comp = render(withText());
|
||||
fireEvent.click(comp.getByText('Hello Button'));
|
||||
expect(mockAction).toHaveBeenCalled();
|
||||
});
|
||||
});
|
@ -1,4 +1,4 @@
|
||||
import React from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import { action } from '@storybook/addon-actions';
|
||||
import { Button } from '@storybook/react/demo';
|
||||
|
||||
@ -24,3 +24,14 @@ export const withSomeEmoji = () => (
|
||||
withSomeEmoji.story = {
|
||||
name: 'with some emoji',
|
||||
};
|
||||
|
||||
export const withCounter = () =>
|
||||
React.createElement(() => {
|
||||
const [counter, setCounter] = useState(0);
|
||||
const label = `Testing: ${counter}`;
|
||||
return <Button onClick={() => setCounter(counter + 1)}>{label}</Button>;
|
||||
});
|
||||
|
||||
withCounter.story = {
|
||||
name: 'with coumter',
|
||||
};
|
||||
|
@ -0,0 +1,21 @@
|
||||
import { render, fireEvent } from 'react-testing-library';
|
||||
import { withText, withCounter } from './button.stories';
|
||||
|
||||
const mockAction = jest.fn();
|
||||
jest.mock('@storybook/addon-actions', () => ({
|
||||
action: () => mockAction,
|
||||
}));
|
||||
|
||||
describe('module story embedding', () => {
|
||||
it('should test actions', () => {
|
||||
const comp = render(withText());
|
||||
fireEvent.click(comp.getByText('Hello Button'));
|
||||
expect(mockAction).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should test story state', () => {
|
||||
const comp = render(withCounter());
|
||||
fireEvent.click(comp.getByText('Testing: 0'));
|
||||
expect(comp.getByText('Testing: 1')).toBeTruthy();
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user