Skip to content

Commit 9fc6b39

Browse files
chore(eui): rewrite EuiCopy tests to RTL
1 parent 6f12545 commit 9fc6b39

1 file changed

Lines changed: 56 additions & 13 deletions

File tree

packages/eui/src/components/copy/copy.test.tsx

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,33 @@
77
*/
88

99
import React from 'react';
10-
import { shallow } from 'enzyme';
11-
import { render } from '../../test/rtl';
10+
import { fireEvent } from '@testing-library/react';
11+
import {
12+
waitForEuiToolTipVisible,
13+
waitForEuiToolTipHidden,
14+
render,
15+
} from '../../test/rtl';
1216
import { requiredProps } from '../../test';
1317

1418
import { EuiCopy } from './copy';
1519

1620
describe('EuiCopy', () => {
21+
const originalExecCommand = document.execCommand;
22+
23+
beforeAll(() => {
24+
Object.defineProperty(document, 'execCommand', {
25+
value: jest.fn(() => true),
26+
writable: true,
27+
});
28+
});
29+
30+
afterAll(() => {
31+
Object.defineProperty(document, 'execCommand', {
32+
value: originalExecCommand,
33+
writable: true,
34+
});
35+
});
36+
1737
it('renders', () => {
1838
const { container } = render(
1939
<EuiCopy textToCopy="some text" {...requiredProps}>
@@ -24,23 +44,46 @@ describe('EuiCopy', () => {
2444
});
2545

2646
describe('props', () => {
27-
test('beforeMessage', () => {
28-
const component = shallow(
29-
<EuiCopy textToCopy="some text" beforeMessage="copy this">
30-
{(copy) => <button onClick={copy}>Click to copy input text</button>}
47+
it('beforeMessage', async () => {
48+
const beforeMessage = 'copy this';
49+
const { getByRole, getByText } = render(
50+
<EuiCopy textToCopy="some text" beforeMessage={beforeMessage}>
51+
{() => (
52+
<button onMouseOver={() => {}} onFocus={() => {}}>
53+
Click to copy input text
54+
</button>
55+
)}
3156
</EuiCopy>
3257
);
33-
expect(component.state('tooltipText')).toBe('copy this');
58+
// Simulate mouse over to show the tooltip
59+
fireEvent.mouseOver(getByRole('button'));
60+
await waitForEuiToolTipVisible();
61+
// The beforeMessage should be shown in the tooltip
62+
expect(getByText(beforeMessage)).toBeInTheDocument();
63+
fireEvent.mouseOut(getByRole('button'));
64+
await waitForEuiToolTipHidden();
3465
});
3566

36-
test('afterMessage', () => {
37-
const component = shallow<EuiCopy>(
38-
<EuiCopy textToCopy="some text" afterMessage="successfuly copied">
39-
{(copy) => <button onClick={copy}>Click to copy input text</button>}
67+
it('afterMessage', async () => {
68+
const afterMessage = 'successfully copied';
69+
const { getByRole, getByText } = render(
70+
<EuiCopy textToCopy="some text" afterMessage={afterMessage}>
71+
{(copy) => (
72+
<button onClick={copy} onMouseOver={() => {}} onFocus={() => {}}>
73+
Click to copy input text
74+
</button>
75+
)}
4076
</EuiCopy>
4177
);
42-
const instance = component.instance();
43-
expect(instance.props.afterMessage).toBe('successfuly copied');
78+
79+
// Simulate a click to copy the text
80+
fireEvent.click(getByRole('button'));
81+
fireEvent.mouseOver(getByRole('button'));
82+
await waitForEuiToolTipVisible();
83+
// The afterMessage should be shown after the copy action
84+
expect(getByText(afterMessage)).toBeInTheDocument();
85+
fireEvent.mouseOut(getByRole('button'));
86+
await waitForEuiToolTipHidden();
4487
});
4588
});
4689
});

0 commit comments

Comments
 (0)