From f34cdd18710d6dce6a6ed8813ee89dc56af54834 Mon Sep 17 00:00:00 2001 From: Chris Williams Date: Tue, 6 Oct 2020 19:52:37 -0700 Subject: [PATCH] test(xychart): add more Tooltip tests --- .../test/components/Tooltip.test.tsx | 123 +++++++++++++++--- 1 file changed, 106 insertions(+), 17 deletions(-) diff --git a/packages/visx-xychart/test/components/Tooltip.test.tsx b/packages/visx-xychart/test/components/Tooltip.test.tsx index 360a031ae..84bf57c86 100644 --- a/packages/visx-xychart/test/components/Tooltip.test.tsx +++ b/packages/visx-xychart/test/components/Tooltip.test.tsx @@ -2,29 +2,42 @@ import React from 'react'; import ResizeObserver from 'resize-observer-polyfill'; import { mount } from 'enzyme'; import { Tooltip as BaseTooltip } from '@visx/tooltip'; -import { Tooltip, TooltipContext, TooltipContextType } from '../../src'; +import { DataContext, Tooltip, TooltipContext, TooltipContextType } from '../../src'; import { TooltipProps } from '../../src/components/Tooltip'; +import getDataContext from '../mocks/getDataContext'; describe('', () => { type SetupProps = | { - props?: Partial; - context?: Partial; + props?: Partial>; + context?: Partial>; + Parent?: ({ children }: { children: React.ReactElement }) => React.ReactElement; } | undefined; - function setup({ props, context }: SetupProps = {}) { + + function setup({ + props, + context, + Parent = ({ children }: { children: React.ReactElement }) => children, + }: SetupProps = {}) { const wrapper = mount( - - null} {...props} /> - , + + +
} + {...props} + /> + + , ); return wrapper; } @@ -38,13 +51,13 @@ describe('', () => { }); it('should not render a BaseTooltip when TooltipContext.tooltipOpen=true and renderTooltip returns false', () => { - const wrapper = setup({ context: { tooltipOpen: true } }); + const wrapper = setup({ context: { tooltipOpen: true }, props: { renderTooltip: () => null } }); expect(wrapper.find(BaseTooltip)).toHaveLength(0); }); it('should render a BaseTooltip when TooltipContext.tooltipOpen=true and renderTooltip returns non-null', () => { const wrapper = setup({ - props: { renderTooltip: () =>
}, + props: { renderTooltip: () =>
, snapTooltipToDatumX: true }, context: { tooltipOpen: true }, }); expect(wrapper.find(BaseTooltip)).toHaveLength(1); @@ -66,4 +79,80 @@ describe('', () => { }); expect(renderTooltip).toHaveBeenCalledTimes(1); }); + + it('should render a vertical crosshair if showVerticalCrossHair=true', () => { + const wrapper = setup({ + props: { showVerticalCrosshair: true }, + context: { tooltipOpen: true }, + }); + expect(wrapper.find('div.visx-crosshair-vertical')).toHaveLength(1); + }); + + it('should render a horizontal crosshair if showVerticalCrossHair=true', () => { + const wrapper = setup({ + props: { showHorizontalCrosshair: true }, + context: { tooltipOpen: true }, + }); + expect(wrapper.find('div.visx-crosshair-horizontal')).toHaveLength(1); + }); + + it('should not render a circle if showDatumCircle=true and there is no nearestDatum', () => { + const wrapper = setup({ + props: { showDatumCircle: true }, + context: { tooltipOpen: true }, + }); + expect(wrapper.find('div.visx-tooltip-circle')).toHaveLength(0); + }); + it('should render a circle if showDatumCircle=true if there is a nearestDatum', () => { + const wrapper = setup({ + props: { showDatumCircle: true }, + context: { + tooltipOpen: true, + tooltipData: { + nearestDatum: { distance: 1, key: '', index: 0, datum: {} }, + datumByKey: {}, + }, + }, + }); + expect(wrapper.find('div.visx-tooltip-circle')).toHaveLength(1); + }); + it('should render a circle for each series if showSeriesCircles=true', () => { + const wrapper = setup({ + props: { showSeriesCircles: true }, + context: { + tooltipOpen: true, + tooltipData: { + datumByKey: { + d1: { key: 'd1', index: 0, datum: {} }, + d2: { key: 'd2', index: 1, datum: {} }, + }, + }, + }, + Parent: ( + { children }, // circles snap to data points, so scales/accessors must exist + ) => ( + 3, + yAccessor: () => 7, + data: [{}], + }, + { + key: 'd2', + xAccessor: () => 3, + yAccessor: () => 7, + data: [{}], + }, + ]), + }} + > + {children} + + ), + }); + expect(wrapper.find('div.visx-tooltip-circle')).toHaveLength(2); + }); });