import { clear } from 'jest-date-mock';
import * as _ from 'lodash';
import Tooltip from '../index';
import Button from '../../button';
import Select from '../../select';
import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
import { strings } from '../../../semi-foundation/tooltip/constants';
import { genAfterEach, genBeforeEach, mount, sleep } from '../../_test_/utils';
describe(`Tooltip`, () => {
beforeEach(() => {
clear();
genBeforeEach()();
});
afterEach(genAfterEach());
it(`test appearance`, async () => {
const containerId = `container`;
const scrollItemId = `scroll-item`;
const scrollContainerId = `scroll-container`;
const triggerId = `trigger`;
const demo = mount(
);
const elem = demo.find(Tooltip);
const tooltipOuter = document.querySelector(`.${BASE_CLASS_PREFIX}-portal-inner`);
// check if rendered
expect(document.querySelectorAll(`.${BASE_CLASS_PREFIX}-tooltip-wrapper`).length).toBe(1);
// scroll
const deltaY = 50;
const scrollContainer = demo.find(`#${scrollContainerId}`);
const oldTop = window.getComputedStyle(tooltipOuter).top;
scrollContainer.getDOMNode().scrollTop += deltaY;
scrollContainer.simulate('scroll');
await sleep(100);
const curTop = window.getComputedStyle(tooltipOuter).top;
expect(oldTop).toBe(curTop);
// click inside
document.querySelector(`.${BASE_CLASS_PREFIX}-tooltip-wrapper`).click();
await sleep(100);
expect(elem.state(`visible`)).toBe(true);
// click outside
// document.body.click();
document.dispatchEvent(new Event('mousedown', { bubbles: true, cancelable: true }));
// demo.find(`#${triggerId}`)
// .at(0)
// .simulate(`mouseDown`);
await sleep(100);
expect(elem.state('visible')).toBe(false);
// click button to show tooltip
document.getElementById(triggerId).click();
await sleep(100);
expect(elem.state('visible')).toBe(true);
// unmount elem
demo.unmount();
await sleep(100);
// document.body.click();
document.dispatchEvent(new Event('mousedown', { bubbles: true, cancelable: true }));
expect(document.getElementsByClassName(`${BASE_CLASS_PREFIX}-tooltip-wrapper`).length).toBe(0);
});
test(`test appearance with motion`, async () => {
const containerId = 'container';
const scrollItemId = `scroll-item`;
const scrollContainerId = `scroll-container`;
const triggerId = `trigger`;
const refObj = { current: null };
const refFn = sinon.spy();
const demo = mount(
);
await sleep(100);
const portalInners = document.querySelectorAll(`.${BASE_CLASS_PREFIX}-portal-inner`);
expect(portalInners.length).toBe(2);
expect(refObj.current).not.toBeNull();
expect(refFn.called).toBeTruthy();
// click outside
// document.body.click();
document.dispatchEvent(new Event('mousedown', { bubbles: true, cancelable: true }));
await sleep(100);
expect(
demo
.find(Tooltip)
.map(el => el.state('visible'))
.reduce((count, v) => (v ? count + 1 : count), 0)
).toBe(0);
});
test(`test special appearance`, async () => {
const containerId = 'container';
const scrollItemId = `scroll-item`;
const scrollContainerId = `scroll-container`;
const demo = mount(
);
await sleep(100);
expect(document.querySelectorAll(`.${BASE_CLASS_PREFIX}-portal-inner`).length).toBe(3);
// expect(document.querySelectorAll(`.${BASE_CLASS_PREFIX}-icons-triangle_arrow`).length).toBe(3);
});
test(`test events`, async () => {
const close = sinon.spy(() => {
demo.setProps({ visible: false });
});
const demo = mount(
}
visible={true}
trigger={'click'}
>
Click here
);
await sleep(100);
expect(document.querySelectorAll(`.${BASE_CLASS_PREFIX}-portal-inner`).length).toBe(1);
document.querySelector(`.${BASE_CLASS_PREFIX}-portal-inner .${BASE_CLASS_PREFIX}-button`).click();
await sleep(100);
expect(demo.state('visible')).toBe(false);
});
test(`vertical position: `, async () => {
const close = sinon.spy(() => {
demo.setProps({ visible: false });
});
const demo = mount(
}
visible={true}
trigger={'click'}
motion={() => false}
>
Click here
);
await sleep(100);
expect(document.querySelector(`.${BASE_CLASS_PREFIX}-portal-inner svg path`).getAttribute('d')).toBe('M0 0L1 0C1 4, 2 5.5, 4 7.5S7,10 7,12S6 14.5, 4 16.5S1,20 1,24L0 24L0 0z');
});
test(`test position: `, async () => {
const positionList = strings.POSITION_SET
for (const position of positionList) {
const demo = mount(
1
}
visible={true}
>
Click here
);
await sleep(100);
expect(document.querySelector(`.${BASE_CLASS_PREFIX}-tooltip-wrapper`).getAttribute('x-placement')).toBe(position);
}
});
it(`test click outside handler`, async () => {
const containerId = `container`;
const demo = mount(
);
const toolTipElem = demo.find(Tooltip);
const buttonElem = demo.find(Button);
// click inside
buttonElem.simulate('click');
toolTipElem.update();
await sleep(100);
expect(toolTipElem.state(`visible`)).toBe(true);
// click outside
// document.body.click();
document.dispatchEvent(new Event('mousedown', { bubbles: true, cancelable: true }));
toolTipElem.update();
await sleep(100);
expect(toolTipElem.state('visible')).toBe(false);
// click button to show tooltip
buttonElem.simulate('click');
toolTipElem.update();
await sleep(100);
expect(toolTipElem.state('visible')).toBe(true);
document.getElementById(containerId).dispatchEvent(new Event('mousedown', { bubbles: true, cancelable: true }));
toolTipElem.update();
await sleep(100);
expect(toolTipElem.state('visible')).toBe(false);
});
});
it('wrapperClassName', () => {
const wrapperWithMultipleChildren = mount(
);
expect(wrapperWithMultipleChildren.exists('.test')).toEqual(true);
const wrapperWithDisabledButton = mount(
);
expect(wrapperWithDisabledButton.exists('.test')).toEqual(true);
const wrapperWithDisabledSelect = mount(
);
expect(wrapperWithDisabledSelect.exists('.test')).toEqual(true);
});