Skip to content

Commit de7c2fb

Browse files
authored
Use jest.useFakeTimers instead of hard coded timeout for tooltip tests. (#74642) (#74926)
Refactor to use jest.useFakeTimers().
1 parent 7edb74c commit de7c2fb

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

x-pack/plugins/ml/public/application/components/field_title_bar/field_title_bar.test.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,33 @@ describe('FieldTitleBar', () => {
6262
expect(hasClassName).toBeTruthy();
6363
});
6464

65-
test(`tooltip hovering`, (done) => {
65+
test(`tooltip hovering`, () => {
66+
// Use fake timers so we don't have to wait for the EuiToolTip timeout
67+
jest.useFakeTimers();
68+
6669
const props = { card: { fieldName: 'foo', type: 'bar' } };
6770
const wrapper = mountWithIntl(<FieldTitleBar {...props} />);
6871
const container = wrapper.find({ className: 'field-name' });
6972

7073
expect(wrapper.find('EuiToolTip').children()).toHaveLength(1);
7174

7275
container.simulate('mouseover');
73-
// EuiToolTip mounts children after a 250ms delay
74-
setTimeout(() => {
75-
wrapper.update();
76-
expect(wrapper.find('EuiToolTip').children()).toHaveLength(2);
77-
container.simulate('mouseout');
78-
wrapper.update();
79-
expect(wrapper.find('EuiToolTip').children()).toHaveLength(1);
80-
done();
81-
}, 250);
76+
77+
// Run the timers so the EuiTooltip will be visible
78+
jest.runAllTimers();
79+
80+
wrapper.update();
81+
expect(wrapper.find('EuiToolTip').children()).toHaveLength(2);
82+
83+
container.simulate('mouseout');
84+
85+
// Run the timers so the EuiTooltip will be hidden again
86+
jest.runAllTimers();
87+
88+
wrapper.update();
89+
expect(wrapper.find('EuiToolTip').children()).toHaveLength(1);
90+
91+
// Clearing all mocks will also reset fake timers.
92+
jest.clearAllMocks();
8293
});
8394
});

x-pack/plugins/ml/public/application/components/field_type_icon/field_type_icon.test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ describe('FieldTypeIcon', () => {
2727
});
2828

2929
test(`render with tooltip and test hovering`, () => {
30+
// Use fake timers so we don't have to wait for the EuiToolTip timeout
31+
jest.useFakeTimers();
32+
3033
const typeIconComponent = mount(
3134
<FieldTypeIcon type={ML_JOB_FIELD_TYPES.KEYWORD} tooltipEnabled={true} />
3235
);
@@ -35,11 +38,23 @@ describe('FieldTypeIcon', () => {
3538
expect(typeIconComponent.find('EuiToolTip').children()).toHaveLength(1);
3639

3740
container.simulate('mouseover');
38-
// EuiToolTip mounts children after a 250ms delay
39-
setTimeout(() => expect(typeIconComponent.find('EuiToolTip').children()).toHaveLength(2), 250);
41+
42+
// Run the timers so the EuiTooltip will be visible
43+
jest.runAllTimers();
44+
45+
typeIconComponent.update();
46+
expect(typeIconComponent.find('EuiToolTip').children()).toHaveLength(2);
4047

4148
container.simulate('mouseout');
49+
50+
// Run the timers so the EuiTooltip will be hidden again
51+
jest.runAllTimers();
52+
53+
typeIconComponent.update();
4254
expect(typeIconComponent.find('EuiToolTip').children()).toHaveLength(1);
55+
56+
// Clearing all mocks will also reset fake timers.
57+
jest.clearAllMocks();
4358
});
4459

4560
test(`update component`, () => {

x-pack/plugins/security_solution/public/cases/components/configure_cases/button.test.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ describe('Configuration button', () => {
8080
});
8181

8282
test('it shows the tooltip when hovering the button', () => {
83+
// Use fake timers so we don't have to wait for the EuiToolTip timeout
84+
jest.useFakeTimers();
85+
8386
const msgTooltip = 'My message tooltip';
8487
const titleTooltip = 'My title';
8588

@@ -96,11 +99,14 @@ describe('Configuration button', () => {
9699
);
97100

98101
newWrapper.find('[data-test-subj="configure-case-button"]').first().simulate('mouseOver');
99-
// EuiToolTip mounts children after a 250ms delay
100-
setTimeout(
101-
() =>
102-
expect(newWrapper.find('.euiToolTipPopover').text()).toBe(`${titleTooltip}${msgTooltip}`),
103-
250
104-
);
102+
103+
// Run the timers so the EuiTooltip will be visible
104+
jest.runAllTimers();
105+
106+
newWrapper.update();
107+
expect(newWrapper.find('.euiToolTipPopover').text()).toBe(`${titleTooltip}${msgTooltip}`);
108+
109+
// Clearing all mocks will also reset fake timers.
110+
jest.clearAllMocks();
105111
});
106112
});

0 commit comments

Comments
 (0)