Skip to content

Commit

Permalink
fix: Exception in enter key callback on editable or copyable Paragraph (
Browse files Browse the repository at this point in the history
#33976)

* fix: Exception on enter key callback

* Move spyOn position
  • Loading branch information
mrwd2009 committed Feb 9, 2022
1 parent 29d3407 commit 745ebf2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
8 changes: 4 additions & 4 deletions components/typography/Base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ const Base = React.forwardRef((props: InternalBlockProps, ref: any) => {
}
}, [editing]);

const onEditClick = (e: React.MouseEvent<HTMLDivElement>) => {
e.preventDefault();
const onEditClick = (e?: React.MouseEvent<HTMLDivElement>) => {
e?.preventDefault();
triggerEdit(true);
};

Expand All @@ -192,8 +192,8 @@ const Base = React.forwardRef((props: InternalBlockProps, ref: any) => {
clearTimeout(copyIdRef.current!);
};

const onCopyClick = (e: React.MouseEvent<HTMLDivElement>) => {
e.preventDefault();
const onCopyClick = (e?: React.MouseEvent<HTMLDivElement>) => {
e?.preventDefault();

if (copyConfig.text === undefined) {
copyConfig.text = String(children);
Expand Down
32 changes: 32 additions & 0 deletions components/typography/__tests__/enter-key-callback.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { mount } from 'enzyme';
import KeyCode from 'rc-util/lib/KeyCode';
import Paragraph from '../Paragraph';

test('Callback on enter key is triggered', () => {
const onEditStart = jest.fn();
const onCopy = jest.fn();

const wrapper = mount(
<Paragraph
copyable={{
onCopy,
}}
editable={{
onStart: onEditStart,
}}
>
test
</Paragraph>,
);
const timer: any = 9527;
jest.spyOn(window, 'setTimeout').mockReturnValue(timer);
jest.spyOn(window, 'clearTimeout');
// must copy first, because editing button will hide copy button
wrapper.find('.ant-typography-copy').at(0).simulate('keyup', { keyCode: KeyCode.ENTER });
wrapper.find('.anticon-edit').at(0).simulate('keyup', { keyCode: KeyCode.ENTER });

expect(onEditStart.mock.calls.length).toBe(1);
expect(onCopy.mock.calls.length).toBe(1);
jest.restoreAllMocks();
});

0 comments on commit 745ebf2

Please sign in to comment.