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(
document.getElementById(containerId)} >
); 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(
document.getElementById(containerId)} > {/* document.getElementById(containerId)} > */} document.getElementById(containerId)} wrapWhenSpecial={false} >
); 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(
Click here Click Me
); 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( ); 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); });