|
| 1 | +import { DomTooltipHandler } from '../../../../src/plugin/components/tooltip-handler/dom-tooltip-handler'; |
| 2 | +import { TOOLTIP_CONTAINER_EL_CLASS_NAME } from '../../../../src/plugin/components/tooltip-handler/constants'; |
| 3 | +import { createDiv, removeDom } from '../../../util/dom'; |
| 4 | + |
| 5 | +const createTooltipSpec = (spec: any) => ({ |
| 6 | + className: 'vchart-tooltip', |
| 7 | + style: { |
| 8 | + shape: {}, |
| 9 | + keyLabel: {}, |
| 10 | + valueLabel: {} |
| 11 | + }, |
| 12 | + ...spec |
| 13 | +}); |
| 14 | + |
| 15 | +const createHandler = (spec: any, chartContainer?: HTMLElement) => { |
| 16 | + const handler = new DomTooltipHandler() as any; |
| 17 | + |
| 18 | + handler._component = { |
| 19 | + getSpec: () => createTooltipSpec(spec) |
| 20 | + }; |
| 21 | + handler._chartOption = { |
| 22 | + getTheme: (): undefined => undefined |
| 23 | + }; |
| 24 | + handler._chartContainer = chartContainer; |
| 25 | + handler._initStyle(); |
| 26 | + |
| 27 | + return handler as DomTooltipHandler; |
| 28 | +}; |
| 29 | + |
| 30 | +describe('DomTooltipHandler', () => { |
| 31 | + let container: HTMLElement; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + container = createDiv(); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + removeDom(container); |
| 39 | + }); |
| 40 | + |
| 41 | + it('falls back to chart container when tooltip parentElement is not a dom element', () => { |
| 42 | + const handler = createHandler( |
| 43 | + { |
| 44 | + parentElement: {} |
| 45 | + }, |
| 46 | + container |
| 47 | + ); |
| 48 | + |
| 49 | + expect(() => handler.initRootDom()).not.toThrow(); |
| 50 | + expect(container.querySelector(`.${TOOLTIP_CONTAINER_EL_CLASS_NAME}`)).toBe(handler.getTooltipContainer()); |
| 51 | + expect(container.querySelector('.vchart-tooltip')).toBe(handler.getRootDom()); |
| 52 | + }); |
| 53 | + |
| 54 | + it('creates tooltip container under an empty valid parentElement', () => { |
| 55 | + const parentElement = createDiv(container); |
| 56 | + const handler = createHandler({ |
| 57 | + parentElement |
| 58 | + }); |
| 59 | + |
| 60 | + handler.initEl(); |
| 61 | + |
| 62 | + expect(parentElement.querySelector(`.${TOOLTIP_CONTAINER_EL_CLASS_NAME}`)).toBe(handler.getTooltipContainer()); |
| 63 | + }); |
| 64 | +}); |
0 commit comments