Skip to content

Commit

Permalink
feat(popups): Add ReplyField component (#444)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
Mingze and mergify[bot] authored Apr 21, 2020
1 parent f6f4580 commit f3b490c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 26 deletions.
41 changes: 22 additions & 19 deletions src/components/Popups/PopupReply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { KEYS } from 'box-ui-elements/es/constants';
import { FormattedMessage } from 'react-intl';
import messages from './messages';
import PopupBase from './PopupBase';
import ReplyField from './ReplyField';
import './PopupReply.scss';

export type Props = {
Expand Down Expand Up @@ -33,7 +34,7 @@ export default function PopupReply({ defaultValue, onCancel, onChange, onSubmit,
setText(event.target.value);
onChange(event.target.value);
};
const handleCreate = (event: React.MouseEvent): void => {
const handleSubmit = (event: React.FormEvent): void => {
handleEvent(event);
onSubmit(text);
};
Expand All @@ -59,24 +60,26 @@ export default function PopupReply({ defaultValue, onCancel, onChange, onSubmit,

return (
<PopupBase onKeyDown={handleKeyDown} options={{ onFirstUpdate: handleFirstUpdate }} {...rest}>
<div className="ba-Popup-main">
<textarea
ref={textareaRef}
className="ba-Popup-text"
data-testid="ba-Popup-text"
defaultValue={defaultValue}
onChange={handleChange}
onClick={handleEvent}
/>
</div>
<div className="ba-Popup-footer">
<Button data-testid="ba-Popup-cancel" onClick={handleCancel} type="button">
<FormattedMessage {...messages.buttonCancel} />
</Button>
<PrimaryButton data-testid="ba-Popup-submit" onClick={handleCreate} type="submit">
<FormattedMessage {...messages.buttonPost} />
</PrimaryButton>
</div>
<form className="ba-Popup-form" data-testid="ba-Popup-form" onSubmit={handleSubmit}>
<div className="ba-Popup-main">
<ReplyField
ref={textareaRef}
className="ba-Popup-text"
data-testid="ba-Popup-text"
defaultValue={defaultValue}
onChange={handleChange}
onClick={handleEvent}
/>
</div>
<div className="ba-Popup-footer">
<Button data-testid="ba-Popup-cancel" onClick={handleCancel} type="button">
<FormattedMessage {...messages.buttonCancel} />
</Button>
<PrimaryButton data-testid="ba-Popup-submit" type="submit">
<FormattedMessage {...messages.buttonPost} />
</PrimaryButton>
</div>
</form>
</PopupBase>
);
}
17 changes: 17 additions & 0 deletions src/components/Popups/ReplyField/ReplyField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';
import classnames from 'classnames';

export type Props = {
className?: string;
defaultValue?: string;
onChange: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
onClick: (event: React.SyntheticEvent) => void;
};

const ReplyField = (props: Props, ref: React.Ref<HTMLTextAreaElement>): JSX.Element => {
const { className, ...rest } = props;

return <textarea ref={ref} className={classnames('ba-TextArea', className)} {...rest} />;
};

export default React.forwardRef(ReplyField);
35 changes: 35 additions & 0 deletions src/components/Popups/ReplyField/__tests__/ReplyField-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import ReplyField, { Props } from '../ReplyField';

describe('components/Popups/ReplyField', () => {
const defaults: Props = {
className: 'ba-Popup-text',
defaultValue: '',
onChange: jest.fn(),
onClick: jest.fn(),
};

const getWrapper = (props = {}): ShallowWrapper => shallow(<ReplyField {...defaults} {...props} />);

describe('event handlers', () => {
test('should call onChange event', () => {
const wrapper = getWrapper();
const textarea = wrapper.find('textarea');

textarea.simulate('change', 'test');
textarea.simulate('click', 'test');

expect(defaults.onChange).toHaveBeenCalledWith('test');
expect(defaults.onClick).toHaveBeenCalledWith('test');
});
});

describe('render()', () => {
test('should render the textarea with right props', () => {
const wrapper = getWrapper();

expect(wrapper.prop('className')).toBe('ba-TextArea ba-Popup-text');
});
});
});
1 change: 1 addition & 0 deletions src/components/Popups/ReplyField/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './ReplyField';
14 changes: 7 additions & 7 deletions src/components/Popups/__tests__/PopupReply-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type PropsIndex = {
[key: string]: Function | HTMLElement;
};

describe('PopupReply', () => {
describe('components/Popups/PopupReply', () => {
const defaults: Props & PropsIndex = {
onCancel: jest.fn(),
onChange: jest.fn(),
Expand All @@ -37,14 +37,14 @@ describe('PopupReply', () => {

describe('event handlers', () => {
test.each`
testId | callback
${'ba-Popup-cancel'} | ${'onCancel'}
${'ba-Popup-submit'} | ${'onSubmit'}
`('should cancel the $testId button click events', ({ callback, testId }) => {
testId | event | callback
${'ba-Popup-cancel'} | ${'click'} | ${'onCancel'}
${'ba-Popup-form'} | ${'submit'} | ${'onSubmit'}
`('should cancel the $testId button $event event', ({ callback, event, testId }) => {
const wrapper = getWrapper();
const button = wrapper.find(`[data-testid="${testId}"]`);

button.simulate('click', mockEvent);
button.simulate(event, mockEvent);

expect(mockEvent.nativeEvent.stopImmediatePropagation).toHaveBeenCalled();
expect(mockEvent.stopPropagation).toHaveBeenCalled();
Expand All @@ -68,7 +68,7 @@ describe('PopupReply', () => {

test('should handle the textarea onChange event', () => {
const wrapper = getWrapper();
const textarea = wrapper.find(`textarea[data-testid="ba-Popup-text"]`);
const textarea = wrapper.find(`[data-testid="ba-Popup-text"]`);

textarea.simulate('change', { target: { value: 'test' } });

Expand Down

0 comments on commit f3b490c

Please sign in to comment.