Addon-docs: Module format embedding example (#7441)

Addon-docs: Module format embedding example
This commit is contained in:
Michael Shilman 2019-07-19 11:21:39 +08:00 committed by GitHub
commit 64219b87d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View File

@ -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';
@ -21,7 +21,17 @@ export const withSomeEmoji = () => (
</span>
</Button>
);
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',
};

View File

@ -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();
});
});