fix: Typography copyable in clipboard incorrect (#34034)

Co-authored-by: chenxq <chenxq@neocrm.com>
This commit is contained in:
opopeieie 2022-02-16 19:41:06 +08:00 committed by GitHub
parent 0596e338ca
commit 58c1998e1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 4 deletions

View File

@ -197,10 +197,7 @@ const Base = React.forwardRef((props: InternalBlockProps, ref: any) => {
e?.preventDefault();
e?.stopPropagation();
if (copyConfig.text === undefined) {
copyConfig.text = String(children);
}
copy(copyConfig.text || '');
copy(copyConfig.text || String(children) || '');
setCopied(true);

View File

@ -1,6 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';
import { SmileOutlined, LikeOutlined } from '@ant-design/icons';
import * as copyObj from 'copy-to-clipboard';
import Base from '../Base';
@ -206,5 +207,34 @@ describe('Typography copy', () => {
wrapper.find('.ant-typography-copy').first().simulate('click');
expect(onDivClick).not.toBeCalled();
});
it('copy to clipboard', done => {
const spy = jest.spyOn(copyObj, 'default');
const originText = 'origin text.';
const nextText = 'next text.';
const Test = () => {
const [dynamicText, setDynamicText] = React.useState(originText);
React.useEffect(() => {
setTimeout(() => {
setDynamicText(nextText);
}, 500);
});
return (
<Base component="p" copyable>
{dynamicText}
</Base>
);
};
const wrapper = mount(<Test />);
const copyBtn = wrapper.find('.ant-typography-copy').first();
copyBtn.simulate('click');
expect(spy.mock.calls[0][0]).toEqual(originText);
setTimeout(() => {
spy.mockReset();
copyBtn.simulate('click');
expect(spy.mock.calls[0][0]).toEqual(nextText);
done();
}, 500);
});
});
});