diff --git a/packages/heatmap/src/HeatMapCellCircle.tsx b/packages/heatmap/src/HeatMapCellCircle.tsx index 01ec7400d2..f9848cef0e 100644 --- a/packages/heatmap/src/HeatMapCellCircle.tsx +++ b/packages/heatmap/src/HeatMapCellCircle.tsx @@ -27,6 +27,7 @@ const NonMemoizedHeatMapCellCircle = ({ return ( ({ return ( { - const component = renderer.create() - - let tree = component.toJSON() - expect(tree).toMatchSnapshot() -}) - -it('should allow to disable labels', () => { - const wrapper = mount() - - const cells = wrapper.find(HeatMapCellRect) - expect(cells).toHaveLength(9) - cells.forEach(cell => { - expect(cell.find('text').exists()).toBe(false) - }) -}) - -it('should allow to format labels', () => { - const wrapper = mount( - datum[key].toFixed(2)} /> - ) - - const cells = wrapper.find(HeatMapCellRect) - expect(cells).toHaveLength(9) - cells.forEach(cell => { - expect(cell.find('text').exists()).toBe(true) - expect(cell.find('text').text().length).toEqual(5) - }) -}) diff --git a/packages/heatmap/tests/HeatMap.test.tsx b/packages/heatmap/tests/HeatMap.test.tsx new file mode 100644 index 0000000000..8eb3b105b2 --- /dev/null +++ b/packages/heatmap/tests/HeatMap.test.tsx @@ -0,0 +1,263 @@ +import { mount } from 'enzyme' +import { Globals } from '@react-spring/web' +// @ts-ignore +import { HeatMap, HeatMapSvgProps, DefaultHeatMapDatum } from '../src' + +const baseProps: HeatMapSvgProps> = { + width: 300, + height: 300, + data: [ + { + id: 'A', + data: [ + { x: 'X', y: 1 }, + { x: 'Y', y: 2 }, + { x: 'Z', y: 3 }, + ], + }, + { + id: 'B', + data: [ + { x: 'X', y: 3 }, + { x: 'Y', y: 2 }, + { x: 'Z', y: 1 }, + ], + }, + { + id: 'C', + data: [ + { x: 'X', y: 2 }, + { x: 'Y', y: 2 }, + { x: 'Z', y: 2 }, + ], + }, + ], +} + +const dataPoints: { + id: string + data: DefaultHeatMapDatum +}[] = [] +baseProps.data.forEach(serie => { + serie.data.forEach(datum => { + dataPoints.push({ + id: `${serie.id}.${datum.x}`, + data: datum, + }) + }) +}) + +const CELL_SHAPES = ['rect', 'circle'] as const + +beforeAll(() => { + Globals.assign({ skipAnimation: true }) +}) + +afterAll(() => { + Globals.assign({ skipAnimation: false }) +}) + +it('should render a basic heamap chart', () => { + const wrapper = mount() + + dataPoints.forEach(datum => { + expect(wrapper.find(`g[data-testid='cell.${datum.id}']`).exists()).toBeTruthy() + }) +}) + +describe('interactivity', () => { + CELL_SHAPES.forEach(shape => { + describe(shape, () => { + it('onClick', () => { + const onClick = jest.fn() + const wrapper = mount( + + ) + + dataPoints.forEach(datum => { + wrapper.find(`g[data-testid='cell.${datum.id}']`).simulate('click') + + expect(onClick).toHaveBeenCalledTimes(1) + const [data] = onClick.mock.calls[0] + expect(data.id).toEqual(datum.id) + expect(data.data).toEqual(datum.data) + + onClick.mockClear() + }) + }) + + it('onMouseEnter', () => { + const onMouseEnter = jest.fn() + const wrapper = mount( + + ) + + dataPoints.forEach(datum => { + wrapper.find(`g[data-testid='cell.${datum.id}']`).simulate('mouseenter') + + expect(onMouseEnter).toHaveBeenCalledTimes(1) + const [data] = onMouseEnter.mock.calls[0] + expect(data.id).toEqual(datum.id) + expect(data.data).toEqual(datum.data) + + onMouseEnter.mockClear() + }) + }) + + it('onMouseMove handler', () => { + const onMouseMove = jest.fn() + const wrapper = mount( + + ) + + dataPoints.forEach(datum => { + wrapper.find(`g[data-testid='cell.${datum.id}']`).simulate('mousemove') + + expect(onMouseMove).toHaveBeenCalledTimes(1) + const [data] = onMouseMove.mock.calls[0] + expect(data.id).toEqual(datum.id) + expect(data.data).toEqual(datum.data) + + onMouseMove.mockClear() + }) + }) + + it('onMouseLeave handler', () => { + const onMouseLeave = jest.fn() + const wrapper = mount( + + ) + + dataPoints.forEach(datum => { + wrapper.find(`g[data-testid='cell.${datum.id}']`).simulate('mouseleave') + + expect(onMouseLeave).toHaveBeenCalledTimes(1) + const [data] = onMouseLeave.mock.calls[0] + expect(data.id).toEqual(datum.id) + expect(data.data).toEqual(datum.data) + + onMouseLeave.mockClear() + }) + }) + + it('disabled if non interactive', () => { + const onClick = jest.fn() + const onMouseEnter = jest.fn() + const onMouseMove = jest.fn() + const onMouseLeave = jest.fn() + + const wrapper = mount( + + ) + + dataPoints.forEach(datum => { + const cell = wrapper.find(`g[data-testid='cell.${datum.id}']`) + + cell.simulate('mouseenter') + expect(onMouseEnter).not.toHaveBeenCalled() + + cell.simulate('mousemove') + expect(onMouseMove).not.toHaveBeenCalled() + + cell.simulate('mouseleave') + expect(onMouseLeave).not.toHaveBeenCalled() + + cell.simulate('click') + expect(onClick).not.toHaveBeenCalled() + }) + }) + }) + }) +}) + +describe('layers', () => { + it('custom order', () => { + /* + const wrapper = mount() + + const layers = wrapper.find('svg > g').children() + expect(layers.at(0).is('NetworkNodes')).toBeTruthy() + expect(layers.at(1).is('NetworkLinks')).toBeTruthy() + */ + }) + + it('custom layer', () => { + /* + const CustomLayer = () => null + const wrapper = mount() + + const customLayer = wrapper.find(CustomLayer) + expect(customLayer.exists()).toBeTruthy() + + const customLayerProps = customLayer.props() + expect(customLayerProps).toHaveProperty('nodes') + expect(customLayerProps).toHaveProperty('links') + expect(customLayerProps).toHaveProperty('activeNodeIds') + expect(customLayerProps).toHaveProperty('setActiveNodeIds') + */ + }) +}) + +describe('annotations', () => { + it('rect annotation using id', () => { + /* + const annotatedNodeId = 'C' + const wrapper = mount( + + ) + + const annotation = wrapper.find(Annotation) + expect(annotation.exists()).toBeTruthy() + + const annotatedNode = wrapper.find(`circle[data-testid='node.${annotatedNodeId}']`) + const [nodeX, nodeY] = Array.from( + annotatedNode.prop('transform').match(/translate\(([0-9.]+),([0-9.]+)\)/) + ) + .slice(1) + .map(Number) + + expect(annotation.find('circle').first().prop('cx')).toEqual(nodeX) + expect(annotation.find('circle').first().prop('cy')).toEqual(nodeY) + */ + }) +}) + +describe('accessibility', () => { + it('aria properties', () => { + const wrapper = mount( + + ) + + const svg = wrapper.find('svg') + + expect(svg.prop('aria-label')).toBe('AriaLabel') + expect(svg.prop('aria-labelledby')).toBe('AriaLabelledBy') + expect(svg.prop('aria-describedby')).toBe('AriaDescribedBy') + }) +}) diff --git a/packages/heatmap/tests/__snapshots__/HeatMap.test.js.snap b/packages/heatmap/tests/__snapshots__/HeatMap.test.js.snap deleted file mode 100644 index da10185933..0000000000 --- a/packages/heatmap/tests/__snapshots__/HeatMap.test.js.snap +++ /dev/null @@ -1,612 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render a basic heat map chart 1`] = ` -
- - - - - - - - rent - - - - - - loan - - - - - - income - - - - - - - - - 20 - - - - - - 30 - - - - - - 40 - - - - - - - - 30 - - - - - - 24 - - - - - - 60 - - - - - - 40 - - - - - - 50 - - - - - - 90 - - - - - - 20 - - - - - - 13 - - - - - - 40 - - - - -
-`;