Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(MessageBox): allow any react node to be passed as children #670

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions packages/main/src/components/MessageBox/MessageBox.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Title, Subtitle, Description, Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs/blocks';
import { DocsHeader } from '@shared/stories/DocsHeader';
import { Button } from '@ui5/webcomponents-react/lib/Button';
import { MessageBox } from '@ui5/webcomponents-react/lib/MessageBox';
import { MessageBoxActions } from '@ui5/webcomponents-react/lib/MessageBoxActions';
import { MessageBoxTypes } from '@ui5/webcomponents-react/lib/MessageBoxTypes';
import { useRef, useCallback } from 'react';
import { createSelectArgTypes } from '@shared/stories/createSelectArgTypes';

<Meta
title="Components / MessageBox"
component={MessageBox}
argTypes={{ ref: { type: null }, ...createSelectArgTypes({ type: MessageBoxTypes }) }}
args={{
open: false,
type: MessageBoxTypes.CONFIRM,
children: 'My Message Box Content'
}}
/>

<DocsHeader />

<Canvas>
<Story name="Default">
{(args) => {
const messageBoxRef = useRef();
const onButtonClick = useCallback(
(e) => {
messageBoxRef.current.open();
},
[messageBoxRef]
);
const onCloseMessageBox = useCallback(
(e) => {
messageBoxRef.current.close();
},
[messageBoxRef]
);
return (
<>
<Button onClick={onButtonClick}>Open Messagebox</Button>
<MessageBox
ref={messageBoxRef}
type={args.type}
open={args.open}
onClose={onCloseMessageBox}
title={args.title}
>
{args.children}
</MessageBox>
</>
);
}}
</Story>
</Canvas>

<ArgsTable story="." />

### with Custom Action

<Canvas>
<Story name="With Custom Action">
{(args) => {
const messageBoxRef = useRef();
const onButtonClick = useCallback(
(e) => {
messageBoxRef.current.open();
},
[messageBoxRef]
);
const onCloseMessageBox = useCallback(
(e) => {
messageBoxRef.current.close();
},
[messageBoxRef]
);
return (
<>
<Button onClick={onButtonClick}>Open Messagebox</Button>
<MessageBox
ref={messageBoxRef}
type={args.type}
open={args.open}
onClose={onCloseMessageBox}
title={args.title}
actions={[MessageBoxActions.OK, 'A custom action', MessageBoxActions.CANCEL]}
>
{args.children}
</MessageBox>
</>
);
}}
</Story>
</Canvas>
95 changes: 35 additions & 60 deletions packages/main/src/components/MessageBox/MessageBox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import { createPassThroughPropsTest } from '@shared/tests/utils';
import { fireEvent, render, screen } from '@shared/tests';
import { createPassThroughPropsTest } from '@shared/tests/utils';
import '@ui5/webcomponents-icons/dist/icons/add';
import { Icon } from '@ui5/webcomponents-react/lib/Icon';
import { MessageBox } from '@ui5/webcomponents-react/lib/MessageBox';
import { MessageBoxActions } from '@ui5/webcomponents-react/lib/MessageBoxActions';
import { MessageBoxTypes } from '@ui5/webcomponents-react/lib/MessageBoxTypes';
import React from 'react';

describe('MessageBox', () => {
test('Confirm - OK', () => {
test.each([
[MessageBoxTypes.CONFIRM, MessageBoxActions.OK],
[MessageBoxTypes.SUCCESS, MessageBoxActions.OK],
[MessageBoxTypes.WARNING, MessageBoxActions.OK],
[MessageBoxTypes.ERROR, MessageBoxActions.CLOSE],
[MessageBoxTypes.INFORMATION, MessageBoxActions.OK],
[MessageBoxTypes.HIGHLIGHT, MessageBoxActions.OK]
])('%s', (type, buttonText) => {
const callback = jest.fn();
const { asFragment } = render(
<MessageBox type={MessageBoxTypes.CONFIRM} open onClose={callback}>
Confirm
const { asFragment, unmount } = render(
<MessageBox type={type} open onClose={callback}>
My Message Box Content
</MessageBox>
);

expect(asFragment()).toMatchSnapshot();

fireEvent.click(screen.getByText('OK'));
fireEvent.click(screen.getByText(buttonText));

expect(callback.mock.calls[0][0].detail.action).toEqual(MessageBoxActions.OK);
expect(callback.mock.calls[0][0].detail.action).toEqual(buttonText);
unmount();
});

test('Confirm - Cancel', () => {
Expand All @@ -35,58 +45,6 @@ describe('MessageBox', () => {
expect(callback.mock.calls[0][0].detail.action).toEqual(MessageBoxActions.CANCEL);
});

test('Success', () => {
const callback = jest.fn();
const { asFragment } = render(
<MessageBox type={MessageBoxTypes.SUCCESS} open onClose={callback}>
Success
</MessageBox>
);
expect(asFragment()).toMatchSnapshot();

fireEvent.click(screen.getByText('OK'));
expect(callback.mock.calls[0][0].detail.action).toEqual(MessageBoxActions.OK);
});

test('Warning', () => {
const callback = jest.fn();
const { asFragment } = render(
<MessageBox type={MessageBoxTypes.WARNING} open onClose={callback}>
Warning
</MessageBox>
);
expect(asFragment()).toMatchSnapshot();

fireEvent.click(screen.getByText('OK'));
expect(callback.mock.calls[0][0].detail.action).toEqual(MessageBoxActions.OK);
});

test('Error', () => {
const callback = jest.fn();
const { asFragment } = render(
<MessageBox type={MessageBoxTypes.ERROR} open onClose={callback}>
Error
</MessageBox>
);
expect(asFragment()).toMatchSnapshot();

fireEvent.click(screen.getByText('Close'));
expect(callback.mock.calls[0][0].detail.action).toEqual(MessageBoxActions.CLOSE);
});

test('Information', () => {
const callback = jest.fn();
const { asFragment } = render(
<MessageBox type={MessageBoxTypes.INFORMATION} open onClose={callback}>
Information
</MessageBox>
);
expect(asFragment()).toMatchSnapshot();

fireEvent.click(screen.getByText('OK'));
expect(callback.mock.calls[0][0].detail.action).toEqual(MessageBoxActions.OK);
});

test('Show', () => {
const callback = jest.fn();
const { asFragment } = render(
Expand All @@ -107,7 +65,13 @@ describe('MessageBox', () => {
test('Success w/ custom title', () => {
const callback = jest.fn();
const { asFragment } = render(
<MessageBox type={MessageBoxTypes.SUCCESS} open onClose={callback} title="Custom Success">
<MessageBox
type={MessageBoxTypes.SUCCESS}
open
onClose={callback}
title="Custom Success"
icon={<Icon name="add" />}
>
Custom Success
</MessageBox>
);
Expand Down Expand Up @@ -162,5 +126,16 @@ describe('MessageBox', () => {
expect(onClose.mock.calls[1][0].detail.action).toEqual('My Custom Action');
});

test("Don't crash on unknown type", () => {
const callback = jest.fn();
const { asFragment } = render(
<MessageBox open onClose={callback} type="FOO_BAR">
Unknown Type!
</MessageBox>
);

expect(asFragment()).toMatchSnapshot();
});

createPassThroughPropsTest(MessageBox);
});
Loading