2019-08-13 15:20:56 -04:00
|
|
|
import React from 'react';
|
|
|
|
import {mountWithIntl} from '../../helpers/intl-helpers.jsx';
|
|
|
|
import InfoButton from '../../../src/components/info-button/info-button';
|
|
|
|
|
|
|
|
describe('InfoButton', () => {
|
2019-09-23 15:17:02 -04:00
|
|
|
// mock window.addEventListener
|
|
|
|
// for more on this technique, see discussion at https://github.com/airbnb/enzyme/issues/426#issuecomment-253515886
|
|
|
|
const mockedAddEventListener = {};
|
|
|
|
/* eslint-disable no-undef */
|
|
|
|
window.addEventListener = jest.fn((event, cb) => {
|
|
|
|
mockedAddEventListener[event] = cb;
|
|
|
|
});
|
|
|
|
/* eslint-enable no-undef */
|
|
|
|
|
2019-08-13 15:20:56 -04:00
|
|
|
test('Info button defaults to not visible', () => {
|
|
|
|
const component = mountWithIntl(
|
|
|
|
<InfoButton
|
|
|
|
message="Here is some info about something!"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(false);
|
|
|
|
});
|
2019-09-23 15:17:02 -04:00
|
|
|
|
|
|
|
test('mouseOver on info button makes info message visible', done => {
|
2019-08-17 21:44:00 -04:00
|
|
|
const component = mountWithIntl(
|
|
|
|
<InfoButton
|
|
|
|
message="Here is some info about something!"
|
|
|
|
/>
|
|
|
|
);
|
2019-09-23 15:17:02 -04:00
|
|
|
|
|
|
|
// mouseOver info button
|
2019-08-17 21:44:00 -04:00
|
|
|
component.find('div.info-button').simulate('mouseOver');
|
2023-10-24 14:22:28 -04:00
|
|
|
setTimeout(() => { // necessary because mouseover uses debounce
|
2019-09-23 15:17:02 -04:00
|
|
|
// crucial: if we don't call update(), then find() below looks through an OLD
|
|
|
|
// version of the DOM! see https://github.com/airbnb/enzyme/issues/1233#issuecomment-358915200
|
|
|
|
component.update();
|
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(true);
|
|
|
|
done();
|
|
|
|
}, 500);
|
2019-08-17 21:44:00 -04:00
|
|
|
});
|
2019-09-23 15:17:02 -04:00
|
|
|
|
2019-08-13 15:20:56 -04:00
|
|
|
test('clicking on info button makes info message visible', () => {
|
|
|
|
const component = mountWithIntl(
|
|
|
|
<InfoButton
|
|
|
|
message="Here is some info about something!"
|
|
|
|
/>
|
|
|
|
);
|
2019-09-23 15:17:02 -04:00
|
|
|
const buttonRef = component.instance().buttonRef;
|
|
|
|
|
|
|
|
// click on info button
|
|
|
|
mockedAddEventListener.mousedown({target: buttonRef});
|
|
|
|
component.update();
|
|
|
|
expect(component.find('div.info-button').exists()).toEqual(true);
|
2019-08-13 15:20:56 -04:00
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(true);
|
|
|
|
});
|
2019-09-23 15:17:02 -04:00
|
|
|
|
|
|
|
test('clicking on info button, then mousing out makes info message still appear', done => {
|
2019-08-13 15:20:56 -04:00
|
|
|
const component = mountWithIntl(
|
|
|
|
<InfoButton
|
|
|
|
message="Here is some info about something!"
|
|
|
|
/>
|
|
|
|
);
|
2019-09-23 15:17:02 -04:00
|
|
|
const buttonRef = component.instance().buttonRef;
|
|
|
|
|
|
|
|
// click on info button
|
|
|
|
mockedAddEventListener.mousedown({target: buttonRef});
|
|
|
|
component.update();
|
|
|
|
expect(component.find('div.info-button').exists()).toEqual(true);
|
2019-08-13 15:20:56 -04:00
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(true);
|
2019-09-23 15:17:02 -04:00
|
|
|
|
2020-04-13 22:25:07 -04:00
|
|
|
// mouseLeave from info button
|
|
|
|
component.find('div.info-button').simulate('mouseLeave');
|
2023-10-24 14:22:28 -04:00
|
|
|
setTimeout(() => { // necessary because mouseover uses debounce
|
2019-09-23 15:17:02 -04:00
|
|
|
component.update();
|
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(true);
|
|
|
|
done();
|
|
|
|
}, 500);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clicking on info button, then clicking on it again makes info message go away', () => {
|
|
|
|
const component = mountWithIntl(
|
|
|
|
<InfoButton
|
|
|
|
message="Here is some info about something!"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const buttonRef = component.instance().buttonRef;
|
|
|
|
|
|
|
|
// click on info button
|
|
|
|
mockedAddEventListener.mousedown({target: buttonRef});
|
|
|
|
component.update();
|
|
|
|
expect(component.find('div.info-button').exists()).toEqual(true);
|
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(true);
|
|
|
|
|
|
|
|
// click on info button again
|
|
|
|
mockedAddEventListener.mousedown({target: buttonRef});
|
|
|
|
component.update();
|
2019-08-13 15:20:56 -04:00
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(false);
|
|
|
|
});
|
2019-09-23 15:17:02 -04:00
|
|
|
|
|
|
|
test('clicking on info button, then clicking somewhere else', () => {
|
|
|
|
const component = mountWithIntl(
|
|
|
|
<InfoButton
|
|
|
|
message="Here is some info about something!"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const buttonRef = component.instance().buttonRef;
|
|
|
|
|
|
|
|
// click on info button
|
|
|
|
mockedAddEventListener.mousedown({target: buttonRef});
|
|
|
|
component.update();
|
|
|
|
expect(component.find('div.info-button').exists()).toEqual(true);
|
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(true);
|
|
|
|
|
|
|
|
// click on some other target
|
|
|
|
mockedAddEventListener.mousedown({target: null});
|
|
|
|
component.update();
|
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(false);
|
|
|
|
});
|
|
|
|
|
2020-04-13 22:25:07 -04:00
|
|
|
test('after message is visible, mouseLeave makes it vanish', () => {
|
2019-09-23 15:17:02 -04:00
|
|
|
const component = mountWithIntl(
|
|
|
|
<InfoButton
|
|
|
|
message="Here is some info about something!"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
// mouseOver info button
|
|
|
|
component.find('div.info-button').simulate('mouseOver');
|
2020-04-13 22:25:07 -04:00
|
|
|
component.update();
|
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(true);
|
2019-09-23 15:17:02 -04:00
|
|
|
|
2020-04-13 22:25:07 -04:00
|
|
|
// mouseLeave away from info button
|
|
|
|
component.find('div.info-button').simulate('mouseLeave');
|
|
|
|
component.update();
|
|
|
|
expect(component.find('div.info-button-message').exists()).toEqual(false);
|
2019-09-23 15:17:02 -04:00
|
|
|
});
|
2019-08-13 15:20:56 -04:00
|
|
|
});
|