Skip to content

Commit

Permalink
Use jest.useFakeTimers instead of hard coded timeout for tooltip test…
Browse files Browse the repository at this point in the history
…s. (#74642) (#74926)

Refactor to use jest.useFakeTimers().
  • Loading branch information
walterra committed Aug 19, 2020
1 parent 7edb74c commit de7c2fb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,33 @@ describe('FieldTitleBar', () => {
expect(hasClassName).toBeTruthy();
});

test(`tooltip hovering`, (done) => {
test(`tooltip hovering`, () => {
// Use fake timers so we don't have to wait for the EuiToolTip timeout
jest.useFakeTimers();

const props = { card: { fieldName: 'foo', type: 'bar' } };
const wrapper = mountWithIntl(<FieldTitleBar {...props} />);
const container = wrapper.find({ className: 'field-name' });

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

container.simulate('mouseover');
// EuiToolTip mounts children after a 250ms delay
setTimeout(() => {
wrapper.update();
expect(wrapper.find('EuiToolTip').children()).toHaveLength(2);
container.simulate('mouseout');
wrapper.update();
expect(wrapper.find('EuiToolTip').children()).toHaveLength(1);
done();
}, 250);

// Run the timers so the EuiTooltip will be visible
jest.runAllTimers();

wrapper.update();
expect(wrapper.find('EuiToolTip').children()).toHaveLength(2);

container.simulate('mouseout');

// Run the timers so the EuiTooltip will be hidden again
jest.runAllTimers();

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

// Clearing all mocks will also reset fake timers.
jest.clearAllMocks();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ describe('FieldTypeIcon', () => {
});

test(`render with tooltip and test hovering`, () => {
// Use fake timers so we don't have to wait for the EuiToolTip timeout
jest.useFakeTimers();

const typeIconComponent = mount(
<FieldTypeIcon type={ML_JOB_FIELD_TYPES.KEYWORD} tooltipEnabled={true} />
);
Expand All @@ -35,11 +38,23 @@ describe('FieldTypeIcon', () => {
expect(typeIconComponent.find('EuiToolTip').children()).toHaveLength(1);

container.simulate('mouseover');
// EuiToolTip mounts children after a 250ms delay
setTimeout(() => expect(typeIconComponent.find('EuiToolTip').children()).toHaveLength(2), 250);

// Run the timers so the EuiTooltip will be visible
jest.runAllTimers();

typeIconComponent.update();
expect(typeIconComponent.find('EuiToolTip').children()).toHaveLength(2);

container.simulate('mouseout');

// Run the timers so the EuiTooltip will be hidden again
jest.runAllTimers();

typeIconComponent.update();
expect(typeIconComponent.find('EuiToolTip').children()).toHaveLength(1);

// Clearing all mocks will also reset fake timers.
jest.clearAllMocks();
});

test(`update component`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ describe('Configuration button', () => {
});

test('it shows the tooltip when hovering the button', () => {
// Use fake timers so we don't have to wait for the EuiToolTip timeout
jest.useFakeTimers();

const msgTooltip = 'My message tooltip';
const titleTooltip = 'My title';

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

newWrapper.find('[data-test-subj="configure-case-button"]').first().simulate('mouseOver');
// EuiToolTip mounts children after a 250ms delay
setTimeout(
() =>
expect(newWrapper.find('.euiToolTipPopover').text()).toBe(`${titleTooltip}${msgTooltip}`),
250
);

// Run the timers so the EuiTooltip will be visible
jest.runAllTimers();

newWrapper.update();
expect(newWrapper.find('.euiToolTipPopover').text()).toBe(`${titleTooltip}${msgTooltip}`);

// Clearing all mocks will also reset fake timers.
jest.clearAllMocks();
});
});

0 comments on commit de7c2fb

Please sign in to comment.